Blocking and Non-blocking I/O¶
Blocking and Non-blocking I/O define the behavior of a program when input/output operations are performed but data is not yet ready^[600-developer__java__java-io__bio-nio-aio.md]. The core distinction lies in the state of the thread while waiting for the operation to complete^[600-developer__java__java-io__bio-nio-aio.md].
Blocking I/O¶
In a blocking I/O model, the calling thread is suspended (put to sleep) until the result is returned^[600-developer__java__java-io__bio-nio-aio.md]. During this waiting period, the thread remains inactive and "does nothing else"^[600-developer__java__java-io__bio-nio-aio.md].
Non-blocking I/O¶
In a non-blocking I/O model, the system call returns immediately, typically indicating that the operation is not yet complete^[600-developer__java__java-io__bio-nio-aio.md]. This allows the thread to continue performing other tasks rather than being suspended, effectively meaning the thread "can do some other things" while the OS handles the I/O^[600-developer__java__java-io__bio-nio-aio.md].
Comparison with Synchronous/Asynchronous¶
While often discussed together, blocking/non-blocking is distinct from synchronous/asynchronous I/O^[600-developer__java__java-io__bio-nio-aio.md]. These concepts focus on the communication mechanism regarding the result, whereas blocking/non-blocking focus on the state of the waiting thread^[600-developer__java__java-io__bio-nio-aio.md].
- Synchronous: The caller actively participates in the I/O and waits for the result, either by blocking or by continuous polling^[600-developer__java__java-io__bio-nio-aio.md].
- Asynchronous: The caller does not actively wait for the result; instead, it relies on mechanisms such as status notifications or callback functions^[600-developer__java__java-io__bio-nio-aio.md].
Sources¶
^[600-developer__java__java-io__bio-nio-aio.md]