Search code examples
javathreadpoolfork-joinforkjoinpool

Why is the common pool's parallelism not the same as the number of available processors?


I have the following implementation

@Test
void concurrency() {
    System.out.println("#cores : " + Runtime.getRuntime().availableProcessors());
    System.out.println("#pool : " + ForkJoinPool.getCommonPoolParallelism());
}

Output

#cores : 8
#pool : 7

Why are these two numbers different?


Solution

  • Number of Cores (N) - 1 is considered the optimal for the ForkJoinPool.
    So you have 1 core spare for other tasks, like:
    for the operation system,
    for the main thread,
    for garbage collection, etc.

    It is unlikely that you can utilise all cores at all time with the work of your pool. So you would have more context switches if you would create one more thread and that would slow down the over all execution time.