Skip to content

Instantly share code, notes, and snippets.

@qsLI
Forked from ocadaruma/LongTTSP.java
Created November 19, 2024 06:58
Show Gist options
  • Save qsLI/1036408f5fd6f2f4a7b6868e72aeac27 to your computer and use it in GitHub Desktop.
Save qsLI/1036408f5fd6f2f4a7b6868e72aeac27 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