Java Concurrency Interview

Fork/join and parallel streams question 104 of 180 senior

What is the common pool, and why does it cause trouble?

ForkJoinPool.commonPool() is a static, shared, lazily created pool sized at availableProcessors() - 1. Every parallel stream in the JVM uses it, including ones inside libraries, and CompletableFuture's async methods use it when you do not pass an executor.

So a single parallel stream doing blocking I/O occupies a scarce global resource, and every other parallel operation in the process slows down or stalls. On a machine reporting one CPU, the common pool has a single worker, so a parallel stream gets that one thread plus the caller.

You can raise its size with -Djava.util.concurrent.ForkJoinPool.common.parallelism=N, which is a blunt global setting, or run the stream inside your own ForkJoinPool by submitting it there, which works because the stream uses the pool of the thread it runs on.

forkjoin parallel-streams

Quiz yourself on Fork/join and parallel streams Suggest a correction