首頁 > Java > java教程 > 主體

completablefuture 用法

DDD
發布: 2024-08-15 14:15:30
原創
258 人瀏覽過

本文介紹了 CompletableFuture,一個用於建立和管理非同步任務的類別。討論瞭如何使用 allOf 方法等待多個非同步操作完成,描述了取消機制,並解釋瞭如何

completablefuture 用法

如何使用 CompletableFuture 等待多個非同步操作完成?

CompletableFuture 提供了一個名為 allOf 的方法,可用來等待多個非同步操作完成。 allOf 方法採用可變數量的 CompletableFuture 物件作為參數,並傳回一個新的 CompletableFuture,該新 CompletableFuture 在所有輸入

物件完成時完成。

allOf以下程式碼範例顯示如何使用

方法等待多個非同步操作完成:
<code class="java">CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2);

allOf.join();

System.out.println(future1.get()); // Prints "Hello"
System.out.println(future2.get()); // Prints "World"</code>
登入後複製

CompletableFuture 的取消機制是如何運作的?

cancelCompletableFuture 提供了一個 CompletableFuture 方法,可以用來取消由 cancel 表示的非同步操作。

方法採用一個布林參數,指示取消是否應該中斷。

cancel如果在中斷標誌設定為 true 的情況下呼叫 cancel 方法,非同步操作將中斷如果它仍在運作。如果非同步操作已經完成,則

方法將無法運作。

cancel如果在中斷標誌設定為false的情況下呼叫cancel方法,則非同步操作將被取消,如果尚未完成。如果非同步操作已經完成,

方法將無法運作。

cancel以下程式碼範例展示如何使用

方法取消非同步操作:
<code class="java">CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "Hello World";
});

future.cancel(true); // Interrupt the asynchronous operation

if (future.isCancelled()) {
    System.out.println("The asynchronous operation was cancelled.");
}</code>
登入後複製

如何使用 CompletableFuture 處理異常和回傳值?

thenApplyCompletableFuture 提供了處理異常和傳回值的方法。 thenAcceptexceptionally 方法可用來處理回傳值,而 handle

方法可用於處理異常。

thenApplyCompletableFuture 方法將函數作為參數並傳回一個新的 CompletableFuture,該新 thenApply 將透過將函數應用於原始

的結果來完成。以下程式碼範例展示如何使用
<code class="java">CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");

CompletableFuture<Integer> future2 = future.thenApply(s -> s.length());

future2.join();

System.out.println(future2.get()); // Prints 11</code>
登入後複製
方法來處理回傳值:

thenAcceptCompletableFutureCompletableFuture 方法將消費者作為參數並傳回將完成的新thenAccept當消費者已應用於原始

的結果時。以下程式碼範例展示如何使用
<code class="java">CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");

CompletableFuture<Void> future2 = future.thenAccept(s -> System.out.println(s));

future2.join(); // Prints "Hello World"</code>
登入後複製
方法來處理傳回值:

exceptionallyCompletableFutureCompletableFuture 方法接受一個函數作為參數,並傳回一個將完成的新exceptionally將函數應用於導致原始

異常完成的異常的結果。以下程式碼範例展示如何使用
<code class="java">CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("Error!");
});

CompletableFuture<String> future2 = future.exceptionally(e -> "Error occurred: " + e.getMessage());

future2.join();

System.out.println(future2.get()); // Prints "Error occurred: java.lang.RuntimeException: Error!"</code>
登入後複製
方法來處理異常:

handleCompletableFutureCompletableFuture 方法採用雙函數作為參數,並傳回一個新的CompletableFuture,它將完成,並將雙函數應用於原始handle 的結果以及導致原始

異常完成的異常(如果有)。以下程式碼範例顯示如何使用
<code class="java">CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    if (Math.random() > 0.5) {
        return "Success";
    } else {
        throw new RuntimeException("Error!");
    }
});

CompletableFuture<String> future2 = future.handle((result, exception) -> {
    if (exception != null) {
        return "Error occurred: " + exception.getMessage();
    } else {
        return result;
    }
});

future2.join();

System.out.println(future2.get()); // Prints "Success" or "Error occurred: java.lang.RuntimeException: Error!"</code>
登入後複製
方法處理回傳值或異常:

以上是completablefuture 用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!