Search code examples
javamultithreadingjava-8java-streamforkjoinpool

Can a long running task in a Fork-Join thread pool block all threads?


I've been learning about Java 8's parallel streams and this article came up on the first page of Google search. I am unable to understand this particular line from the article

...all parallel streams use common fork-join thread pool and if you submit a long-running task, you effectively block all threads in the pool.

I can't figure out how a long running task can block all other threads in a pool.


Solution

  • In this statement “submit a long-running task” means “perform a long-running task with a parallel stream”. It’s the intention of parallel stream processing, to split the work and distribute at to all worker threads of the common thread pool.

    This pool has a number of threads configured to utilize all CPU cores and if all cores are busy, that goal has been reached.

    A problem arises if these threads get blocked by waiting for the completion of an I/O operation. Then, the CPU cores are not utilized, which may not only degrade the performance of other parallel streams, even within the same operation, the optimal number of concurrent I/O operations may be entirely different than the number of CPU cores.

    The conclusion is that the Stream API is not suitable for parallel I/O operations.