Guide to Java NIO2 Asynchronous Channel APIs – Java NIO2异步通道API指南

最后修改: 2016年 12月 1日


1. Overview


In this article, we are going to explore the foundations of one of the key additional APIs of the new I/O (NIO2) in Java 7- asynchronous channel APIs.

在这篇文章中,我们将探讨Java 7中新I/O(NIO2)的一个关键附加API的基础–异步通道API

This is first in a series of articles that will cover this particular topic.


The asynchronous channel APIs are an enhancement to the earlier new I/O (NIO) APIs that shipped with Java 1.4. To read about NIO Selectors, please follow this link.

异步通道 API 是对早期随 Java 1.4 出厂的新 I/O (NIO) API 的增强。要阅读有关NIO选择器的内容,请遵循这个链接

Another enhancement to the NIO APIs is the new File System API. You can read more about its file operations and path operations on this site too.

NIO API的另一项改进是新的文件系统API。你也可以在这个网站上阅读关于其文件操作路径操作的更多信息。

To use the NIO2 asynchronous channels in our projects, we have to import the java.nio.channels package as the required classes are bundled in it:

为了在我们的项目中使用NIO2异步通道,我们必须导入 java.nio.channels包,因为所需的类都捆绑在里面。

import java.nio.channels.*;

2. How Asynchronous Channel APIs Work


The asynchronous channel APIs were introduced into the existing java.nio.channels package, simply put – by prefixing the class names with the word Asynchronous.


Some of the core classes include: AsynchronousSocketChannel, AsynchronousServerSocketChannel and AsynchronousFileChannel.

一些核心类包括。AsynchronousSocketChannel, AsynchronousServerSocketChannelAsynchronousFileChannel

As you may have noticed, these classes are similar in style to the standard NIO channel APIs.


And, most API operations available to the NIO channel classes are also available in the new asynchronous versions. The main difference is that the new channels enable some operations to be executed asynchronously.


When an operation is initiated, the asynchronous channel APIs provide us with two alternatives for monitoring and controlling the pending operations. The operation can return java.util.concurrent.Future object or we can pass to it a java.nio.channels.CompletionHandler.


3. The Future Approach


A Future object represents a result of an asynchronous computation. Assuming we want to create a server to listen to client connections, we call the static open API on the AsynchronousServerSocketChannel and optionally bind the returned socket channel to an address:

一个Future对象代表一个异步计算的结果。假设我们想创建一个服务器来监听客户端连接,我们在AsynchronousServerSocketChannel上调用静态的open API,并选择将返回的socket通道绑定到一个地址。

AsynchronousServerSocketChannel server 

We have passed in null so that the system can auto-assign an address. Then, we call the accept method on the returned server SocketChannel:


Future<AsynchronousSocketChannel> future = server.accept();

When we call the accept method of a ServerSocketChannel in the old IO, it blocks until an incoming connection is received from a client. But the accept method of an AsynchronousServerSocketChannel returns a Future object right away.


The generic type of the Future object is the return type of the operation. In our case above, it is AsynchronousSocketChannel but it could just as well have been Integer or String, depending on the ultimate return type of the operation.


We can use the Future object to query the state of the operation:



This API returns true if the underlying operation already completed. Note that completion, in this case, may mean normal termination, an exception, or cancellation.


We can also explicitly check if the operation has been canceled:



It only returns true if the operation was canceled before completing normally, otherwise, it returns false. Cancellation is performed by the cancel method:



The call cancels the operation represented by the Future object. The parameter indicates that even if the operation has started, it can be interrupted. Once an operation has completed, it cannot be canceled


To retrieve the result of a computation, we use the get method:


AsynchronousSocketChannel client= future.get();

If we call this API before the operation completes, it will block until completion and then return the result of the operation.


4. The CompletionHandler Approach


The alternative to using Future to handle operations is a callback mechanism using the CompletionHandler class. The asynchronous channels allow a completion handler to be specified to consume the result of an operation:


AsynchronousServerSocketChannel listener

  attachment, new CompletionHandler<AsynchronousSocketChannel, Object>() {
    public void completed(
      AsynchronousSocketChannel client, Object attachment) {
          // do whatever with client
    public void failed(Throwable exc, Object attachment) {
          // handle failure

The completed callback API is invoked when the I/O operation completes successfully. The failed callback is invoked if the operation has failed.

当I/O操作成功完成时,completedcallback API被调用。如果操作失败,failed回调会被调用。

These callback methods accept other parameters – to allow us to pass in any data we think may be suitable to tag along with the operation. This first parameter is available as the second parameter of the callback method.


Finally, a clear scenario is – using the same CompletionHandler for different asynchronous operations. In this case, we’d benefit from tagging each operation to provide context when handling the results we will see this in action in the following section.


5. Conclusion


In this article, we have explored introductory aspects of the Asynchronous Channel APIs of Java NIO2.

在这篇文章中,我们已经探讨了Java NIO2的异步通道API的介绍性内容。

To get all code snippets and the full source code for this article, you can visit the GitHub project.