为什么常见的 ForkJoinPool 不尝试使用所有核心?
Why doesn't the common ForkJoinPool try to use all cores?
我理解为什么线程池大小与 CPU 核心数相关,但为什么 ForkJoinThread
的设计者默认使用 # of cpu cores - 1
线程?为什么 -1
?
如果我正在构建自己的 ForkJoinPool
(不使用公共实例),并且 main
线程被阻塞在池中等待它 return 一些结果,是有什么理由我想分配少于 Runtime.getRuntime().availableProcessors()
个线程?
UPDATE:请解释您为什么要投反对票。否则,我无法改进问题。
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 线程的一部分)(可以)帮助执行任务时,额外的线程并不总是有益的。
这是有道理的。主线程始终需要一个线程,一次最大线程数 运行 就是核心总数。因此,默认并行度为 # of cpu cores - 1
.
我理解为什么线程池大小与 CPU 核心数相关,但为什么 ForkJoinThread
的设计者默认使用 # of cpu cores - 1
线程?为什么 -1
?
如果我正在构建自己的 ForkJoinPool
(不使用公共实例),并且 main
线程被阻塞在池中等待它 return 一些结果,是有什么理由我想分配少于 Runtime.getRuntime().availableProcessors()
个线程?
UPDATE:请解释您为什么要投反对票。否则,我无法改进问题。
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 线程的一部分)(可以)帮助执行任务时,额外的线程并不总是有益的。
这是有道理的。主线程始终需要一个线程,一次最大线程数 运行 就是核心总数。因此,默认并行度为 # of cpu cores - 1
.