NIO vs Traditional IO
Fast recall
Quick revision
Core answer
Selector — one thread serves many sockets. Use streams for simple files; NIO for high-concurrency servers.Whether you know the core conceptual difference (stream/blocking vs buffer/non-blocking) and when each model fits.
Thinking NIO is always faster — for a single file read, traditional IO is simpler and not slower. NIO shines under high connection concurrency.
Mention Netty as a NIO framework, zero-copy with FileChannel.transferTo(), and that virtual threads (Loom) make traditional blocking IO competitive again.
Interview-ready guide
Interview answer
Java NIO (Non-Blocking I/O) was introduced to address the scalability limitation of traditional java.io, which requires one thread blocked per open connection. NIO uses Channels, Buffers, and Selectors to allow a single thread to monitor and serve thousands of connections simultaneously.
NIO abstractions:
- Channels: bidirectional data flow —
SocketChannel,FileChannel - Buffers: hold the data being read or written — explicit position/limit management
- Selectors: monitor multiple channels for readiness — maps to OS-level
epoll(Linux) /kqueue(macOS)
Zero-copy capability:
FileChannel.transferTo(): moves data from page cache to socket without entering user space- Eliminates the kernel-to-user-to-kernel copy that standard IO requires — significant throughput gain for file serving
Practical gotcha:
- Raw NIO is notoriously hard to get right — partial writes, selector wakeup races, cancelled-key cleanup are all traps
- Use Netty instead — it wraps NIO into a reliable event loop abstraction that handles these edge cases
Java 21 virtual threads:
- Blocking IO on a virtual thread parks it without consuming an OS thread — traditional
java.ioscales to high concurrency again - For new services: blocking IO with virtual threads is simpler and sufficient; reserve NIO for zero-copy file serving
I recommend blocking IO with virtual threads for new services; use NIO for zero-copy file serving or explicit back-pressure control.
NIO vs Traditional IO
Direct Answer
java.io uses blocking streams; NIO uses channels + ByteBuffer for scalable I/O. NIO enables multiplexing with Selector — one thread serves many sockets. Use streams for simple files; NIO for high-concurrency servers.
Common Mistakes & Pitfalls
Thinking NIO is always faster — for a single file read, traditional IO is simpler and not slower. NIO shines under high connection concurrency.
Check your understanding
Answer these without looking back at the article.
- How does Netty use NIO internally?
- What is zero-copy and how does FileChannel.transferTo() achieve it?
- How do virtual threads change the IO model in Java 21?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.