Skip to content

Instantly share code, notes, and snippets.

@ocadaruma
Created December 25, 2022 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocadaruma/c459addbacf78550261fc94a3bdda916 to your computer and use it in GitHub Desktop.
Save ocadaruma/c459addbacf78550261fc94a3bdda916 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.time.LocalDateTime;
public class LongTTSP {
public static void main(String[] args) throws Exception {
log(String.format("pid: %d", ProcessHandle.current().pid()));
Thread th = new Thread(() -> {
log("Gonna read from mapped file");
long t0 = System.nanoTime();
new MappedFile(args[0]).read();
log(String.format("Finished read in %d us", (System.nanoTime() - t0) / 1000));
});
th.setName("reader-thread");
th.start();
log("Sleeping...");
Thread.sleep(10000L);
}
static class MappedFile {
private final MappedByteBuffer buffer;
MappedFile(String path) {
try (FileChannel channel = FileChannel.open(Paths.get(path))) {
buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
void read() {
buffer.get(0);
}
}
static void log(String msg) {
System.out.printf("[%s] (%s) %s\n",
LocalDateTime.now(),
Thread.currentThread().getName(),
msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment