netty底层是事件驱动的异步库 但是可以await或者sync(本质是future超时机制)同步返回 但是官方 Prefer addListener(GenericFutureListener) to await()
Interface ChannelFuture
- All Superinterfaces:
- java.util.concurrent.Future<java.lang.Void>
- All Known Subinterfaces:
- ChannelProgressiveFuture, ChannelProgressivePromise, ChannelPromise
- All Known Implementing Classes:
- DefaultChannelProgressivePromise, DefaultChannelPromise
public interface ChannelFuture extends Future<java.lang.Void>
The result of an asynchronousChannel
I/O operation.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.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.+---------------------------+ | Completed successfully | +---------------------------+ +----> isDone() = true | +--------------------------+ | | isSuccess() = true | | Uncompleted | | +===========================+ +--------------------------+ | | Completed with failure | | isDone() = false | | +---------------------------+ | isSuccess() = false |----+----> isDone() = true | | isCancelled() = false | | | cause() = non-null | | cause() = null | | +===========================+ +--------------------------+ | | Completed by cancellation | | +---------------------------+ +----> isDone() = true | | isCancelled() = true | +---------------------------+
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 addChannelFutureListener
s so you can get notified when the I/O operation is completed.Prefer
It is recommended to preferaddListener(GenericFutureListener)
toawait()
addListener(GenericFutureListener)
toawait()
wherever possible to get notified when an I/O operation is done and to do any follow-up tasks.addListener(GenericFutureListener)
is non-blocking. It simply adds the specifiedChannelFutureListener
to theChannelFuture
, 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.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 withawait()
, 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.Do not call
await()
insideChannelHandler
The event handler methods in
ChannelHandler
are usually called by an I/O thread. Ifawait()
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 becauseawait()
can block the I/O operation it is waiting for, which is a dead lock.@Override ChannelHandlerContext ChannelFuture @Override ChannelHandlerContext ChannelFuture ChannelFutureListener ChannelFuture
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 callawait()
in an I/O thread. Otherwise,BlockingOperationException
will be raised to prevent a dead lock.Do not confuse I/O timeout and await timeout
The timeout value you specify withFuture.await(long)
,Future.await(long, TimeUnit)
,Future.awaitUninterruptibly(long)
, orFuture.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:Bootstrap ChannelFuture Bootstrap ChannelOption ChannelFuture
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2016-11-24 es-hadoop saveToEsWithMeta