29 August 2019

Java 8 parallel streams pitfall

1.
All parallel streams use a common fork-join thread pool, and if you submit a long-running task, you effectively block all threads in the pool. Consequently, you block all other tasks that are using parallel streams.

2.
The default processing that occurs in such a Stream uses the ForkJoinPool.commonPool(), a Thread Pool shared by the entire application.

3.
This way we essentially force the parallel stream to have a ForkJoin as a parent thread, instead of a regular thread, so ForkJoinPool.commonPool is not used. Hooray!

My Blog List

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.