Introduction to Java AIO

foreword

Starting from the JDK 7 version, the newly added file and network io feature of Java is called nio2 (new io 2, because there is already a NIO in jdk1.4), which contains many performance and functional improvements, the most important of which is The part is support for asynchronous io, called Java AIO (asynchronous IO).

Because the implementation of AIO needs to fully call the OS to participate, IO needs the support of the operating system, and concurrency also needs the support of the operating system, so the difference in performance between different operating systems will be more obvious.

 

BIO, NIO, AIO three IO processing models

BIO : that is, the traditional synchronous blocking IO. The server implementation mode is one connection and one thread, that is, when the client has a connection request, the server needs to start a thread for processing, even if the connection does nothing or is used very rarely. It will cause the thread overhead of the server. When the number of threads reaches the limit of the operating system, new connection requests may be delayed or rejected. Its read method blocks itself until there is data to read, so it is called synchronous blocking.

NIO : The synchronous non-blocking IO proposed by JDK1.4 , the server implementation mode is one request and one thread, the connection request sent by the client will be registered on the multiplexer, and the multiplexer polls to connect with I/O A thread is started for processing when requested. NIO is based on the Reactor mode, and its select bottom layer calls the select/poll model of the operating system (Linux2.6 uses epoll) to continuously poll until there is data to operate. This is actually a kind of synchronous IO. The IO operation will not be blocked, that is, non-blocking IO. In addition, under this model, it takes a certain time interval from notifying the application that there is data to read, to the read call to load the external data into the kernel space, and then copying it from the kernel space to the user space, which will also reduce the overall data throughput.

AIO : Asynchronous non-blocking IO proposed by JDK1.7 , the server implementation mode is one effective request for one thread, and the client's I/O request is completed by the OS first and then notifies the server application to start the thread for processing. Different from NIO, AIO is based on the Proactor mode. When performing read and write operations, it only needs to directly call the read or write methods of the API. Both methods are asynchronous. For read operations, when there is a stream to read, the operating system will pass the readable stream into the buffer of the read method and notify the application; for write operations, when When the operating system has finished writing the stream passed by the write method, the operating system will actively notify the application. In this case, the operating system will be responsible for transferring the readable data from the kernel space to the user space before actively notifying the application program. The application program not only does not need to constantly poll, but also the data reading process is more efficient. fast.

 

BIO, NIO, AIO applicable scenarios:

  • The BIO method is suitable for a structure with a relatively small number of connections and a fixed number of connections. This method has relatively high requirements on server resources, and concurrency is limited to applications. It is the only choice before JDK1.4, but the program is intuitive and easy to understand.
  • The NIO method is suitable for architectures with a large number of connections and relatively short connections (light operation), such as chat servers, where concurrency is limited to applications, and programming is more complicated. JDK1.4 began to support it.
  • The AIO method is used for architectures with a large number of connections and relatively long connections (heavy operations), such as photo album servers, which fully call the OS to participate in concurrent operations, and the programming is more complicated. JDK7 began to support

 

AIO implementation principle

We know that after Linux 2.6, the implementation of java NIO, Linux is implemented through epoll, and Windows is implemented through less efficient select/poll, which can be found through the source code of jdk. For AIO, it is implemented through IOCP on Windows, and through epoll on Linux.

The new APIs of AIO are mainly as follows:

  • AsynchronousSocketChannel
  • AsynchronousServerSocketChannel
  • AsynchronousFileChannel
  • AsynchronousDatagramChannel
  • CompletionHandler

异步channel API提供了两种方式监控/控制异步操作(connect,accept, read,write等)。第一种方式是返回java.util.concurrent.Future对象, 检查Future的状态可以得到操作是否完成还是失败,还是进行中, future.get阻塞当前进程。
第二种方式为操作提供一个回调参数java.nio.channels.CompletionHandler,这个回调类包含completed,failed两个方法。
channel的每个I/O操作都为这两种方式提供了相应的方法, 你可以根据自己的需要选择合适的方式编程。

 

参考文章:http://www.ibm.com/developerworks/cn/linux/l-async/

 

Guess you like

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