OIO vs NIO vs AIO: Mastering Sync/Async and Blocking/Non‑Blocking in Java
The article compares Java's OIO, NIO, and AIO models, explaining how each handles threads, channels, and I/O requests, and clarifies the distinctions between synchronous, asynchronous, blocking, and non‑blocking communication, while discussing thread pooling, selector behavior, and performance implications for massive connections.
In OIO (Old I/O), each thread can handle only one channel; the thread is bound to that channel. When a thread initiates an I/O request, it blocks until the operation finishes.
In NIO (New I/O), a thread can manage multiple channels asynchronously. The thread issues an I/O request and returns immediately; the kernel notifies the thread via a registered callback when the operation is ready, after which the thread blocks until completion.
In AIO (Asynchronous I/O), the thread also returns immediately after issuing the request. Once the I/O is prepared, the operation proceeds, and upon success or failure the kernel invokes the registered callback to inform the thread.
To handle a massive number of connections with OIO, each request is wrapped into a request object and assigned to a worker thread from a thread pool. If the pool is exhausted, incoming requests are queued. Any read/write operation blocks the serving thread.
In NIO, a channel must operate in non‑blocking mode; otherwise an exception is thrown. This is because the selector only notifies the program when data is available on the channel.
Synchronous and blocking are not identical concepts, just as asynchronous and non‑blocking differ. Blocking I/O wastes CPU cycles due to unnecessary context switches, especially when no data is read or written.
Synchronous blocking combines notification and read/write actions: the thread is notified only after data arrives, then it performs the I/O. Synchronous non‑blocking, asynchronous blocking, and asynchronous non‑blocking each have distinct behaviors, with asynchronous non‑blocking typically providing the highest efficiency by reducing thread switches.
In summary, “synchronous” aligns with “blocking,” while “asynchronous” aligns with “non‑blocking.” The terms describe communication mode (sync/async) versus the waiting behavior of send/receive operations (blocking/non‑blocking).
Communication sync means the client must wait for the server’s response before sending the next request; communication async allows the client to send subsequent requests without waiting.
Blocking and non‑blocking apply only to the read/write phase of a request. When both server and client are asynchronous, throughput is high; mixed sync/async configurations work but may reduce efficiency.
Key points:
Synchronous = blocking I/O.
Asynchronous = non‑blocking I/O.
Both concepts refer to local socket operations.
In synchronous mode, threads are typically required to handle I/O; in asynchronous mode, the call returns immediately and results are delivered via notifications.
首先是通信的同步,主要是指客户端在发送请求后,必须得在服务端有回应后才发送下一个请求。
所以这个时候的所有请求将会在服务端得到同步其次是通信的异步,指客户端在发送请求后,
不必等待服务端的回应就可以发送下一个请求,这样对于所有的请求动作来说将会在服务端得到异步,这条请求的链路就象是一个请求队列,所有的动作在这里不会得到同步的。Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
