Interface ChannelFuture

官方文档:

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会被记为"执行完毕,结果:失败",如前文第一张图所示。举个例子,连接超时应该通过传递特定参数来配置。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,133评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,682评论 3 390
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,784评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,508评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,603评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,607评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,604评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,359评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,805评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,121评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,280评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,959评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,588评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,206评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,442评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,193评论 2 367
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,144评论 2 352

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,460评论 0 23
  • 怀念姥姥。牙牙学语时,我叫她yao yao. 水洗捶打过的青布衣裳,柔软的妥帖的,斜襟,旧而肃穆,整整齐齐的晒在晾...
    阿眉住在石榴树上阅读 543评论 4 2
  • 9.稀缺
    尘世书童阅读 167评论 0 0
  • 我们明天将登陆罗瑟拉站。上世纪五十年代,英国在南极半岛建立二十来个基地,如今只剩下罗斯冰架上的哈雷站和玛格...
    christiewho1阅读 488评论 2 2