为什么常见的ForkJoinPool不会尝试使用所有内核?

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

我理解为什么线程池大小与CPU核心数量有关,但为什么ForkJoinThread的设计者默认使用# of cpu cores - 1线程?为什么-1

如果我正在构建我自己的ForkJoinPool(不使用公共实例),并且main线程在池上被阻塞等待它返回一些结果,有什么理由我想分配少于Runtime.getRuntime().availableProcessors()线程?

更新:请解释你为什么要投票。否则,我无法改善这个问题。

java threadpool fork-join
2个回答
1
投票

Oracle JDK implementation有评论。它指出

 * When external threads submit to the common pool, they can
 * perform subtask processing (see externalHelpComplete and
 * related methods) upon joins.  This caller-helps policy makes it
 * sensible to set common pool parallelism level to one (or more)
 * less than the total number of available cores, or even zero for
 * pure caller-runs.

换句话说,当外部(不是FJP线程的一部分)(可以)帮助执行任务时,额外的线程并不总是有益的。


0
投票

这是有道理的。主线程始终需要一个线程,一次运行的最大线程数是核心总数。因此,默认的并行性是# of cpu cores - 1

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