官方文档:
Interface ChannelFuture ------------------ Netty API Reference (4.0.54.Final)
The result of an asynchronous Channel I/O operation.
异步 Channel I/O操作执行后所得的结果
All I/O operations in Netty are asynchronous. It means any I/O calls will return immediately with no guarantee that the requested I/O operation has been completed at the end of the call. Instead, you will be returned with a ChannelFuture instance which gives you the information about the result or status of the I/O operation.
Netty的所有I/O操作都是异步的。这意味着任何I/O调用都会立即返回结果,不保证请求的I/O操作能执行到I/O函数的最后一条语句。相反,它会返回ChannelFuture实例告诉你此次I/O操作的结果和状态码。
A ChannelFuture is either uncompleted or completed. When an I/O operation begins, a new future object is created. The new future is uncompleted initially - it is neither succeeded, failed, nor cancelled because the I/O operation is not finished yet. If the I/O operation is finished either successfully, with failure, or by cancellation, the future is marked as completed with more specific information, such as the cause of the failure. Please note that even failure and cancellation belong to the completed state.
一个ChannelFuture只有两种状态:完成和未完成。当一个I/O操作开始执行,新的ChannelFuture对象就会被创建。future对象初始化为未完成---既不是成功或失败,也不是被取消,因为这个I/O操作还没有完成。如果I/O操作执行成功、或执行失败、或被取消,future实例被标记为完成,而且会返回特定的信息,如执行失败时返回失败信息。请注意即使是失败或被取消,future仍属于完成状态。
Various methods are provided to let you check if the I/O operation has been completed, wait for the completion, and retrieve the result of the I/O operation. It also allows you to add ChannelFutureListeners so you can get notified when the I/O operation is completed.
Netty提供了多种方法让你确认I/O是否已经完成,还是等待完成,还是返回了I/O操作的结果。同时,你可以添加监听者 ChannelFutureListener来告知你I/O已经完成。
Prefer addListener(GenericFutureListener) to await()
It is recommended to prefer addListener(GenericFutureListener) to await() wherever possible to get notified when an I/O operation is done and to do any follow-up tasks.
每当你需要被告知I/O操作已完成并想接着做点什么事,建议用addListener(GenericFutureListener) 而不要用await()。
addListener(GenericFutureListener) is non-blocking. It simply adds the specified ChannelFutureListener to the ChannelFuture, and I/O thread will notify the listeners when the I/O operation associated with the future is done. ChannelFutureListener yields the best performance and resource utilization because it does not block at all, but it could be tricky to implement a sequential logic if you are not used to event-driven programming.
addListener(GenericFutureListener) 是非阻塞的。它可以很简单地将ChannelFutureListener添加到ChannelFuture,然后I/O线程会提醒listener I/O完成。ChannelFutureListener有最好的性能和资源的充分利用,因为它是完全非阻塞的。但假如你不熟悉事件驱动编程,你反而可能觉得实现连续逻辑会比较困难。
By contrast, await() is a blocking operation. Once called, the caller thread blocks until the operation is done. It is easier to implement a sequential logic with await(), but the caller thread blocks unnecessarily until the I/O operation is done and there's relatively expensive cost of inter-thread notification. Moreover, there's a chance of dead lock in a particular circumstance, which is described below.
相比之下,await()是阻塞方法。一旦调用,调用者线程会一直阻塞,直到I/O操作完成。await()更容易实现连续逻辑,但它造成了I/O操作时不必要的线程阻塞,而且要实现线程间的通信需要很大的代价。再者,在特定情况下还会发生死锁,下文将会提到。
Do not call await() inside ChannelHandler
The event handler methods in ChannelHandler are usually called by an I/O thread. If await() is called by an event handler method, which is called by the I/O thread, the I/O operation it is waiting for might never complete because await() can block the I/O operation it is waiting for, which is a dead lock.
事件处理函数经常在ChannelHandler里面被I/O线程调用。如果await()被事件处理函数调用,事件处理函数又被I/O线程调用,那么它(事件处理函数)将一直等待永远不会完成的I/O操作。因为await()可以阻塞它(事件处理函数)一直等待的I/O操作,这就造成了死锁。
In spite of the disadvantages mentioned above, there are certainly the cases where it is more convenient to call await(). In such a case, please make sure you do not call await() in an I/O thread. Otherwise, BlockingOperationException will be raised to prevent a dead lock.
尽管有以上缺陷,但仍有某些特例调用await()会更便捷。在这种情况下,请确保你没有在I/O线程调用await()。否则将会抛出BlockingOperationException异常以防止死锁。
Do not confuse I/O timeout and await timeout
The timeout value you specify with Future.await(long), Future.await(long, TimeUnit), Future.awaitUninterruptibly(long), or Future.awaitUninterruptibly(long, TimeUnit) are not related with I/O timeout at all. If an I/O operation times out, the future will be marked as 'completed with failure,' as depicted in the diagram above. For example, connect timeout should be configured via a transport-specific option:
你用Future.await(long),Future.await(long, TimeUnit),Future.awaitUniterruptibly(long)或Future.awaitUninterruptibly(long, TimeUnit) 等方法指定timeoue的值,跟I/O的timeout没有一点关系。如果I/O操作超时,ChannelFuture会被记为"执行完毕,结果:失败",如前文第一张图所示。举个例子,连接超时应该通过传递特定参数来配置。