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:
    1. Sharing commonPool() with blocking I/O operations starves other application tasks.
    2. Operations with stateful or non-thread-safe accumulators produce race conditions.
    3. Overhead for small collections makes parallel processing slower than sequential iterations.
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?