日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CompletableFuture API用法介绍(二)

發布時間:2025/1/21 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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用法介紹(一),這篇繼續講解CompletableFuture 其它API用法。

一、純消費 API

單純的去消費結果而不會返回新的值,因些計算結果為 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)】
public static void completableFuturThenAccep()throws Exception {CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> 10).thenAccept(System.out::println) //消費 上一級返回值 10.thenAcceptAsync(System.out::println); //上一級沒有返回值 輸出nullSystem.out.println(future.get());//null}

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 相比,參數類型多了一個 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 類型的參數,不接收上一級的返回值

  • public CompletableFuture runAfterBoth
  • public CompletableFuture runAfterBothAsync
  • public CompletableFuture runAfterBothAsync
public static void complateFuturRunAfterBoth() throws Exception { System.out.println("開始");CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {System.out.println(10);return 10;}).runAfterBoth(CompletableFuture.supplyAsync(() -> {System.out.println(20);return 20;}), () -> System.out.println("開始運行run"));System.out.println(future.get());}

4、thenRun(Runnable action)

thenRun它的入參是一個Runnable的實例,表示當得到上一步的結果時的操作。

  • public CompletionStage thenRun(Runnable action);
  • public CompletionStage thenRunAsync(Runnable action);
  • public CompletionStage thenRunAsync(Runnable action,Executor executor);
public static void complateFuturThenRun() throws Exception {CompletableFuture.supplyAsync(() -> {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "hello";}).thenRun(() -> System.out.println("hello world"));while (true){}}

二、組合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 接收上一級返回的結果,并返回一個新的 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是并行執行的,它們之間并沒有先后依賴順序,other并不會等待先前的CompletableFuture執行完畢后再執行。

thenCombine 和 supplyAsync 不一定哪個先哪個后,是并行執行的。

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 這個消費者就會被執行。這個方法返回 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先執行完就會根據它的結果計算。

3、 applyToEither

總會碰到有兩種渠道完成同一個事情,所以就可以調用這個方法,找一個最快的結果進行處理。

  • 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);
public static void complateFuturApplyToEitherr() throws Exception {String result = CompletableFuture.supplyAsync(() -> {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}return "s1";}).applyToEither(CompletableFuture.supplyAsync(() -> {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "hello world";}), s -> s).join();System.out.println(result);}

acceptEither 沒有返回值,applyToEither 有返回值

四、allOf / anyOf

1、allOf

  • public static CompletableFuture allOf
    這個方法的意思是把有方法都執行完才往下執行,沒有返回值
public static void complateFuturAllOf() throws Exception {Random random = new Random();CompletableFuture.allOf(CompletableFuture.runAsync(() -> {try {Thread.sleep(random.nextInt(5000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println(1);}),CompletableFuture.runAsync(() -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println(2);})).get();}

2、anyOf

  • public static CompletableFuture anyOf(CompletableFuture<?>… cfs)
    任務一個方法執行完都往下執行,返回一個Object類型的值
public static void complateFuturAnyOf() throws Exception {Random random = new Random();Object o = CompletableFuture.anyOf(CompletableFuture.runAsync(() -> {try {Thread.sleep(random.nextInt(4000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println(1);}),CompletableFuture.runAsync(() -> {try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println(2);})).get();System.out.println(o);}

以上就是CompletableFuture API的基本用法,根據業務靈活組合使用。對我們的編程會很有幫助。

總結

以上是生活随笔為你收集整理的CompletableFuture API用法介绍(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。