Before you get to know Bio,nio,aio, let's look at several concepts of IO:
1. Synchronization
The user process triggers an IO operation and waits or polls to see if the IO operation is ready, for example by taking a bank card to withdraw money from the bank.
2. Asynchronous
After the user triggers the IO operation, you can do something else, and then notify the current thread after the IO operation is complete, such as letting the younger brother go to the bank to help you get the money, you can do something else.
3. Blocking
When trying to read and write files, found unreadable or nothing to read, then enter the waiting state to know the readable, ATM queue to take money
4. Non-blocking
When the user process accesses the data, it will return a status value (readable unreadable), such as in the bank counter business, first take a number, and then sit in the chair to do other things, you can keep asking the lobby manager is not available, the lobby manager if it is not up to you can not go until the broadcast notify you to Transact, If you cannot read and write the Java call will return immediately, when the IO event dispatcher will notify the read and write to continue to read and write, continuously loop until read-write completed.
Synchronous blocking (JAVA BIO), each socket socket needs to be processed using a single thread. It is possible to block when establishing a connection and reading and writing operations. On the server side, if you want to support concurrent connections, you need more threads. Connecting without doing anything creates unnecessary thread overhead that can be improved through the thread pool.
NIO (synchronous non-blocking) based on event-driven (in the program, the program stops there, you click a button, it reacts, after a while, and no response, you click again, it continues to run), using the reactor mode, (reactor mode is first Event-driven, there is one or more concurrent input sources, there is a service Handler, there are multiple request handlers; this service Handler will synchronously distribute the input request (Event) multiplexing to the corresponding request Handler. Reactor handles all client socket sockets and distributes them to different threads. This solves the need for more threads in bio to support more socket sockets.
AIO (asynchronous non-blocking) AIO uses the Proactor mode, the difference between AIO and NiO is that when AIO in the read and write operations, do not have to wait for notification, you can directly call the corresponding Read/write method, both of which are asynchronous, for the read operation, when there is a stream to read, The operating system will stream readable into the buffer of the Read method and notify the application, and for a write operation, when the operating system writes the Write method to the completion of the flow, the operating system actively notifies the application, and NIO's notification occurs before the action, when it is readable and writable, Selector has discovered these events after calling handler processing
Bio,nio,aio in Java