Interface ChannelFuture

官方文檔:

Interface ChannelFuture ------------------ Netty API Reference (4.0.54.Final)

The result of an asynchronous Channel I/O operation.

異步 Channel I/O操作執(zhí)行后所得的結果

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調(diào)用都會立即返回結果,不保證請求的I/O操作能執(zhí)行到I/O函數(shù)的最后一條語句。相反,它會返回ChannelFuture實例告訴你此次I/O操作的結果和狀態(tài)碼。

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只有兩種狀態(tài):完成和未完成。當一個I/O操作開始執(zhí)行,新的ChannelFuture對象就會被創(chuàng)建俄占。future對象初始化為未完成---既不是成功或失敗,也不是被取消剥汤,因為這個I/O操作還沒有完成颠放。如果I/O操作執(zhí)行成功、或執(zhí)行失敗吭敢、或被取消碰凶,future實例被標記為完成,而且會返回特定的信息鹿驼,如執(zhí)行失敗時返回失敗信息欲低。請注意即使是失敗或被取消,future仍屬于完成狀態(tài)畜晰。

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是否已經(jīng)完成砾莱,還是等待完成,還是返回了I/O操作的結果凄鼻。同時腊瑟,你可以添加監(jiān)聽者 ChannelFutureListener來告知你I/O已經(jīng)完成聚假。

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有最好的性能和資源的充分利用瘪贱,因為它是完全非阻塞的。但假如你不熟悉事件驅(qū)動編程辆毡,你反而可能覺得實現(xiàn)連續(xù)邏輯會比較困難菜秦。

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()是阻塞方法舶掖。一旦調(diào)用球昨,調(diào)用者線程會一直阻塞,直到I/O操作完成眨攘。await()更容易實現(xiàn)連續(xù)邏輯褪尝,但它造成了I/O操作時不必要的線程阻塞,而且要實現(xiàn)線程間的通信需要很大的代價期犬。再者,在特定情況下還會發(fā)生死鎖避诽,下文將會提到龟虎。

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.

事件處理函數(shù)經(jīng)常在ChannelHandler里面被I/O線程調(diào)用。如果await()被事件處理函數(shù)調(diào)用沙庐,事件處理函數(shù)又被I/O線程調(diào)用鲤妥,那么它(事件處理函數(shù))將一直等待永遠不會完成的I/O操作。因為await()可以阻塞它(事件處理函數(shù))一直等待的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.

盡管有以上缺陷,但仍有某些特例調(diào)用await()會更便捷铸抑。在這種情況下贡耽,請確保你沒有在I/O線程調(dià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)蒲赂,F(xiàn)uture.await(long, TimeUnit),F(xiàn)uture.awaitUniterruptibly(long)或Future.awaitUninterruptibly(long, TimeUnit) 等方法指定timeoue的值刁憋,跟I/O的timeout沒有一點關系滥嘴。如果I/O操作超時,ChannelFuture會被記為"執(zhí)行完畢至耻,結果:失敗"若皱,如前文第一張圖所示镊叁。舉個例子,連接超時應該通過傳遞特定參數(shù)來配置走触。

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末晦譬,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子饺汹,更是在濱河造成了極大的恐慌蛔添,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兜辞,死亡現(xiàn)場離奇詭異迎瞧,居然都是意外死亡,警方通過查閱死者的電腦和手機逸吵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進店門凶硅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人扫皱,你說我怎么就攤上這事足绅⌒鸱玻” “怎么了壳贪?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長嚷掠。 經(jīng)常有香客問我段多,道長首量,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任进苍,我火速辦了婚禮加缘,結果婚禮上,老公的妹妹穿的比我還像新娘觉啊。我一直安慰自己拣宏,他們只是感情好,可當我...
    茶點故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布杠人。 她就那樣靜靜地躺著勋乾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪嗡善。 梳的紋絲不亂的頭發(fā)上市俊,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天,我揣著相機與錄音滤奈,去河邊找鬼摆昧。 笑死,一個胖子當著我的面吹牛蜒程,可吹牛的內(nèi)容都是我干的绅你。 我是一名探鬼主播伺帘,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼忌锯!你這毒婦竟也來了伪嫁?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤偶垮,失蹤者是張志新(化名)和其女友劉穎张咳,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體似舵,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡脚猾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了砚哗。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片龙助。...
    茶點故事閱讀 38,039評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蛛芥,靈堂內(nèi)的尸體忽然破棺而出提鸟,到底是詐尸還是另有隱情,我是刑警寧澤仅淑,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布称勋,位于F島的核電站,受9級特大地震影響涯竟,放射性物質(zhì)發(fā)生泄漏铣缠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一昆禽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蝇庭,春花似錦醉鳖、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至北发,卻和暖如春纹因,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背琳拨。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工瞭恰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人狱庇。 一個月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓惊畏,卻偏偏與公主長得像恶耽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子颜启,可洞房花燭夜當晚...
    茶點故事閱讀 42,786評論 2 345

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,389評論 0 23
  • 懷念姥姥偷俭。牙牙學語時,我叫她yao yao. 水洗捶打過的青布衣裳缰盏,柔軟的妥帖的涌萤,斜襟,舊而肅穆口猜,整整齊齊的曬在晾...
    阿眉住在石榴樹上閱讀 538評論 4 2
  • 9.稀缺
    塵世書童閱讀 166評論 0 0
  • 我們明天將登陸羅瑟拉站负溪。上世紀五十年代,英國在南極半島建立二十來個基地暮的,如今只剩下羅斯冰架上的哈雷站和瑪格...
    christiewho1閱讀 482評論 2 2