How do I check the number of threads created by ForkJoinPool.commonPool()
in java? Is the degree of parallelism the same as the number of threads created by the commonPool()
? How do we create threads greater than the number of CPU cores?
How do I check the number of threads created by
ForkJoinPool.commonPool()
in java?
If you want to know the number of threads that are currently running within the ForkJoinPool
, you need to invoke the method getRunningThreadCount()
. However, this method returns only an estimate of the number of worker threads that are not blocked waiting to join tasks.
Is the degree of parallelism the same as the number of threads created by the
commonPool()
?
The level of parallelism does not correspond to the number of threads created in the ForkJoinPool
. This value is initially used to define the core pool size, which determines how many workers can stay alive at the same time. The pool size, instead, corresponds to the number of workers that are either running a task or simply in idle. However, even though the parallelism level stays the same throughout the whole existence of a ForkJoinPool
, its pool size is adjusted according to the workload. In fact, if a ForkJoinPool
continues to receive further requests, then the pool size is incremented up to a maximum pool size value. Instead, if a ForkJoinPool
does not receive any further requests for a certain period of time called keep alive time, then the workers are terminated until their number reaches core pool size once again.
How do we create threads greater than the number of CPU cores?
By default, the level of parallelism is equal to the number of physical processors, in order to ensure sort of an actual parallelism. However, this value can be set up manually via some of the ForkJoinPool
's constructors, and it cannot be changed once an instance has been created. Furthermore, according to the implementation notes, the level of parallelism cannot exceed 32767
.