Java8中CompletableFuture的使用方法

小編給大家分享一下Java8中CompletableFuture的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

十載的元寶網站建設經驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網絡營銷推廣的優(yōu)勢是能夠根據用戶設備顯示端的尺寸不同,自動調整元寶建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯公司從事“元寶網站設計”,“元寶網站推廣”以來,每個客戶項目都認真落實執(zhí)行。

作為Java 8 Concurrency API改進而引入,本文是CompletableFuture類的功能和用例的介紹。同時在Java 9 也有對CompletableFuture有一些改進,之后再進入講解。

Future計算

Future異步計算很難操作,通常我們希望將任何計算邏輯視為一系列步驟。但是在異步計算的情況下,表示為回調的方法往往分散在代碼中或者深深地嵌套在彼此內部。但是當我們需要處理其中一個步驟中可能發(fā)生的錯誤時,情況可能會變得更復雜。

Futrue接口是Java 5中作為異步計算而新增的,但它沒有任何方法去進行計算組合或者處理可能出現的錯誤。

在Java 8中,引入了CompletableFuture類。與Future接口一起,它還實現了CompletionStage接口。此接口定義了可與其他Future組合成異步計算契約。

CompletableFuture同時是一個組合和一個框架,具有大約50種不同的構成,結合,執(zhí)行異步計算步驟和處理錯誤。

如此龐大的API可能會令人難以招架,下文將調一些重要的做重點介紹。

使用CompletableFuture作為Future實現

首先,CompletableFuture類實現Future接口,因此你可以將其用作Future實現,但需要額外的完成實現邏輯。

例如,你可以使用無構參構造函數創(chuàng)建此類的實例,然后使用complete方法完成。消費者可以使用get方法來阻塞當前線程,直到get()結果。

在下面的示例中,我們有一個創(chuàng)建CompletableFuture實例的方法,然后在另一個線程中計算并立即返回Future。

計算完成后,該方法通過將結果提供給完整方法來完成Future:

public Future<String> calculateAsync() throws InterruptedException {
    CompletableFuture<String> completableFuture 
      = new CompletableFuture<>();
 
    Executors.newCachedThreadPool().submit(() -> {
        Thread.sleep(500);
        completableFuture.complete("Hello");
        return null;
    });
 
    return completableFuture;
}

為了分離計算,我們使用了Executor API ,這種創(chuàng)建和完成CompletableFuture的方法可以與任何并發(fā)包(包括原始線程)一起使用。

請注意,calculateAsync方法返回一個Future實例。

我們只是調用方法,接收Future實例并在我們準備阻塞結果時調用它的get方法。

另請注意,get方法拋出一些已檢查的異常,即ExecutionException(封裝計算期間發(fā)生的異常)和InterruptedException(表示執(zhí)行方法的線程被中斷的異常):

Future<String> completableFuture = calculateAsync();
 
// ... 
 
String result = completableFuture.get();
assertEquals("Hello", result);

如果你已經知道計算的結果,也可以用變成同步的方式來返回結果。

Future<String> completableFuture = 
  CompletableFuture.completedFuture("Hello");
 
// ...
 
String result = completableFuture.get();
assertEquals("Hello", result);

作為在某些場景中,你可能希望取消Future任務的執(zhí)行。

假設我們沒有找到結果并決定完全取消異步執(zhí)行任務。這可以通過Future的取消方法完成。此方法mayInterruptIfRunning,但在CompletableFuture的情況下,它沒有任何效果,因為中斷不用于控制CompletableFuture的處理。

這是異步方法的修改版本:

public Future<String> calculateAsyncWithCancellation() throws InterruptedException {
    CompletableFuture<String> completableFuture = new CompletableFuture<>();
 
    Executors.newCachedThreadPool().submit(() -> {
        Thread.sleep(500);
        completableFuture.cancel(false);
        return null;
    });
 
    return completableFuture;
}

當我們使用Future.get()方法阻塞結果時,cancel()表示取消執(zhí)行,它將拋出CancellationException:

Future<String> future = calculateAsyncWithCancellation();
future.get(); // CancellationException

API介紹

static方法說明

上面的代碼很簡單,下面介紹幾個 static 方法,它們使用任務來實例化一個 CompletableFuture 實例。

CompletableFuture.runAsync(Runnable runnable);
CompletableFuture.runAsync(Runnable runnable, Executor executor);

CompletableFuture.supplyAsync(Supplier<U> supplier);
CompletableFuture.supplyAsync(Supplier<U> supplier, Executor executor)

