BIO NIO AIO

BIO: synchronous blocking IO, one connection and one thread

NIO: synchronous non-blocking, one thread per request

AIO: Asynchronous non-blocking, one thread per valid request

 

BIO:

Before java 1.4, a network request was required to establish a socket. At this time, a socket connection was required to ask if there was a thread to handle it. If not, wait or reject it. A connection requires the server to be processed by one thread.

 

NIO

It is completed based on the event-driven idea to solve the large concurrency problem of BIO: in the network application of synchronous IO, if you want to process multiple client requests at the same time, or if the client wants to communicate with multiple servers at the same time, you must use multithreading to process. Each client's connection request requires a thread to handle it separately.

Question: Every time a thread is created, a certain amount of memory space (provided by the JVM) must be allocated for the thread. At the same time, the operating system also has an upper limit on the total number of threads. Requesting too many server programs may be overwhelmed and refuse client requests to crash or crash.

NIO is based on Reactor. When the socket has a stream to read or write, the operating system will notify the application to process it, and the application will read the stream to the buffer or write to the operating system.

That is to say, at this time, it is no longer a socket corresponding to a thread, but a request corresponding to a thread. When the connection has no data, there is no thread to process it.

 

AIO

Unlike NIO, when performing read and write operations, you only need to directly call the read and write methods of the API, both of which are asynchronous.

For read operations, when there is a stream read operation, the operating system will pass the stream into the buffer of the read method to notify the application program of the write operation. When the operating system has finished writing the stream passed by the write method, the operating system will actively notify the application program.

It can be understood that both read and write are asynchronous, and the callback function will be actively called after completion.

In java1.7, there are four asynchronous channels under the java.net.channels package

AsynchronousSocketChannel

AsynchronousServerSocketChannel

AsynchronousFileChannel

AsynchronousDataframChannel callback function.

Calling the read/write method will return the calling function object at the same time. When the execution is completed, it will be called immediately.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705496&siteId=291194637