本文介紹相關API,使用上的介紹參見CompletableFuture 示例鞠柄。
CompletableFuture是 jdk 8里面的新的API嫉柴,類似于google guava的 ListenableFuture
和SettableFuture
计螺,提供了對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);