runAsync 方法接收的是 Runnable 的實例,但是它沒有返回值

supplyAsync 方法是JDK8函數式接口,無參數,會返回一個結果

這兩個方法是 executor 的升級,表示讓任務在指定的線程池中執(zhí)行,不指定的話,通常任務是在 ForkJoinPool.commonPool() 線程池中執(zhí)行的。

supplyAsync()使用

靜態(tài)方法runAsyncsupplyAsync允許我們相應地從Runnable和Supplier功能類型中創(chuàng)建CompletableFuture實例。

該Runnable的接口是在線程使用舊的接口,它不允許返回值。

Supplier接口是一個不具有參數,并返回參數化類型的一個值的單個方法的通用功能接口。

這允許將Supplier的實例作為lambda表達式提供,該表達式執(zhí)行計算并返回結果:

CompletableFuture<String> future
  = CompletableFuture.supplyAsync(() -> "Hello");
 
// ...
 
assertEquals("Hello", future.get());

thenRun()使用

在兩個任務任務A,任務B中,如果既不需要任務A的值也不想在任務B中引用,那么你可以將Runnable lambda 傳遞給thenRun()方法。在下面的示例中,在調用future.get()方法之后,我們只需在控制臺中打印一行:

模板

CompletableFuture.runAsync(() -> {}).thenRun(() -> {}); 
CompletableFuture.supplyAsync(() -> "resultA").thenRun(() -> {});
  • 第一行用的是 thenRun(Runnable runnable),任務 A 執(zhí)行完執(zhí)行 B,并且 B 不需要 A 的結果。
  • 第二行用的是 thenRun(Runnable runnable),任務 A 執(zhí)行完執(zhí)行 B,會返回resultA,但是 B 不需要 A 的結果。

實戰(zhàn)

CompletableFuture<String> completableFuture 
  = CompletableFuture.supplyAsync(() -> "Hello");
 
CompletableFuture<Void> future = completableFuture
  .thenRun(() -> System.out.println("Computation finished."));
 
future.get();

thenAccept()使用

在兩個任務任務A,任務B中,如果你不需要在Future中有返回值,則可以用 thenAccept方法接收將計算結果傳遞給它。最后的future.get()調用返回Void類型的實例。

模板

CompletableFuture.runAsync(() -> {}).thenAccept(resultA -> {}); 

CompletableFuture.supplyAsync(() -> "resultA").thenAccept(resultA -> {});

第一行中,runAsync不會有返回值,第二個方法thenAccept,接收到的resultA值為null,同時任務B也不會有返回結果

第二行中,supplyAsync有返回值,同時任務B不會有返回結果。

實戰(zhàn)

CompletableFuture<String> completableFuture
  = CompletableFuture.supplyAsync(() -> "Hello");
 
CompletableFuture<Void> future = completableFuture
  .thenAccept(s -> System.out.println("Computation returned: " + s));
 
future.get();

thenApply()使用

在兩個任務任務A,任務B中,任務B想要任務A計算的結果,可以用thenApply方法來接受一個函數實例,用它來處理結果,并返回一個Future函數的返回值:

模板

CompletableFuture.runAsync(() -> {}).thenApply(resultA -> "resultB");
CompletableFuture.supplyAsync(() -> "resultA").thenApply(resultA -> resultA + " resultB");
  • 第二行用的是 thenApply(Function fn),任務 A 執(zhí)行完執(zhí)行 B,B 需要 A 的結果,同時任務 B 有返回值。

實戰(zhàn)

CompletableFuture<String> completableFuture
  = CompletableFuture.supplyAsync(() -> "Hello");
 
CompletableFuture<String> future = completableFuture
  .thenApply(s -> s + " World");
 
assertEquals("Hello World", future.get());

當然,多個任務的情況下,如果任務 B 后面還有任務 C,往下繼續(xù)調用 .thenXxx() 即可。

thenCompose()使用

接下來會有一個很有趣的設計模式;

CompletableFuture API 的最佳場景是能夠在一系列計算步驟中組合CompletableFuture實例。

這種組合結果本身就是CompletableFuture,允許進一步再續(xù)組合。這種方法在函數式語言中無處不在,通常被稱為monadic設計模式。

簡單說,Monad就是一種設計模式,表示將一個運算過程,通過函數拆解成互相連接的多個步驟。你只要提供下一步運算所需的函數,整個運算就會自動進行下去。

在下面的示例中,我們使用thenCompose方法按順序組合兩個Futures。

