CompletableFuture相關API

本文介紹相關API,使用上的介紹參見CompletableFuture 示例鞠柄。
CompletableFuture是 jdk 8里面的新的API嫉柴,類似于google guava的 ListenableFutureSettableFuture计螺,提供了對future結果的監(jiān)聽(在future到達某個狀態(tài)時執(zhí)行指定action)和自行設置future的狀態(tài)(狀態(tài)只能設置一次,但也有強制設值的接口)的能力危尿,正常結束complete, 或者異常結束completeExceptionally谊娇。listener的能力由CompletionStage接口聲明罗晕, settable能力由CompletableFuture定義赠堵。CompletableFuture實現(xiàn)了CompletionStage接口。

1. 對CompletableFuture的監(jiān)聽的API

  這類API基本都提供了同步酬屉、異步兩類接口揍愁。同步接口是在指定future結束時,使用當時thread執(zhí)行回調函數(shù)(action)谬擦;異步接口是在指定future接收時朽缎,使用額外的executor執(zhí)行回調函數(shù)。異步接口又有兩個北秽,一個是使用默認executor(default asynchronous execution facility,據(jù)觀察使用的是ForkJoinPool)執(zhí)行異步最筒,一個是使用指定executor執(zhí)行異步。

  • thenApply
  • thenAccept
  • thenRun
  • handle(入?yún)⑹荈unction)
  • exceptionally(入?yún)⑹荈unction)
  • whenComplete(入?yún)⑹荂onsumer)
/**
 * 當future正常返回掠归、異常結束時動作悄泥, 入?yún)⑹荁iFunction
 */
