Video in progress

When does parallelizing a divide and conquer algorithm with ForkJoinPool or a parallel stream give no benefit, or even do harm?

When the work is small, the cost of splitting and combining is larger than the gain. Parallelism is useless or dangerous when the data source splits poorly (LinkedList, Stream.iterate), when memory bandwidth is the limit, when there is blocking I/O inside, or when everything runs in the shared ForkJoinPool.commonPool(). In a service, request-level parallelism already exists.

📖 Answer

Amdahl's law: if the serial part is 10%, even with unlimited cores you can't go more than 10 times faster. In divide and conquer, the serial part is the splitting, the combining of results (merge), and thread synchronization.

When it doesn't help.

When N is small or the operation is cheap. Summing 10 thousand elements is often slower in parallel. A rough rule: unless N × the cost of one element exceeds about 100 microseconds, don't think about parallelism. Measure with JMH.

When the source splits badly. ArrayList, arrays and IntStream.range have a good Spliterator. LinkedList, Stream.iterate and BufferedReader.lines() need a sequential walk to split.

When memory is the bottleneck. Summing an array is limited by memory bandwidth, not CPU. With 4 cores you get ~1.5-2x, and with 16 cores about the same.

For order-dependent operations. On an ordered stream, limit, findFirst and sorted are expensive in parallel. unordered() or findAny helps if you don't truly need the order.

The shared pool. Parallel streams run on ForkJoinPool.commonPool(), whose size is the core count − 1. If a parallel stream inside one endpoint makes an HTTP call, the other parallel streams across the whole application are blocked too. I've seen this incident on prod: since CompletableFutures use the same pool, everything froze.

Be careful inside a server as well. While Tomcat is running 200 requests in parallel, grabbing 8 cores inside a single request slows down the others. Total throughput doesn't grow, only the latency distribution changes. Parallelism makes sense in batch jobs and in rare, heavy requests.

When it helps: CPU-heavy computation over large arrays held in memory (image processing, simulation, large sorts: Arrays.parallelSort), with a dedicated ForkJoinPool, confirmed by a JMH measurement.

For parallel tasks that do I/O, virtual threads in Java 21 are the right tool, not ForkJoin.