溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java8?CompletableFuture異步多線程怎么實現

發布時間:2023-04-08 16:48:58 來源:億速云 閱讀:403 作者:iii 欄目:開發技術

Java8 CompletableFuture異步多線程怎么實現

目錄

  1. 引言
  2. CompletableFuture簡介
  3. 創建CompletableFuture
  4. 異步執行任務
  5. 任務鏈式調用
  6. 異常處理
  7. 組合多個CompletableFuture
  8. 超時處理
  9. CompletableFuture與線程池
  10. 實際應用場景
  11. 總結

引言

在現代軟件開發中,異步編程和多線程處理變得越來越重要。Java 8引入了CompletableFuture類,它提供了一種強大的方式來處理異步任務和多線程編程。本文將詳細介紹如何使用CompletableFuture來實現異步多線程編程,并探討其在實際應用中的使用場景。

CompletableFuture簡介

CompletableFuture是Java 8引入的一個類,它實現了Future接口,并提供了更豐富的功能來處理異步任務。與傳統的Future相比,CompletableFuture支持鏈式調用、異常處理、任務組合等高級功能,使得異步編程更加靈活和強大。

創建CompletableFuture

1. 使用CompletableFuture.runAsync()

runAsync()方法用于執行一個沒有返回值的異步任務。

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    // 異步任務代碼
    System.out.println("異步任務執行中...");
});

2. 使用CompletableFuture.supplyAsync()

supplyAsync()方法用于執行一個有返回值的異步任務。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // 異步任務代碼
    return "異步任務執行結果";
});

異步執行任務

1. 使用thenApply()

thenApply()方法用于在異步任務完成后對結果進行處理。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    return "Hello";
}).thenApply(result -> {
    return result + " World";
});

System.out.println(future.get()); // 輸出: Hello World

2. 使用thenAccept()

thenAccept()方法用于在異步任務完成后消費結果。

CompletableFuture.supplyAsync(() -> {
    return "Hello";
}).thenAccept(result -> {
    System.out.println(result + " World");
});

3. 使用thenRun()

thenRun()方法用于在異步任務完成后執行一個沒有返回值的操作。

CompletableFuture.supplyAsync(() -> {
    return "Hello";
}).thenRun(() -> {
    System.out.println("任務完成");
});

任務鏈式調用

CompletableFuture支持鏈式調用,可以將多個異步任務串聯起來執行。

CompletableFuture.supplyAsync(() -> {
    return "Hello";
}).thenApply(result -> {
    return result + " World";
}).thenApply(result -> {
    return result + "!";
}).thenAccept(result -> {
    System.out.println(result); // 輸出: Hello World!
});

異常處理

1. 使用exceptionally()

exceptionally()方法用于處理異步任務執行過程中發生的異常。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("任務執行失敗");
}).exceptionally(ex -> {
    System.out.println("捕獲異常: " + ex.getMessage());
    return "默認值";
});

System.out.println(future.get()); // 輸出: 默認值

2. 使用handle()

handle()方法用于在異步任務完成后處理結果或異常。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("任務執行失敗");
}).handle((result, ex) -> {
    if (ex != null) {
        System.out.println("捕獲異常: " + ex.getMessage());
        return "默認值";
    }
    return result;
});

System.out.println(future.get()); // 輸出: 默認值

組合多個CompletableFuture

1. 使用thenCompose()

thenCompose()方法用于將一個CompletableFuture的結果作為另一個CompletableFuture的輸入。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    return "Hello";
});

CompletableFuture<String> future2 = future1.thenCompose(result -> {
    return CompletableFuture.supplyAsync(() -> {
        return result + " World";
    });
});

System.out.println(future2.get()); // 輸出: Hello World

2. 使用thenCombine()

thenCombine()方法用于將兩個CompletableFuture的結果進行組合。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    return "Hello";
});

CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
    return "World";
});

CompletableFuture<String> combinedFuture = future1.thenCombine(future2, (result1, result2) -> {
    return result1 + " " + result2;
});

System.out.println(combinedFuture.get()); // 輸出: Hello World

3. 使用allOf()

allOf()方法用于等待多個CompletableFuture全部完成。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    return "Hello";
});

CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
    return "World";
});

CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2);

allFutures.thenRun(() -> {
    System.out.println("所有任務完成");
});

4. 使用anyOf()

anyOf()方法用于等待多個CompletableFuture中的任意一個完成。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    return "Hello";
});

CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
    return "World";
});

CompletableFuture<Object> anyFuture = CompletableFuture.anyOf(future1, future2);

System.out.println(anyFuture.get()); // 輸出: Hello 或 World

超時處理

CompletableFuture本身不直接支持超時處理,但可以通過CompletableFutureScheduledExecutorService結合來實現。

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    try {
        Thread.sleep(5000); // 模擬長時間任務
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "任務完成";
});

CompletableFuture<String> timeoutFuture = new CompletableFuture<>();
scheduler.schedule(() -> {
    timeoutFuture.completeExceptionally(new TimeoutException("任務超時"));
}, 2, TimeUnit.SECONDS);

CompletableFuture<String> resultFuture = future.applyToEither(timeoutFuture, Function.identity());

try {
    System.out.println(resultFuture.get()); // 輸出: 任務超時
} catch (ExecutionException e) {
    System.out.println(e.getCause().getMessage()); // 輸出: 任務超時
}

CompletableFuture與線程池

CompletableFuture默認使用ForkJoinPool.commonPool()來執行異步任務,但也可以通過指定自定義的線程池來控制任務的執行。

ExecutorService customExecutor = Executors.newFixedThreadPool(10);

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    return "Hello";
}, customExecutor);

System.out.println(future.get()); // 輸出: Hello

實際應用場景

1. 并行處理多個任務

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    return "任務1完成";
});

CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
    return "任務2完成";
});

CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
    return "任務3完成";
});

CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3);

allFutures.thenRun(() -> {
    System.out.println("所有任務完成");
});

2. 異步調用外部服務

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // 調用外部服務
    return callExternalService();
}).thenApply(result -> {
    // 處理外部服務返回的結果
    return processResult(result);
}).exceptionally(ex -> {
    // 處理異常
    return handleException(ex);
});

3. 批量處理數據

List<CompletableFuture<String>> futures = new ArrayList<>();

for (int i = 0; i < 10; i++) {
    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
        return processData(i);
    });
    futures.add(future);
}

CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));

allFutures.thenRun(() -> {
    System.out.println("所有數據處理完成");
});

總結

CompletableFuture是Java 8中引入的一個強大的工具,它提供了豐富的功能來處理異步任務和多線程編程。通過CompletableFuture,我們可以輕松地實現任務的鏈式調用、異常處理、任務組合等高級功能。在實際應用中,CompletableFuture可以用于并行處理多個任務、異步調用外部服務、批量處理數據等場景。掌握CompletableFuture的使用,將極大地提高我們的異步編程能力和代碼的可維護性。


以上是關于Java 8中CompletableFuture異步多線程實現的詳細介紹。希望本文能幫助你更好地理解和使用CompletableFuture,并在實際項目中發揮其強大的功能。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女