Java CompletableFuture與ForkJoinPool的關(guān)系及說(shuō)明
CompletableFuture 與 ForkJoinPool 的關(guān)系
CompletableFuture
默認(rèn)使用 ForkJoinPool.commonPool()
來(lái)執(zhí)行異步任務(wù),但這不是唯一的選擇。
1. 默認(rèn)行為
當(dāng)您使用以下方法創(chuàng)建異步任務(wù)時(shí),默認(rèn)會(huì)使用 ForkJoinPool.commonPool()
:
CompletableFuture.supplyAsync(() -> {...}); // 使用ForkJoinPool.commonPool() CompletableFuture.runAsync(() -> {...}); // 使用ForkJoinPool.commonPool()
2. 自定義線程池
您也可以顯式指定其他 Executor
(線程池):
ExecutorService customExecutor = Executors.newFixedThreadPool(10); CompletableFuture.supplyAsync(() -> {...}, customExecutor); // 使用自定義線程池
3. ForkJoinPool 的特點(diǎn)
ForkJoinPool.commonPool()
是一個(gè)共享的工作竊取線程池,具有以下特性:
- 默認(rèn)線程數(shù)等于 CPU 核心數(shù)減一(Runtime.getRuntime().availableProcessors() - 1)
- 使用工作竊?。╳ork-stealing)算法,適合處理大量小任務(wù)
- 是 JVM 全局共享的,適合輕量級(jí)并行任務(wù)
4. 為什么選擇 ForkJoinPool
Java 設(shè)計(jì)者選擇 ForkJoinPool
作為默認(rèn)實(shí)現(xiàn)是因?yàn)椋?/p>
- 工作竊取算法:可以更好地利用多核處理器
- 適合異步任務(wù):
CompletableFuture
通常用于組合多個(gè)小任務(wù) - 避免線程創(chuàng)建開(kāi)銷:使用共享池減少資源消耗
5. 實(shí)際應(yīng)用建議
- CPU密集型任務(wù):使用默認(rèn)的
ForkJoinPool
通常效果不錯(cuò) - IO密集型任務(wù):建議使用自定義的固定大小線程池(如
Executors.newFixedThreadPool
) - 長(zhǎng)時(shí)間運(yùn)行任務(wù):避免使用公共池,以免影響其他使用公共池的功能
6. 示例代碼
import java.util.concurrent.*; public class CompletableFuturePoolExample { public static void main(String[] args) { // 默認(rèn)使用ForkJoinPool.commonPool() CompletableFuture<Void> defaultPoolFuture = CompletableFuture.runAsync(() -> { System.out.println("Default pool - Thread: " + Thread.currentThread().getName()); }); // 使用自定義線程池 ExecutorService customPool = Executors.newFixedThreadPool(2); CompletableFuture<Void> customPoolFuture = CompletableFuture.runAsync(() -> { System.out.println("Custom pool - Thread: " + Thread.currentThread().getName()); }, customPool); // 等待任務(wù)完成 CompletableFuture.allOf(defaultPoolFuture, customPoolFuture).join(); customPool.shutdown(); } }
7. 注意事項(xiàng)
公共池的大小可以通過(guò)系統(tǒng)屬性調(diào)整:
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "8");
在 Java 9+ 中,公共池的默認(rèn)行為有所改變,使用更保守的線程數(shù)策略
總結(jié)
CompletableFuture
默認(rèn)確實(shí)基于 ForkJoinPool
,但可以根據(jù)需要靈活選擇其他線程池實(shí)現(xiàn)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
這篇文章主要介紹了SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Java使用條件語(yǔ)句和循環(huán)結(jié)構(gòu)確定控制流(實(shí)例)
下面小編就為大家?guī)?lái)一篇Java使用條件語(yǔ)句和循環(huán)結(jié)構(gòu)確定控制流(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Mybatis plus中使用in查詢出錯(cuò)如何解決
這篇文章主要介紹了Mybatis plus中使用in查詢出錯(cuò)的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08springboot處理url中帶斜杠/\字符的參數(shù)報(bào)400問(wèn)題
這篇文章主要介紹了springboot處理url中帶斜杠/\字符的參數(shù)報(bào)400問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java構(gòu)造函數(shù)里的一些坑記錄super()和this()
這篇文章主要介紹了Java構(gòu)造函數(shù)里的一些坑記錄super()和this(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03SpringBoot使用MyBatis時(shí)的幾種傳參規(guī)范示例
使用Mybatis作為持久層框架時(shí),對(duì)于數(shù)據(jù)庫(kù)的增刪改查等操作都需要參數(shù)的傳遞,本文就詳細(xì)的介紹了一下SpringBoot使用MyBatis時(shí)的幾種傳參規(guī)范示例,感興趣的可以了解一下2022-02-02Java 中synchronize函數(shù)的實(shí)例詳解
這篇文章主要介紹了Java 中synchronize函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家理解使用synchronize函數(shù)的使用方法,需要的朋友可以參考下2017-09-09