This article is not intended to explain how to use bio? How does NiO work? This paper focuses on the underlying principle of NIO. This article intends to explain from the following aspects: 1, Bio communication model (network aspect) is what? 2. What is the NIO communication model (network aspect)? What problems have been solved? 1. Bio Communication Model Explanation: In the bio scenario, the client initiates a connection request, and after the server receives the request, it assigns a business thread to process the access, performs the business processing, and writes the response stream. Both the server and the client are blocking the reading and writing of the data. This process can be a waste of CPU resources. Also, as user requests increase, the blocking queue is full, and the application thread is not released, resulting in subsequent requests being discarded and not processed. Why does this happen? The reason for this is that the server performs the read data operation, essentially the CPU sends an instruction to the operating system kernel, letting the operating system read the data from the network to the kernel, from the kernel to the memory through the TCP/IP protocol. CPU execution instructions are very fast, and the operating system performs IO much faster than the CPU execution instructions, which can lead to wasted CPU time. 2.nio Communication Model Explanation: In the NIO scenario, the client initiates the request, and after the server receives the request, it does not directly assign the business thread to process the request, but instead to the dedicated IO thread (selector in JAVA) to read the request stream. When the data is ready, it is handed over to the business thread to execute the business logic and finally to the IO thread to write the response stream. Here, the reader may have two questions? is the IO thread a bottleneck in the NIO model? What problems does NiO solve (compared to bio)? Will IO threads become a bottleneck? This problem has to be said from the bottom of the IO thread implementation, NIO is synchronous non-blocking, because the underlying operating system supports synchronous non-blocking, the JVM only through the system calls the local method to achieve synchronous non-blocking (essentially, the operating system to implement synchronous non-blocking, and the JVM only through the local method to execute system calls). Linux systems provide Epoll system calls, and Epoll is implemented in an event-driven manner (that is, the underlying operating system prepares the data for event-driven callback notifications), and the selector Select () method call in NiO, is to implement non-blocking through local calls to Epoll system calls to maximize CPU time slices, so the bottleneck of the IO thread is the hardware bottleneck. What problems does NiO solve? Through a separate IO thread, when there is a readable, writableevent occurs when you do the read and write operations, this time do not have to be like bio-blocking wait there, business threads can be released to do more things. To put it bluntly, it improves CPU utilization and allows fewer threads to do more.
Anatomy of the JAVA NiO principle