GotchaDifficulty: Advanced1 min

Why parallel() Made It Slower

One word added to a pipeline that was taking too long. The machine has eight cores, the work is now spread across all of them, and the whole thing takes longer than it did.

java
long total = invoices.parallelStream()
        .mapToLong(Invoice::pence)
        .sum();

Going parallel costs something before it saves anything: the source has to be split, tasks have to be handed to a thread pool, and the partial results have to be combined. On a few thousand cheap operations that overhead is most of the time.

What parallel needs in order to pay for itself
NeedsWhy
A large sourceThousands of elements at least, usually many more
Expensive work per elementCheap work is dominated by the splitting
A source that splits evenlyAn ArrayList does; a LinkedList and a file do not
No shared mutable stateOtherwise the result is wrong rather than slow

Every parallel stream also shares one pool by default, so a slow parallel pipeline in one part of an application delays every other one. Measure before adding it, and measure again after.