java foundation: 12, BIO, NIO, AIO What is the difference?

BIO, NIO, AIO What is the difference?

1, briefly

BIO: synchronous blocking IO. (Blocking IO)
NIO: synchronous non-blocking IO. (NonBlocking IO)
AIO: asynchronous non-blocking IO. (Asynchronous IO)

BIO suitable for a relatively small number of connections and fixed architecture, server resources in this way is relatively high, limited concurrent applications, the JDK1.4 previously only option, but the program intuitive and easy to understand.
NIO connection number suitable for the multi connection and short (light operation) architecture, such as chat server, limited concurrent applications, more complex programming, starts the JDK1.4 support.
AIO method is applicable to more than the number of connections and the connection is relatively long (heavy operation) architecture, such as the album server, call the OS to fully participate in concurrent operation, programming is more complex, JDK7 began to support.

Synchronous and asynchronous
synchronization: After initiating a call, before the caller requests untreated finished, the call does not return.
Asynchronous: caller does not need to wait for the results, the caller will be notified by the caller callback mechanisms that return results.

Blocking and non-blocking
blocking: initiating a request, the caller has been waiting for a request to return the results, that is, the current thread is suspended, unable to engage in other tasks only when the conditions are ready to continue.
Non-blocking: initiating a request, the caller has been waiting for the results do not return, you can go do other things.

2、BIO

BIO concept

BIO is the traditional IO, to achieve the most basic functions of network communication through traditional Socket. In the server side as an example, Socket basic process is as follows:
(. 1) Open: Open ServerSocket connector
(2) accept: accept the connection
(3) read: read data
(4) send: transmission data
(5) close: close the resource
client handshake with the server three times. Keywords: Socket and ServerSocket;
if the client does not initiate the connection request to the server, then accept will block. If the connection is successful, the data is not ready, a call to read will be blocked. Simultaneously handle multiple connections, you need to take a multi-threaded approach. Because each thread has its own stack space, blocking results in a large number of threads to context switch, reducing efficiency.

BIO mode of operation as shown below:
Here Insert Picture Description

BIO scenarios

BIO suitable for a relatively small number of connections and fixed architecture, server resources in this way is relatively high, limited concurrent applications, the JDK1.4 previously only option, but the program intuitive and easy to understand.

BIO code sample

? To be completed link

3、NIO

NIO concept

BIO is a stream-oriented, and is a cache NIO oriented. NIO to achieve non-blocking IO operations through the Channel, Selector, Buffer, using the Reactor design pattern.
NIO, the client and server are connected via Channel using polling registered Channel selector. Keywords: SocketChannel and a ServerSocketChannel;
Channel is a bidirectional non-blocking channel, the channel sides can read and write data. Interact with Buffer.
Selector similar to an observer. Polling access to all the Channel, to the data read and write operations. Implemented using a single thread access to a plurality of channels, no context switching between multiple threads, thus improving the efficiency of the selector.
Buffer is a buffer used to store data. In the read and write data, it is performed in the buffer. Any time access to data in the NIO, are operated by the buffer. Buffer has ByteBuffer, CharBuffer and so on.

NIO mode of operation as shown below:
Here Insert Picture Description

NIO application scenarios

NIO connection number suitable for the multi connection and short (light operation) architecture, such as chat server, limited concurrent applications, more complex programming, starts the JDK1.4 support.

NIO code sample

? To be completed link

4、AIO

AIO concept

Asynchronous non-blocking communication. It introduces the concept of asynchronous channel on the basis of the NIO.
AIO is event-based and callback mechanism, using asynchronous channel asynchronous communication. Keywords: AsynchronousSocketChannel and AsynchronousServerSocketChannel.

AIO scenarios

AIO method is applicable to more than the number of connections and the connection is relatively long (heavy operation) architecture, such as the album server, call the OS to fully participate in concurrent operation, programming is more complex, JDK7 began to support.

AIO code sample

? To be completed link

Published 57 original articles · won praise 13 · views 1128

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105053568