Skip to content

Instantly share code, notes, and snippets.

@g4s8
Last active January 16, 2020 10:18
Show Gist options
  • Save g4s8/7d182f199989b990bb0927d93ff8bd0c to your computer and use it in GitHub Desktop.
Save g4s8/7d182f199989b990bb0927d93ff8bd0c to your computer and use it in GitHub Desktop.
package wtf.g4s8.qabot;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static java.nio.file.StandardOpenOption.*;
/**
*
* @since
*/
public final class Foo {
private static final int BUF_SIZE = 1024 * 1024 * 1024;
public static void main(String[] args) throws Exception {
final var f1 = Paths.get("/tmp/foo/1.txt");
final var f2 = Paths.get("/tmp/foo/2.txt");
final var f3 = Paths.get("/tmp/foo/3.txt");
final var inc = new AtomicInteger(1);
final var exec = Executors.newSingleThreadExecutor(
new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
final var thread = new Thread(r);
thread.setName(String.format("io-%d", inc.incrementAndGet()));
return thread;
}
}
);
final ByteBuffer b1 = newBuff(BUF_SIZE);
final ByteBuffer b2 = newBuff(BUF_SIZE);
final ByteBuffer b3 = newBuff(BUF_SIZE);
final var chan1 = AsynchronousFileChannel.open(f1, new HashSet<>(List.of(READ, WRITE, CREATE)), exec);
final var chan2 = AsynchronousFileChannel.open(f2, new HashSet<>(List.of(READ, WRITE, CREATE)), exec);
final var chan3 = AsynchronousFileChannel.open(f3, new HashSet<>(List.of(READ, WRITE, CREATE)), exec);
final long start = System.nanoTime();
final var ch1complete = new AtomicBoolean();
final var ch2complete = new AtomicBoolean();
final var ch3complete = new AtomicBoolean();
chan1.write(b1, 0, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(final Integer result, final Void attachment) {
final long end = System.nanoTime();
System.out.printf("chan1 completed in %d (ms)\n", (start - end) / 1_000_000);
ch1complete.set(true);
}
@Override
public void failed(final Throwable exc, final Void attachment) {
final long end = System.nanoTime();
System.out.printf("chan1 failed in %d (ms): %s\n", (start - end) / 1_000_000, exc);
ch1complete.set(true);
}
});
chan2.write(b2, 0, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(final Integer result, final Void attachment) {
final long end = System.nanoTime();
System.out.printf("chan2 completed in %d (ms)\n", (start - end) / 1_000_000);
ch2complete.set(true);
}
@Override
public void failed(final Throwable exc, final Void attachment) {
final long end = System.nanoTime();
System.out.printf("chan2 failed in %d (ms): %s\n", (start - end) / 1_000_000, exc);
ch2complete.set(true);
}
});
chan3.write(b3, 0, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(final Integer result, final Void attachment) {
final long end = System.nanoTime();
System.out.printf("chan3 completed in %d (ms)\n", (start - end) / 1_000_000);
ch3complete.set(true);
}
@Override
public void failed(final Throwable exc, final Void attachment) {
final long end = System.nanoTime();
System.out.printf("chan3 failed in %d (ms): %s\n", (start - end) / 1_000_000, exc);
ch3complete.set(true);
}
});
Thread.sleep(10);
exec.submit(() -> {
final var end = System.nanoTime();
System.out.printf("print: %d\n", (start - end) / 1_000_000);
});
while (!Thread.currentThread().isInterrupted()) {
if (ch1complete.get() && ch2complete.get() && ch3complete.get()) {
final long end = System.nanoTime();
System.out.printf("both channels completed in %d (ms)\n", (start - end) / 1_000_000);
return;
}
}
}
private static ByteBuffer newBuff(int size) {
final var buf = ByteBuffer.allocate(size);
return buf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment