Video in progress

Does `AsynchronousFileChannel` really do asynchronous IO? When does it make sense to use it?

The API is asynchronous, but on Linux the JDK runs it in an internal thread pool with plain blocking pread/pwrite: kernel-level async file IO is not used. On Windows there is real overlapped IO. In most cases you get the same result from a plain FileChannel with your own executor.

📖 Answer

AsynchronousFileChannel came with NIO.2 in Java 7: read returns with a Future or a CompletionHandler.

var ch = AsynchronousFileChannel.open(p, Set.of(StandardOpenOption.READ), ioPool);
ByteBuffer buf = ByteBuffer.allocate(64 * 1024);
ch.read(buf, 0, null, new CompletionHandler<Integer, Void>() {
    public void completed(Integer n, Void a) { handle(buf.flip()); }
    public void failed(Throwable t, Void a)   { log.error("o'qish xatosi", t); }
});

What happens inside: on Unix systems the JDK uses "simulated" async: it gives the task to an executor, that thread reads in a blocking way, then calls the handler. So you don't save threads, the wait just moves to another thread. If you don't supply the executor yourself, a default pool is used and you don't control its size.

When it makes sense:

  • the code is already written in a callback/CompletableFuture style and the API fits;
  • many parallel file operations on Windows.

When it is useless: when you hope it will be faster because it's "async". The disk speed doesn't change.

For real async file IO on Linux there is io_uring, but the JDK doesn't use it in the standard API (as of 2026); it is exposed through external libraries such as Netty's io_uring transport. In Java 21+, the simplest path is often plain blocking code with virtual threads, keeping the heavy disk work in a bounded pool.