Future 接口僅支援阻塞獲取結果(get()),無法組合、鏈式處理異步任務。CompletableFuture 實現 CompletionStage,提供豐富的非阻塞組合式 API,支援回調、異常處理、多任務編排,是 Java 8 異步編程的基石。
| 類別 | 代表方法 | 用途 |
|---|---|---|
| 結果轉換 | thenApply、thenCompose | 同步/異步映射、扁平化 |
| 消費結果 | thenAccept、thenRun | 無返回值的副作用操作 |
| 異常處理 | exceptionally、handle | 恢復或記錄異常 |
| 雙任務組合 | thenCombine、thenAcceptBoth、runAfterBoth | 雙結果合併、雙消費、雙運行 |
| 競態選擇 | applyToEither、acceptEither、runAfterEither | 取最快完成任務的結果 |
| 多任務編排 | allOf、anyOf | 等待全部/任一完成 |
// thenApply: 同步映射 String -> Integer -> Double
CompletableFuture<Double> f3 = f1
.thenApply(Integer::parseInt)
.thenApply(r -> r * r * Math.PI);
// thenCompose: 函數返回 CompletableFuture,扁平化避免嵌套
CompletableFuture<Document> docFuture = loadDoc();
CompletableFuture<Double> relevance = docFuture
.thenCompose(this::calculateRelevance); // calculateRelevance 返回 CF<Double>
// 僅處理異常,返回默認值
CompletableFuture<String> safe = future.exceptionally(ex -> "Default: " + ex.getMessage());
// 統一處理正常結果與異常(推薦)
CompletableFuture<Integer> safe2 = future.handle((ok, ex) -> {
if (ok != null) return Integer.parseInt(ok);
log.warn("Error", ex);
return -1;
});
CompletableFuture<Customer> custF = loadCustomer(123);
CompletableFuture<Shop> shopF = findNearestShop();
CompletableFuture<Route> routeF = custF.thenCombine(shopF,
(cust, shop) -> calculateRoute(cust, shop));
// routeF 在兩者完成後自動執行 calculateRoute
CompletableFuture<Void> all = CompletableFuture.allOf(f1, f2, f3);
all.thenRun(() -> System.out.println("全部完成"));
// 或收集結果
List<String> results = Stream.of(f1, f2, f3)
.map(CompletableFuture::join)
.collect(Collectors.toList());
CompletableFuture<String> fast = fetchFromFastService();
CompletableFuture<String> stable = fetchFromStableService();
CompletableFuture<String> first = fast.applyToEither(stable, Function.identity());
// first 完成即表示兩者中最快者已返回
Executor(如 thenApplyAsync(fn, executor)),避免佔用公共 ForkJoinPool。thenCompose/thenCombine 構建管道,少用 get()/join()。handle 或 exceptionally,防止異常吞沒導致後續任務不執行。orTimeout/completeOnTimeout;JDK 8 需配合 ScheduledExecutorService 實現。CompletableFuture 將異步編程從「回調地獄」解脫,以聲明式組合替代命令式協調。掌握 轉換、組合、異常、編排 四大核心模式,即可構建高併發、高可維護的異步系統。