Understanding Synchronous vs Asynchronous, Blocking vs Non-Blocking, and Java I/O Models (BIO, NIO, IO Multiplexing, AIO)
This article explains the concepts of synchronous and asynchronous communication, blocking and non‑blocking thread behavior, and compares Java I/O models—including BIO, NIO, IO multiplexing, and AIO—highlighting their mechanisms, advantages, and typical use cases.
The article introduces the complex topics of synchronous vs. asynchronous communication, blocking vs. non‑blocking execution, and various Java I/O models, aiming to present them in simple, understandable language.
1. Synchronous and Asynchronous
同步 means a request does not return until the result is obtained; the caller waits for completion.
异步 means a request returns immediately with no result; the caller can perform other work and later check the status or receive a callback.
Examples are given using a washing machine analogy.
2. Blocking and Non‑Blocking
阻塞 indicates that the current thread is suspended (blocked) while waiting for a result, doing nothing else.
非阻塞 indicates that the thread continues to run and can handle other tasks while the request is pending.
The article explains how blocking relates to thread states such as BLOCKED and RUNNABLE , and how synchronous communication typically keeps the thread in RUNNABLE until the result arrives, whereas asynchronous communication allows the thread to proceed.
3. I/O Models
Java I/O operations involve two stages: waiting for data to become ready and copying data from the kernel buffer to the process address space. Based on these stages, several I/O models exist:
BIO (Blocking I/O)
NIO (Non‑Blocking I/O)
IO Multiplexing (select, poll, epoll)
AIO (Asynchronous I/O)
3.1 BIO
In BIO, the thread is blocked during both stages of the I/O operation; the request is sent to the kernel and the thread remains suspended until the kernel returns data.
3.2 NIO
NIO introduces a non‑blocking first stage where the thread repeatedly checks whether data is ready (a form of polling), followed by a blocking second stage to read the data.
3.3 IO Multiplexing
IO multiplexing allows a single thread to monitor multiple sockets using mechanisms such as select , poll , and epoll . The thread blocks only until at least one descriptor becomes ready, enabling concurrent handling of many connections.
3.4 AIO
AIO, introduced in Java 1.7 as part of NIO 2.0, is fully asynchronous: after issuing a system call, the user thread can continue working. The OS notifies the application when data is ready. Java provides two ways to handle the result:
Implement a CompletionHandler callback.
Use a Future object, checking isDone() and retrieving the result with get() .
3.5 Summary
BIO, NIO, and IO multiplexing all involve a blocking wait in the second stage and are therefore considered synchronous models, while AIO performs both stages without waiting and is classified as asynchronous.
Readers are invited to leave comments for further discussion.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.