为什么CompletableFuture.runAsync()并不总是提交给ForkJoinPool.commonPool()?

问题描述 投票:0回答:1

CompletableFuture.runAsync文档说明:

返回一个新的CompletableFuture,它由运行在给定操作后在CompletableFuture.runAsync中运行的任务异步完成。

但是据我所知,ForkJoinPool.commonPool()仅在ForkJoinPool.commonPool()时才向runAsync提交任务。如果没有,它将为每个提交的任务手动创建一个新的ForkJoinPool.commonPool()

为什么会这样?

java multithreading asynchronous completable-future forkjoinpool
1个回答
0
投票

是,如果ForkJoinPool并行度级别小于2,则为每个任务ForkJoinPool.getCommonPoolParallelism() > 1创建新线程

所有没有显式Executor参数的异步方法都是使用ForkJoinPool.commonPool()(除非它不支持并行度至少为2,在这种情况下,将创建一个新的线程来运行每个任务)。

© www.soinside.com 2019 - 2024. All rights reserved.