CompletableFuture API用法介绍(二)
文章目錄
- 一、純消費 API
- 1、thenAccep
- 2、thenAcceptBoth
- 3、runAfterBoth
- 4、thenRun(Runnable action)
- 二、組合API
- 1、thenCompose
- 2、thenCombine
- 三、acceptEither / applyToEither
- 1、 acceptEither
- 3、 applyToEither
- 四、allOf / anyOf
- 1、allOf
- 2、anyOf
上一篇文章介紹部分API CompletableFuture API用法介紹(一),這篇繼續(xù)講解CompletableFuture 其它API用法。
一、純消費 API
單純的去消費結(jié)果而不會返回新的值,因些計算結(jié)果為 Void;
1、thenAccep
- public CompletableFuture thenAccept(Consumer<? super T> action)
- public CompletableFuture thenAcceptAsync(Consumer<? super T> action)
- public CompletableFuture thenAcceptAsync(Consumer<? super T> action, Executor executor)】
2、thenAcceptBoth
- public CompletableFuture thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action)
- public CompletableFuture thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action)
- public CompletableFuture thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action, Executor executor)
thenAccept 相比,參數(shù)類型多了一個 CompletionStage<? extends U> other,以上方法會接收上一個CompletionStage返回值,和當前的一個。
public static void complateFuturThenAcceptBoth() throws Exception {CompletableFuture.supplyAsync(() -> 10).thenAcceptBoth(CompletableFuture.supplyAsync(() -> 20), (a, b) -> {System.out.println(a);System.out.println(b);}).get();}3、runAfterBoth
runAfterBoth 和以上方法不同,傳一個 Runnable 類型的參數(shù),不接收上一級的返回值
- public CompletableFuture runAfterBoth
- public CompletableFuture runAfterBothAsync
- public CompletableFuture runAfterBothAsync
4、thenRun(Runnable action)
thenRun它的入?yún)⑹且粋€Runnable的實例,表示當?shù)玫缴弦徊降慕Y(jié)果時的操作。
- public CompletionStage thenRun(Runnable action);
- public CompletionStage thenRunAsync(Runnable action);
- public CompletionStage thenRunAsync(Runnable action,Executor executor);
二、組合API
1、thenCompose
- public CompletableFuture thenCompose(Function<? super T,? extends CompletionStage> fn)
- public CompletableFuture thenComposeAsync(Function<? super T,? extends CompletionStage> fn)
- public CompletableFuture thenComposeAsync(Function<? super T,? extends CompletionStage> fn, Executor executor)
以上接收類型為 Function<? super T,? extends CompletionStage> fn ,fn 接收上一級返回的結(jié)果,并返回一個新的 CompletableFuture
public static void complateFuturThenCompose() throws Exception {CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1).thenApply((a) -> {try {Thread.sleep(1000);} catch (InterruptedException e) {}return a + 10;}).thenCompose((s) -> {System.out.println(s); //11return CompletableFuture.supplyAsync(() -> s * 5);});System.out.println(future.get());//55} }2、thenCombine
- public <U,V> CompletableFuture thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
- public <U,V> CompletableFuture thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
- public <U,V> CompletableFuture thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor)
兩個CompletionStage是并行執(zhí)行的,它們之間并沒有先后依賴順序,other并不會等待先前的CompletableFuture執(zhí)行完畢后再執(zhí)行。
thenCombine 和 supplyAsync 不一定哪個先哪個后,是并行執(zhí)行的。
public static void complateFuturThenCombine() throws Exception {Random random = new Random();CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("supplyAsync");return 2;}).thenApply((a) -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("thenApply");return a * 3;}).thenCombine(CompletableFuture.supplyAsync(() -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("thenCombineAsync");return 10;}), (a, b) -> {System.out.println(a);System.out.println(b);return a + b;});System.out.println(future.get());}三、acceptEither / applyToEither
1、 acceptEither
- public CompletableFuture acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
- public CompletableFuture acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
- public CompletableFuture acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
acceptEither方法是當任意一個 CompletionStage 完成的時候,action 這個消費者就會被執(zhí)行。這個方法返回 CompletableFuture
public static void complateFuturAcceptEither() throws Exception {Random random = new Random();CompletableFuture.supplyAsync(() -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return "A";}).acceptEither(CompletableFuture.supplyAsync(() -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}return "B";}), System.out::println).get();}以上代碼有時輸出A,有時輸出B,哪個Future先執(zhí)行完就會根據(jù)它的結(jié)果計算。
3、 applyToEither
總會碰到有兩種渠道完成同一個事情,所以就可以調(diào)用這個方法,找一個最快的結(jié)果進行處理。
- public CompletionStage applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn);
- public CompletionStage applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn);
- public CompletionStage applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);
acceptEither 沒有返回值,applyToEither 有返回值
四、allOf / anyOf
1、allOf
- public static CompletableFuture allOf
這個方法的意思是把有方法都執(zhí)行完才往下執(zhí)行,沒有返回值
2、anyOf
- public static CompletableFuture anyOf(CompletableFuture<?>… cfs)
任務(wù)一個方法執(zhí)行完都往下執(zhí)行,返回一個Object類型的值
以上就是CompletableFuture API的基本用法,根據(jù)業(yè)務(wù)靈活組合使用。對我們的編程會很有幫助。
總結(jié)
以上是生活随笔為你收集整理的CompletableFuture API用法介绍(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CompletableFuture AP
- 下一篇: JAVA8 Stream方法使用详解Fi