請注意,此方法采用返回CompletableFuture實例的函數。該函數的參數是先前計算步驟的結果。這允許我們在下一個CompletableFuture的lambda中使用這個值:

CompletableFuture<String> completableFuture 
  = CompletableFuture.supplyAsync(() -> "Hello")
    .thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
 
assertEquals("Hello World", completableFuture.get());

該thenCompose方法連同thenApply一樣實現了結果的合并計算。但是他們的內部形式是不一樣的,它們與Java 8中可用的Stream和Optional類的map和flatMap方法是有著類似的設計思路在里面的。

兩個方法都接收一個CompletableFuture并將其應用于計算結果,但thenCompose(flatMap)方法接收一個函數,該函數返回相同類型的另一個CompletableFuture對象。此功能結構允許將這些類的實例繼續(xù)進行組合計算。

thenCombine()

取兩個任務的結果

如果要執(zhí)行兩個獨立的任務,并對其結果執(zhí)行某些操作,可以用Future的thenCombine方法:

模板

CompletableFuture<String> cfA = CompletableFuture.supplyAsync(() -> "resultA");
CompletableFuture<String> cfB = CompletableFuture.supplyAsync(() -> "resultB");

cfA.thenAcceptBoth(cfB, (resultA, resultB) -> {});

cfA.thenCombine(cfB, (resultA, resultB) -> "result A + B");

實戰(zhàn)

CompletableFuture<String> completableFuture 
  = CompletableFuture.supplyAsync(() -> "Hello")
    .thenCombine(CompletableFuture.supplyAsync(
      () -> " World"), (s1, s2) -> s1 + s2));
 
assertEquals("Hello World", completableFuture.get());

更簡單的情況是,當你想要使用兩個Future結果時,但不需要將任何結果值進行返回時,可以用thenAcceptBoth,它表示后續(xù)的處理不需要返回值,而 thenCombine 表示需要返回值:

CompletableFuture future = CompletableFuture.supplyAsync(() -> "Hello")
  .thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"),
    (s1, s2) -> System.out.println(s1 + s2));

thenApply()和thenCompose()之間的區(qū)別

在前面的部分中,我們展示了關于thenApply()和thenCompose()的示例。這兩個API都是使用的CompletableFuture調用,但這兩個API的使用是不同的。

thenApply()

此方法用于處理先前調用的結果。但是,要記住的一個關鍵點是返回類型是轉換泛型中的類型,是同一個CompletableFuture。

因此,當我們想要轉換CompletableFuture 調用的結果時,效果是這樣的  :

CompletableFuture<Integer> finalResult = compute().thenApply(s-> s + 1);

thenCompose()

該thenCompose()方法類似于thenApply()在都返回一個新的計算結果。但是,thenCompose()使用前一個Future作為參數。它會直接使結果變新的Future,而不是我們在thenApply()中到的嵌套Future,而是用來連接兩個CompletableFuture,是生成一個新的CompletableFuture:

CompletableFuture<Integer> computeAnother(Integer i){
    return CompletableFuture.supplyAsync(() -> 10 + i);
}
CompletableFuture<Integer> finalResult = compute().thenCompose(this::computeAnother);

因此,如果想要繼續(xù)嵌套鏈接CompletableFuture  方法,那么最好使用thenCompose()。

并行運行多個任務

當我們需要并行執(zhí)行多個任務時,我們通常希望等待所有它們執(zhí)行,然后處理它們的組合結果。

CompletableFuture.allOf靜態(tài)方法允許等待所有的完成任務:

API

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs){...}

實戰(zhàn)

CompletableFuture<String> future1  
  = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2  
  = CompletableFuture.supplyAsync(() -> "Beautiful");
CompletableFuture<String> future3  
  = CompletableFuture.supplyAsync(() -> "World");
 
CompletableFuture<Void> combinedFuture 
  = CompletableFuture.allOf(future1, future2, future3);
 
// ...
 
combinedFuture.get();
 
assertTrue(future1.isDone());
assertTrue(future2.isDone());
assertTrue(future3.isDone());

請注意,CompletableFuture.allOf()的返回類型是CompletableFuture <Void>。這種方法的局限性在于它不會返回所有任務的綜合結果。相反,你必須手動從Futures獲取結果。幸運的是,CompletableFuture.join()方法和Java 8 Streams API可以解決:

String combined = Stream.of(future1, future2, future3)
  .map(CompletableFuture::join)
  .collect(Collectors.joining(" "));
 
assertEquals("Hello Beautiful World", combined);

