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.
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.
Submitting one task can not use all the threads in the common-pool. common-pool size is core count -1
(-1 for the main thread).
I think in the article, he meant it is not possible(in the API) to separate io intensive tasks and cpu intensive tasks to different pools.
For example, On a 8 core machine, If you submit 8 cpu intensive tasks to the pool then 8 io intensive tasks, io task have to wait cpu intensive tasks to finish(or vice versa). If you could submit them to different pools all 16 operation could run concurrently, because io operation does not costly on the cpu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With