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)文章
多線程_解決Runnable接口無(wú)start()方法的情況
這篇文章主要介紹了多線程_解決Runnable接口無(wú)start()方法的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Java數(shù)據(jù)結(jié)構(gòu)優(yōu)先隊(duì)列實(shí)練
通常都把隊(duì)列比喻成排隊(duì)買東西,大家都很守秩序,先排隊(duì)的人就先買東西。但是優(yōu)先隊(duì)列有所不同,它不遵循先進(jìn)先出的規(guī)則,而是根據(jù)隊(duì)列中元素的優(yōu)先權(quán),優(yōu)先權(quán)最大的先被取出,這篇文章主要介紹了java優(yōu)先隊(duì)列的真題,感興趣的朋友一起看看吧2022-07-07
SpringBoot創(chuàng)建動(dòng)態(tài)定時(shí)任務(wù)的幾種方式小結(jié)
SpringBoot提供了多種實(shí)現(xiàn)定時(shí)任務(wù)的方式,包括使用@Scheduled注解、SchedulingConfigurer接口、TaskScheduler接口和Quartz框架,@Scheduled適合簡(jiǎn)單的定時(shí)任務(wù),文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
SpringBoot2整合Drools規(guī)則引擎及案例詳解
這篇文章主要介紹了SpringBoot2整合Drools規(guī)則引擎及案例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法
這篇文章主要介紹了Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法,需要的朋友可以參考下2015-11-11