CompletableFuture 提供了 join() 方法,它的功能和 get() 方法是一樣的,都是阻塞獲取值,它們的區(qū)別在于 join() 拋出的是 unchecked Exception。這使得它可以在Stream.map()方法中用作方法引用。

異常處理

說到這里,我們順便來說下 CompletableFuture 的異常處理。這里我們要介紹兩個方法:

public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn);
public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);

看下代碼

CompletableFuture.supplyAsync(() -> "resultA")
    .thenApply(resultA -> resultA + " resultB")
    .thenApply(resultB -> resultB + " resultC")
    .thenApply(resultC -> resultC + " resultD");

上面的代碼中,任務 A、B、C、D 依次執(zhí)行,如果任務 A 拋出異常(當然上面的代碼不會拋出異常),那么后面的任務都得不到執(zhí)行。如果任務 C 拋出異常,那么任務 D 得不到執(zhí)行。

那么我們怎么處理異常呢?看下面的代碼,我們在任務 A 中拋出異常,并對其進行處理:

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException();
})
        .exceptionally(ex -> "errorResultA")
        .thenApply(resultA -> resultA + " resultB")
        .thenApply(resultB -> resultB + " resultC")
        .thenApply(resultC -> resultC + " resultD");

System.out.println(future.join());

上面的代碼中,任務 A 拋出異常,然后通過 .exceptionally() 方法處理了異常,并返回新的結果,這個新的結果將傳遞給任務 B。所以最終的輸出結果是:

errorResultA resultB resultC resultD
String name = null;
 
// ...
 
CompletableFuture<String> completableFuture  
  =  CompletableFuture.supplyAsync(() -> {
      if (name == null) {
          throw new RuntimeException("Computation error!");
      }
      return "Hello, " + name;
  })}).handle((s, t) -> s != null ? s : "Hello, Stranger!");
 
assertEquals("Hello, Stranger!", completableFuture.get());

當然,它們也可以都為 null,因為如果它作用的那個 CompletableFuture 實例沒有返回值的時候,s 就是 null。

Async后綴方法

CompletableFuture類中的API的大多數方法都有兩個帶有Async后綴的附加修飾。這些方法表示用于異步線程。

沒有Async后綴的方法使用調用線程運行下一個執(zhí)行線程階段。不帶Async方法使用ForkJoinPool.commonPool()線程池的fork / join實現運算任務。帶有Async方法使用傳遞式的Executor任務去運行。

下面附帶一個案例,可以看到有thenApplyAsync方法。在程序內部,線程被包裝到ForkJoinTask實例中。這樣可以進一步并行化你的計算并更有效地使用系統(tǒng)資源。

CompletableFuture<String> completableFuture  
  = CompletableFuture.supplyAsync(() -> "Hello");
 
CompletableFuture<String> future = completableFuture
  .thenApplyAsync(s -> s + " World");
 
assertEquals("Hello World", future.get());

JDK 9 CompletableFuture API

在Java 9中,  CompletableFuture API通過以下更改得到了進一步增強:

  • 新工廠方法增加了
  • 支持延遲和超時
  • 改進了對子類化的支持。

引入了新的實例API:

  • Executor defaultExecutor()
  • CompletableFuture<U> newIncompleteFuture()
  • CompletableFuture<T> copy()
  • CompletionStage<T> minimalCompletionStage()
  • CompletableFuture<T> completeAsync(Supplier<? extends T> supplier, Executor executor)
  • CompletableFuture<T> completeAsync(Supplier<? extends T> supplier)
  • CompletableFuture<T> orTimeout(long timeout, TimeUnit unit)
  • CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)

還有一些靜態(tài)實用方法:

  • Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
  • Executor delayedExecutor(long delay, TimeUnit unit)
  • <U> CompletionStage<U> completedStage(U value)
  • <U> CompletionStage<U> failedStage(Throwable ex)
  • <U> CompletableFuture<U> failedFuture(Throwable ex)

最后,為了解決超時問題,Java 9又引入了兩個新功能:

  • orTimeout()
  • completeOnTimeout()

結論

在本文中,我們描述了CompletableFuture類的方法和典型用例。

以上是Java8中CompletableFuture的使用方法的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道!

網站名稱:Java8中CompletableFuture的使用方法
文章鏈接:http://muchs.cn/article14/gephde.html

成都網站建設公司_創(chuàng)新互聯,為您提供服務器托管、搜索引擎優(yōu)化手機網站建設、軟件開發(fā)靜態(tài)網站、建站公司

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

搜索引擎優(yōu)化