This article mainly introduces the relevant information about the implementation of network IO in Java (BIO, NIO, AIO). Friends in need can refer to
In NetworkProgramming , the most exposed thing is the use of Socket for network communication development. In Java, there are mainly three implementation methods: BIO, NIO, and AIO.
I always seemed to understand the analysis of these three concepts, but the expression was not very clear. The following is a summary to fully clarify the analysis.
1. BIO method
First of all, I use a more popular language to explain:
BIO is blocking IO, each TCP When connecting to the server, you need to create a thread to establish the connection and process the message. If blocking occurs in the middle (for example, when establishing a connection, reading data, or writing data), the thread will also be blocked. In a concurrent situation, N connections require N threads to process.
The disadvantage of this method is that it is very inefficient under concurrent conditions.
The following is a diagram to illustrate the working situation of BIO
##2. NIO method
NIO was proposed by JDK1.4. Let me first explain the working principle of NIO in a simple way: NIO is non-blocking IO, which is based oneventdriverThe idea (Reactor thread model). Compared with BIO, NIO uses one thread to manage all Socket channels, which is based on the Selector mechanism. When an event is queried (connection, connection acceptance, reading, writing), it will be forwarded to different processing threads (handler) .
3. AIO method
AIO is proposed by JDK1.7, which is asynchronous IO. AIO adopts Proactor mode. The first thing we should analyze is the difference between AIO and NIO: (1) NIO notification occurs before Handler; (2) AIO notification is a callback that occurs after reading and writing, etc. When there is a notification, it means that the relevant operation has ended.
The above is the detailed content of Introduction to the implementation of network IO in Java (picture and text). For more information, please follow other related articles on the PHP Chinese website!