ProLesson 8.8Advanced3 min readUpdated 30 October 2026
Parallel streams: when not to use them
How parallel streams split work across CPU cores, the common pool behind them, and the many cases where they make code slower or wrong.
Adding .parallel() to a stream looks like free speed: the work is split across your CPU cores. Sometimes it helps a lot. Often it makes things slower, and occasionally it gives wrong answers. Knowing when not to use it is the real skill.
01How it works
ParallelBasics.java
import java.util.stream.LongStream;
public class ParallelBasics {
public static void main(String[] args) {
long n = 50_000_000;
long sequential = LongStream.rangeClosed(1, n).map(x -> x % 7).sum();
long parallel = LongStream.rangeClosed(1, n).parallel().map(x -> x % 7).sum();
System.out.println(sequential == parallel);
System.out.println("Cores available: " + (Runtime.getRuntime().availableProcessors() > 0));
}
}true Cores available: true
A parallel stream splits the source into chunks, processes them on threads from the shared ForkJoinPool.commonPool(), and combines the partial results. The common pool has about one thread per CPU core.
Loading the rest of the lesson…