Abstract: Understanding of BIO and NIO
Recently, I probably looked at the source code of ZooKeeper and Mina and found that they are both implemented in Java NIO, so it is necessary to figure out what NIO is. The following is my own summary based on online information, in order to save time, I drew the diagram casually, as long as I can achieve the meaning.
Introduction:
BIO: Synchronous blocking IO, the server implementation mode is to connect one thread at a time, that is, when the client has a connection request, the server needs to start a thread for processing, if this connection does not do anything will cause unnecessary thread overhead, of course, it can be improved through the thread pool mechanism.
NIO: Synchronous non-blocking IO, the server implementation mode is one request per thread, that is, the connection request sent by the client will be registered on the multiplexer, and the multiplexer will start a thread for processing when the connection has an I/O request.
AIO (NIO.2): Asynchronous non-blocking IO, the server implementation mode is to effectively request one thread, and the client's I/O requests are completed by the OS first and then notify the server application to start the thread for processing.
BIO
Synchronous blocking IO, I believe everyone who has learned operating system network programming or any language network programming is familiar, in the while loop the server will call the accept method to wait for the connection request of the receiving client, once a connection request is received, a communication socket can be established on this communication socket for read and write operations, at this time it can no longer receive other client connection requests, can only wait for the execution of the operation with the currently connected client.
If BIO wants to be able to process multiple client requests at the same time, it must use multi-threading, that is, every time accept blocks wait for a client request, once a connection request is received, a communication socket is established and a new thread is opened to process the data read and write request of this socket, and then immediately continue to accept and wait for other client connection requests, that is, a thread is created for each client connection request to be processed individually, the schematic is probably like this:
C:/Users/kevin/AppData/Local/YNote/data/kevinsir2003@163.com/8107c3f773ad4d2aa1a5a476e650ef84/094528_zqyy.jpeg
Although the server has high concurrency at this time, that is, it can handle multiple client requests at the same time, it brings a problem, as the number of open threads increases, it will consume too much memory resources, causing the server to slow down or even crash, and NIO can solve this problem to a certain extent.
NIO
The key to synchronous non-blocking IO is to adopt an event-driven idea to implement a multiplexer.
The biggest difference between NIO and BIO is that you only need to open a thread to handle IO events from multiple clients.
It is a multiplexer that can listen to IO events from multiple clients:
A. If the server listens to the client connection request, it will establish a communication socket for it (channel in Java), and then return to continue listening.
B. If the server listens to the data sent from the client that has created a communication socket, it will call the corresponding interface to process the received data, and if there are multiple clients at the same time, the data can also be processed in turn.
C. Listen to multiple clients' connection requests and receive data requests, and also listen to when you have data to send.
C:/Users/kevin/AppData/Local/YNote/data/kevinsir2003@163.com/41709898aa0a4f8a830d7c348ed05fbb/094528_of9c.jpeg
In short, in one thread, you can call the multiplexing interface (select in java) to block and listen for IO requests from multiple clients at the same time, and once an IO request is received, the corresponding function will be called to process it.
respective application scenarios
By this point, you may have noticed that once a request arrives (whether it is several at the same time or only one), the corresponding IO processing function will be called to handle it, so:
(1) NIO is suitable for handling scenarios with a large number of connections, but the connections are relatively short (light operation), such as Jetty, Mina, ZooKeeper, etc., which are all implemented based on java nio.
(2) The BIO method is suitable for scenarios where the number of connections is relatively small and fixed, which requires high server resources and is limited to applications.
|