Created
December 25, 2022 08:28
-
-
Save ocadaruma/c459addbacf78550261fc94a3bdda916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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