Differences and application scenarios of BIO, NIO, AIO in Java

 To learn IO, you must first understand four things.

1. Synchronization

           Java handles io itself.

2. Asynchronous

         Java hands the io to the operating system for processing, tells the size of the buffer area, and completes the processing callback.

3. Block

         When using blocking IO, the Java call will block until the read and write is completed before returning.

4. Non-blocking

       When using non-blocking IO, if you can't read and write immediately, the Java call will return immediately. When the IO event dispatcher notifies that it can read and write, it is reading and writing, and continues to loop until reading and writing are completed.

 

1.BIO: Synchronization and blocking. The server's implementation mode is to connect one thread. One obvious defect of this mode is: Since the number of client connections is proportional to the number of server threads, it may cause unnecessary thread overhead and serious Will also cause server memory to overflow. Of course, this situation can be improved by the thread pool mechanism, but it does not eliminate this drawback in essence.

2.NIO: Before JDK1.4, Java's IO model has been BIO, but from JDK1.4, the new IO model NIO introduced by JDK, it is synchronous non-blocking. The server's implementation mode is multiple requests for one thread, that is, the request will be registered on the multiplexer Selector, and the multiplexer will start a thread to process when it polls for an IO request.

3. AIO: JDK1.7 released NIO2.0, which is truly asynchronous and non-blocking. The server's implementation mode is multiple effective requests for one thread. The client's IO requests are completed by the OS first and then the server application is notified. Start thread processing (callback).

Application scenario: BIO is used when the number of concurrent connections is not large, because it is very simple to program and debug, but if high concurrency is involved, NIO or AIO should be selected. The better recommendation is to use the mature network communication framework Netty.

Guess you like

Origin www.cnblogs.com/bill89/p/12684731.html