The IO methods in Java are divided into 3 types: BIO (Synchronous blocking), NIO (synchronous non-blocking) and AIO (asynchronous non-blocking). BIO
Synchronous blocking mode. Prior to JDK1.4, using Java to establish a network connection, you could only use the Bio method to start a serversocket on the server side, and then use accept to wait for the client to request, and for each request, a thread is used to process the user request. The thread spends most of its time waiting for requests to come in and IO operations, with very low utilization. And the threads are expensive and limited in number, so the number of connections that the server can handle is also low. NIO
In bio mode, it is "one socket to one thread", whereas in NiO, a single or small thread is used to poll the socket, and the thread is allocated for the request when a request is found on the socket. So it's "one request for one thread".
The implementation is the socket through the channel register to selector, using a thread in the selector polling, found that channel have read and write events, can be assigned to other threads to handle (usually using the thread pool). AIO
The AIO mode is supported from JDK7. The business logic is handled by registering the event callback function in Asynchronousserversocketchannel. When the IO operation is complete, the callback function is invoked. If you pass in Asynchronouschannelgroup, you can bind the thread pool to handle the event.
On the implementation of JDK, Windows platform based on IOCP implementation Aio,linux only Eppoll simulation implemented AIO.
Attachment: About IO often mentions reactor/proactor mode.
There are two roles in both modes: the event demultiplexer and the event handler Handler. The separator is responsible for monitoring IO events and notifies the processor that the processor is responsible for processing the IO content and completing the corresponding business.
The difference between the two is the example of a read operation (similar to a write).
read in reactor:
1. Register the Read-ready event and the corresponding event handler.
2. Event separator waits for an event.
3. The event arrives, activates the separator, the separator calls the corresponding processor.
4. The processor completes the IO read operation, processes the read data, registers the new event, then returns the control right.
read in Proactor:
1. Register the Read completion event and the corresponding event handler (including the buffer address).
2. When the event separator waits for the operation to complete the event, the operating system uses a parallel kernel thread to perform the actual read operation, and the resulting data into the user-defined buffer, and finally notifies the event separator that the read operation is complete.
3. The event separator calls the processor.
4. The event handler processes the data in the user's custom buffer, initiates a new asynchronous operation, and returns control to the event separator.
This shows the main difference between the two: in reactor, the user thread (the thread of the event handler) completes the IO read and write operation, whereas in Proactor, after the OS completes the IO read and write operation, the event handler is notified and the user thread completes only the business logic processing part of the data.