public <U> CompletionStage<U> handle (BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync (BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync (BiFunction<? super T, Throwable, ? extends U> fn, Executor executor);

/**
 * 當future正常返回時才會動作弹囚, 入?yún)⑹荈unction
 */
public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync (Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync (Function<? super T,? extends U> fn, Executor executor);

/**
 * 同thenApply, 不過入?yún)⑹荂onsumer鸥鹉, 返回值也變成了CompletionStage<Void>
 */
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor);

/**
 * 當future正常返回時毁渗,執(zhí)行動作Runnable,不需要future的結果
 */
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action, Executor executor);

/**
 * 當future正常返回、異常結束時府适,執(zhí)行動作, 將future的結果、異常傳入BiConsumer逻淌, 當future為正常返回時疟暖,exception是null,否則 result是null
 */
public CompletionStage<T> whenComplete (BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync (BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync (BiConsumer<? super T, ? super Throwable> action, Executor executor);

/**
 * 當future異常結束時,執(zhí)行動作, 將future的異常傳入Function
 */
public CompletionStage<T> exceptionally (Function<Throwable, ? extends T> fn);

2. 與其他CompletableFuture的協(xié)同的API

協(xié)同一詞不知道描述是否恰當骨望,這類API提供了當前CompletableFuture與兩外一個CompletableFuture組合的能力窜骄,比如兩個Future都正常返回的動作摆屯;兩個future任意一個正常返回;

  • thenCombine
  • thenAcceptBoth
  • runAfterBoth
  • applyToEither
  • acceptEither
  • allOf
  • anyOf
/**
 * 當this, other都正常返回時准验,執(zhí)行動作
 */
public <U,V> CompletionStage<V> thenCombine (CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync (CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync (CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor);

/**
 * 同#thenCombine糊饱, 不過入?yún)⑹荁iConsumer
 */
public <U> CompletionStage<Void> thenAcceptBoth (CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action); public <U> CompletionStage<Void> thenAcceptBothAsync (CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync (CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor);

/**
 * this, other都正常返回之后執(zhí)行action, 類似于#thenRun
 */
public CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor);

/**
 * this, other任意一個正常返回,執(zhí)行Funcion, 不同于#thenCombine, other的類型是兼容this的另锋,而thenCombine不需要兼容狭归,而且thenCombine的action是BiFunction類型
 */
public <U> CompletionStage<U> applyToEither (CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync (CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync (CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor);

/**
 * 同#applyToEither, 不過入?yún)⑹荂onsumer
 */
public CompletionStage<Void> acceptEither (CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync (CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync (CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor);

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) ;
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)室梅;

3.CompletableFuture類似functional的API

  • thenCompose 類似于Function.andThen疚宇,組合多個future成鏈式調用。當前future正常返回后间涵, fn使用該結果正常繼續(xù)處理榜揖,并返回CompletableFuture股耽;與上面監(jiān)聽API不同的是钳幅,入?yún)⒌膄n返回類型是CompletableFuture/** * 當this正常返回之后敢艰, fn使用this future的結果繼續(xù)處理,并返回一個新的CompletableFuture */ public <U> CompletionStage<U> thenCompose (Function<? super T, ? extends CompletionStage<U>> fn); public <U> CompletionStage<U> thenComposeAsync (Function<? super T, ? extends CompletionStage<U>> fn); public <U> CompletionStage<U> thenComposeAsync (Function<? super T, ? extends CompletionStage<U>> fn, Executor executor);

4. 設值相關API

對CompletableFuture設值震嫉,是由CompletableFuture生命并實現(xiàn)牡属,并不是CompleteStage的接口。主要有兩類正常結果設值逮栅,異常設值。還有一些靜態(tài)輔助方法設值措伐。

  • complete
  • completeExceptionally
/**
 * 設值正常結果侥加,且this future之前未設值,get一類的方法返回指定的value担败。如果有監(jiān)聽動作,則相關監(jiān)聽動作觸發(fā)
 */
public boolean complete(T value);

/**
 * 設值正常結果吗货,且this future之前未設值岖研,get一類的方法拋出異常。如果有監(jiān)聽動作害淤,則相關監(jiān)聽動作觸發(fā)
 */
public boolean completeExceptionally(Throwable ex);

/**
 * 靜態(tài)方法拓售,返回一個已經(jīng)被設值的future
 */
public static <U> CompletableFuture<U> completedFuture(U value);

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末础淤,一起剝皮案震驚了整個濱河市哨苛,隨后出現(xiàn)的幾起案子币砂,更是在濱河造成了極大的恐慌,老刑警劉巖亿蒸,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掌桩,死亡現(xiàn)場離奇詭異,居然都是意外死亡茅坛,警方通過查閱死者的電腦和手機则拷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門隔躲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來物延,“玉大人,你說我怎么就攤上這事浑吟『牧铮” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵燎字,是天一觀的道長阿宅。 經(jīng)常有香客問我,道長蛉鹿,這世上最難降的妖魔是什么往湿? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮他膳,結果婚禮上,老公的妹妹穿的比我還像新娘龄句。我一直安慰自己散罕,他們只是感情好,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布职抡。 她就那樣靜靜地躺著误甚,像睡著了一般窑邦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上冈钦,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天瞧筛,我揣著相機與錄音,去河邊找鬼较幌。 笑死乍炉,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的岛琼。 我是一名探鬼主播衷恭,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼灭袁!你這毒婦竟也來了?” 一聲冷哼從身側響起茸歧,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤软瞎,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鳖藕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體只锭,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蜻展,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了伍茄。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片施逾。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡音念,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闷愤,到底是詐尸還是另有隱情件余,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布旬渠,位于F島的核電站端壳,受9級特大地震影響损谦,放射性物質發(fā)生泄漏岳颇。R本人自食惡果不足惜颅湘,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瞻鹏。 院中可真熱鬧鹿寨,春花似錦、人聲如沸叭披。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽同诫。三九已至樟澜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間霹俺,已是汗流浹背毒费。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留想际,地道東北人溪厘。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像侧甫,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子披粟,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

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

  • 在現(xiàn)代軟件開發(fā)中虫碉,系統(tǒng)功能越來越復雜,管理復雜度的方法就是分而治之敦捧,系統(tǒng)的很多功能可能會被切分為小的服務碰镜,對外提供...
    天堂鳥6閱讀 7,152評論 0 23
  • Java 8 有大量的新特性和增強如 Lambda 表達式绪颖,Streams,CompletableFuture等柠横。...
    YDDMAX_Y閱讀 4,755評論 0 15
  • 更多 Java 并發(fā)編程方面的文章牍氛,請參見文集《Java 并發(fā)編程》 所謂異步調用其實就是實現(xiàn)一個可無需等待被調用...
    專職跑龍?zhí)?/span>閱讀 21,363評論 0 26
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,797評論 0 38
  • 晚上一切收拾妥當,四人坐著聊天搬俊,看婆婆聊意甚濃紊扬,我便啟話,早上跟先生討論過要不要孩子的問題唉擂,想聽聽爸媽的意見餐屎。 先...
    puticat閱讀 176評論 0 0