Q59: How do Parallel Streams work and what pitfalls do they present?
Parallel Streams (list.parallelStream()) partition stream workloads into sub-tasks executed concurrently on the shared ForkJoinPool.commonPool().
- Pitfalls:
- Sharing
commonPool()with blocking I/O operations starves other application tasks. - Operations with stateful or non-thread-safe accumulators produce race conditions.
- Overhead for small collections makes parallel processing slower than sequential iterations.
- Sharing
Key Takeaways
- Parallel streams use the shared
ForkJoinPool.commonPool(). - Avoid using parallel streams for blocking I/O tasks or small collections.
Knowledge Check
Which shared thread pool executes parallel stream operations by default?