最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中的CompletableFuture核心用法和常見場(chǎng)景

 更新時(shí)間:2025年12月10日 11:37:54   作者:猩火燎猿  
CompletableFuture是Java8引入的強(qiáng)大的異步編程工具,支持鏈?zhǔn)疆惒骄幊?、組合、異常處理和回調(diào),介紹其核心用法,通過(guò)實(shí)用示例和注意事項(xiàng),展示了如何在實(shí)際場(chǎng)景中應(yīng)用CompletableFuture,以提高并發(fā)能力和響應(yīng)速度,感興趣的朋友跟隨小編一起看看吧

1、引言

CompletableFuture 是 Java 8 引入的一個(gè)非常強(qiáng)大的異步編程工具,屬于 java.util.concurrent 包。它不僅支持異步執(zhí)行任務(wù),還支持任務(wù)的組合、異常處理、回調(diào)等豐富的操作。下面我會(huì)詳細(xì)介紹 CompletableFuture 的核心用法和常見場(chǎng)景。

2. 基本概念

  • Future:早期的異步結(jié)果表示,功能有限,只能通過(guò) get() 阻塞獲取結(jié)果。
  • CompletableFuture:增強(qiáng)版的 Future,支持鏈?zhǔn)疆惒骄幊?、組合、異常處理、回調(diào)等。

3. 創(chuàng)建 CompletableFuture

3.1. 手動(dòng)創(chuàng)建

CompletableFuture<String> future = new CompletableFuture<>();
// 可以手動(dòng)完成
future.complete("Hello");

3.2. 通過(guò)靜態(tài)工廠方法

  • supplyAsync:有返回值,異步執(zhí)行
  • runAsync:無(wú)返回值,異步執(zhí)行
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
    // 模擬耗時(shí)操作
    return "Hello CompletableFuture";
});
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
    // 執(zhí)行某些操作
});

4. 獲取結(jié)果

  • get():阻塞等待結(jié)果
  • join():類似于 get(),但遇到異常會(huì)拋出 unchecked 異常
String result = future1.get(); // 可能拋出異常
String result2 = future1.join(); // RuntimeException

5. 回調(diào)和鏈?zhǔn)讲僮?/h2>

5.1. thenApply / thenApplyAsync

對(duì)結(jié)果進(jìn)行轉(zhuǎn)換(有返回值)

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 100)
        .thenApply(i -> i + 10); // 結(jié)果為110

5.2. thenAccept / thenAcceptAsync

對(duì)結(jié)果做處理,無(wú)返回值

CompletableFuture.supplyAsync(() -> "hello")
        .thenAccept(s -> System.out.println(s));

5.3. thenRun / thenRunAsync

無(wú)參數(shù)、無(wú)返回值,僅執(zhí)行后續(xù)操作

CompletableFuture.supplyAsync(() -> "hello")
        .thenRun(() -> System.out.println("任務(wù)執(zhí)行完畢"));

6. 任務(wù)組合

5.1. thenCombine / thenCombineAsync

兩個(gè)任務(wù)都完成后,合并結(jié)果

CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> 10);
CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> 20);
CompletableFuture<Integer> result = f1.thenCombine(f2, (a, b) -> a + b); // 30

6.2. thenCompose / thenComposeAsync

任務(wù)依賴,前一個(gè)結(jié)果作為后一個(gè)輸入

CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> "Hello")
    .thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));

6.3. allOf / anyOf

等待多個(gè)任務(wù)全部/任一完成

CompletableFuture<Void> all = CompletableFuture.allOf(f1, f2);
all.join(); // 等待全部完成
CompletableFuture<Object> any = CompletableFuture.anyOf(f1, f2);
Object fastest = any.join(); // 任意一個(gè)完成即可

7. 異常處理

7.1. exceptionally

捕獲異常,返回默認(rèn)值

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("出錯(cuò)了");
}).exceptionally(e -> {
    System.out.println(e.getMessage());
    return 0;
});

7.2. handle / handleAsync

無(wú)論成功或失敗都處理

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("出錯(cuò)了");
}).handle((result, ex) -> {
    if (ex != null) {
        System.out.println(ex.getMessage());
        return 0;
    }
    return result;
});

8. 自定義線程池

默認(rèn)使用 ForkJoinPool.commonPool(),可以自定義線程池:

ExecutorService executor = Executors.newFixedThreadPool(2);
CompletableFuture.supplyAsync(() -> "hello", executor);

9. 實(shí)用示例

9.1. 多個(gè)異步任務(wù)并發(fā)執(zhí)行,最后匯總

CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> "A");
CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> "B");
CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> "C");
CompletableFuture<Void> all = CompletableFuture.allOf(f1, f2, f3);
all.thenRun(() -> {
    try {
        System.out.println(f1.get() + f2.get() + f3.get());
    } catch (Exception e) {
        e.printStackTrace();
    }
});

10. 注意事項(xiàng)

  • 盡量避免阻塞(如 get()),推薦使用回調(diào)。
  • 注意線程池資源,合理分配,避免 OOM。
  • 處理好異常,避免線程池線程被異常吞掉。

11. 進(jìn)階用法

11.1. 串聯(lián)和并聯(lián)任務(wù)

