Java Concurrency Interview

Fork/join and parallel streams question 112 of 180 junior

What does parallelStream() change compared with stream()?

The pipeline is the same; the execution is not. The source is split by its Spliterator, the chunks run as fork/join tasks in ForkJoinPool.commonPool(), and the calling thread joins in rather than waiting idle. The common pool has one thread fewer than there are cores, so with the caller the work uses every core.

Three consequences are what the interviewer is after:

And the one that bites in production: every parallel stream in the JVM shares that one common pool, so a slow or blocking operation in one of them slows down all the others.

parallel-streams common-pool

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