串聯(lián)(依賴關(guān)系)

當(dāng)一個(gè)任務(wù)的結(jié)果依賴于另一個(gè)任務(wù)時(shí),使用 thenCompose

CompletableFuture<String> getUserId = CompletableFuture.supplyAsync(() -> "user123");
CompletableFuture<String> getUserInfo = getUserId.thenCompose(id ->
    CompletableFuture.supplyAsync(() -> "用戶信息:" + id)
);

并聯(lián)(聚合結(jié)果)

多個(gè)任務(wù)并發(fā)執(zhí)行,最后聚合結(jié)果:

CompletableFuture<Integer> t1 = CompletableFuture.supplyAsync(() -> 1);
CompletableFuture<Integer> t2 = CompletableFuture.supplyAsync(() -> 2);
CompletableFuture<Integer> t3 = CompletableFuture.supplyAsync(() -> 3);
CompletableFuture<List<Integer>> all = CompletableFuture.allOf(t1, t2, t3)
    .thenApply(v -> {
        List<Integer> result = new ArrayList<>();
        result.add(t1.join());
        result.add(t2.join());
        result.add(t3.join());
        return result;
    });

11.2. 超時(shí)控制

Java 9 后,CompletableFuture 增加了超時(shí)相關(guān)方法:

future.orTimeout(3, TimeUnit.SECONDS)
      .exceptionally(ex -> {
          System.out.println("超時(shí)啦");
          return null;
      });

或自己實(shí)現(xiàn):

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    Thread.sleep(5000);
    return "hello";
});
try {
    String result = future.get(2, TimeUnit.SECONDS); // 2秒超時(shí)
} catch (TimeoutException e) {
    System.out.println("超時(shí)了");
}

11.3. 異步流水線

你可以鏈?zhǔn)降亟M合多個(gè)異步操作,形成“流水線”:

CompletableFuture.supplyAsync(() -> "A")
    .thenApply(s -> s + "B")
    .thenApply(s -> s + "C")
    .thenAccept(System.out::println); // 輸出 ABC

11.4. 處理異常和兜底方案

推薦使用 handle 或 exceptionally 做兜底:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("出錯(cuò)了");
})
.handle((result, ex) -> ex == null ? result : -1);

11.5. 自定義線程池的好處

  • 控制線程數(shù)量,避免公共線程池被占滿。
  • 適合高并發(fā)、IO密集型場(chǎng)景。
ExecutorService executor = Executors.newFixedThreadPool(10);
CompletableFuture.supplyAsync(() -> "業(yè)務(wù)邏輯", executor);

12. 實(shí)戰(zhàn)場(chǎng)景舉例

12.1. 微服務(wù)并發(fā)調(diào)用

假設(shè)要并發(fā)調(diào)用三個(gè)微服務(wù)接口,最后聚合結(jié)果:

CompletableFuture<String> api1 = CompletableFuture.supplyAsync(() -> callApi1());
CompletableFuture<String> api2 = CompletableFuture.supplyAsync(() -> callApi2());
CompletableFuture<String> api3 = CompletableFuture.supplyAsync(() -> callApi3());
CompletableFuture<Void> all = CompletableFuture.allOf(api1, api2, api3);
all.thenAccept(v -> {
    String r1 = api1.join();
    String r2 = api2.join();
    String r3 = api3.join();
    System.out.println("聚合結(jié)果:" + r1 + r2 + r3);
});

12.2. 異步寫數(shù)據(jù)庫(kù)+異步發(fā)消息

CompletableFuture<Void> saveDb = CompletableFuture.runAsync(() -> saveToDb());
CompletableFuture<Void> sendMsg = CompletableFuture.runAsync(() -> sendMsg());
CompletableFuture.allOf(saveDb, sendMsg)
    .thenRun(() -> System.out.println("所有操作完成"));

13. 常見問(wèn)題與建議

  1. 線程池泄漏:線程池要合理關(guān)閉,避免資源泄漏。
  2. 異常未處理:建議所有鏈路最后加 .exceptionally 或 .handle。
  3. 阻塞等待:盡量用回調(diào)而不是 get() 或 join()。
  4. 鏈?zhǔn)讲僮?/strong>:推薦鏈?zhǔn)骄幊?,代碼更清晰。

總結(jié)

CompletableFuture 是 Java 異步編程的利器,支持豐富的組合、回調(diào)和異常處理能力。合理使用可以極大提升程序的并發(fā)能力和響應(yīng)速度。

到此這篇關(guān)于Java中的CompletableFuture核心用法和常見場(chǎng)景的文章就介紹到這了,更多相關(guān)java completablefuture使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

金阳县| 梅河口市| 鸡东县| 四平市| 乃东县| 迁西县| 宾阳县| 全椒县| 谢通门县| 淮阳县| 雅安市| 阜康市| 葵青区| 乐亭县| 泾川县| 香港| 桐庐县| 内黄县| 新郑市| 依兰县| 焉耆| 鹤壁市| 丽水市| 通河县| 临泽县| 宣武区| 洛浦县| 防城港市| 微山县| 合水县| 望城县| 游戏| 崇州市| 高淳县| 昭苏县| 团风县| 龙州县| 泾阳县| 铜陵市| 连江县| 确山县|