Parallel streams
parallel() turns a pipeline into one that may split its work across the cores of the machine, and the rest of the code does not change. That is the promise and the trap at once: the split is free to look at and the speed-up is not free to get, and a pipeline that was correct with one thread can be wrong with several. This lesson measures both sides in this container.
Measured here
Two sources of two million numbers: one where every element costs a hundred arithmetic steps, and one where the body is a plain sum. Each is timed five times and the best run is printed, after one warm-up run that the JIT needs:
import java.util.function.Supplier;
import java.util.stream.IntStream;
public class Measured {
static final int N = 2_000_000;
public static void main(String[] args) {
System.out.println("cores: " + Runtime.getRuntime().availableProcessors());
long seq = best(() -> IntStream.range(0, N).map(Measured::work).asLongStream().sum());
long par = best(() -> IntStream.range(0, N).parallel().map(Measured::work).asLongStream().sum());
System.out.printf("heavy body sequential %.1f ms%n", seq / 1_000_000.0);
System.out.printf("heavy body parallel %.1f ms%n", par / 1_000_000.0);
long cheapSeq = best(() -> IntStream.range(0, N).asLongStream().sum());
long cheapPar = best(() -> IntStream.range(0, N).parallel().asLongStream().sum());
System.out.printf("cheap body sequential %.1f ms%n", cheapSeq / 1_000_000.0);
System.out.printf("cheap body parallel %.1f ms%n", cheapPar / 1_000_000.0);
long a = IntStream.range(0, 1000).map(Measured::work).asLongStream().sum();
long b = IntStream.range(0, 1000).parallel().map(Measured::work).asLongStream().sum();
System.out.println("same answer: " + (a == b));
}
static long best(Supplier<Long> task) {
task.get();
long best = Long.MAX_VALUE;
for (int round = 0; round < 5; round++) {
long start = System.nanoTime();
task.get();
best = Math.min(best, System.nanoTime() - start);
}
return best;
}
static int work(int n) {
int x = n;
for (int i = 0; i < 100; i++) {
x = x * 31 + 7;
}
return x & 0xffff;
}
}
javac -Xlint:all Measured.java
java Measured
cores: 4
heavy body sequential 195.9 ms
heavy body parallel 65.1 ms
cheap body sequential 7.2 ms
cheap body parallel 3.8 ms
same answer: true
Reading the numbers
The heavy body is three times faster in parallel on four cores, which is close to the limit the hardware allows: a speed-up of four would mean no overhead at all. The cheap body gains less than a factor of two, because splitting the source and merging the partial sums is a fixed cost that a fast body cannot hide.
These are one container's numbers and they change with the machine, the load and the JDK, so the shape is what to keep: the more work per element, the more of the hardware the parallel run can use. The last line is the other half of the contract: both runs compute the same answer, because the pipeline is a reduction and the JDK combines the partial results.
The shared list that loses elements
A pipeline is safe in parallel only when each element is processed without touching shared mutable state. Add to one ArrayList from a parallel forEach and the adds race: several threads write the same slot, and the list ends up short. The same program counts the elements in parallel and gets the exact answer:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class Race {
public static void main(String[] args) {
for (int attempt = 0; attempt < 3; attempt++) {
List<Integer> shared = new ArrayList<>();
IntStream.range(0, 10_000).parallel().forEach(shared::add);
System.out.println("shared size: " + shared.size());
}
long count = IntStream.range(0, 10_000).parallel().count();
System.out.println("count: " + count);
}
}
javac -Xlint:all Race.java
java Race
shared size: 3312
shared size: 8359
shared size: 6511
count: 10000
Three sizes for the same ten thousand elements, and no exception: the list lost most of them in silence. That is the worst kind of failure, because a program that checks the total once in a while will see something plausible. count is exact because it is a reduction the stream combines itself, with no shared object.
The fix is never a synchronised list: it is a collector that joins the partial results, which is what collect is for, or a collection built per thread and merged at the end. A Collectors.toList in a parallel stream is safe by construction, and a manual forEach that writes to a field is not.
When parallel pays
Four conditions, and a pipeline that fails one of them is slower or wrong. The work per element must be large enough to hide the split and the merge. The source must be splittable, which an array, a range and an ArrayList are and a LinkedList or a stream from an iterator is not. The operations must be stateless, with no captured variable that changes. And the result must not depend on the order unless the pipeline restores it, which is what ordered terminal operations do.
The default pool is the common ForkJoinPool, shared by every parallel stream in the program, so one heavy pipeline slows every other one. A server that serves requests in parallel streams also competes with its own request threads, which is why the threads of the concurrency part ahead are the tool for that, not a parallel stream. When a pipeline passes the four conditions and the measurement agrees, the change is one call; when it fails one, the call is a bug.
Exercises
- Change the cheap body to a filter that keeps half the elements and compare the two times again.
- Replace the shared list of Race with collect(Collectors.toList()) and print the size.
- Time List.of against ArrayList as the source of a parallel sum and explain the difference.
- Add a captured counter that every element increments and run the pipeline both ways.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
parallel()change about a pipeline?Answer
The stream may split its work across threads, with the same steps
The pipeline is the same description; only who runs it changes.
-
Why is a shared ArrayList a bad target for a parallel forEach?
Answer
Several threads add at once and lose elements
add is not atomic, so two threads write the same slot and the size falls short.
-
Which source splits well for a parallel stream?
Answer
An ArrayList or an array
A random-access source can be halved cheaply, which is what the split needs.
-
What does this program print?
import java.util.stream.IntStream; public class Q1 { public static void main(String[] args) { long seq = IntStream.range(0, 100).asLongStream().sum(); long par = IntStream.range(0, 100).parallel().asLongStream().sum(); System.out.println(seq + " " + par); } }Answer
4950 4950
Both forms sum the same hundred numbers, and the parallel one merges partial sums.
-
What does this program print?
import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; public class Q2 { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); IntStream.range(0, 100).parallel().forEach(list::add); System.out.println(list.size() <= 100); } }Answer
true
The size is at most a hundred because the race can only lose elements, never add them.
-
What does this program print?
import java.util.List; import java.util.stream.Collectors; public class Q3 { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4); List<Integer> doubled = list.parallelStream() .map(n -> n * 2) .collect(Collectors.toList()); System.out.println(doubled); } }Answer
[2, 4, 6, 8]
collect on an ordered stream keeps the encounter order, whatever the threads did.
-
What does this program print?
import java.util.stream.IntStream; public class Q4 { public static void main(String[] args) { System.out.println(IntStream.range(0, 10).parallel().count()); System.out.println(IntStream.range(0, 10).parallel().max().orElse(-1)); } }Answer
10 / 9
The range holds ten values from zero to nine, and the greatest is nine.
-
When is a parallel stream slower than the sequential one?
Answer
When the work per element is too small to hide the split and the merge
The fixed cost of parallelism has to be earned back per element.
-
Which pool runs a parallel stream by default?
Answer
The common ForkJoinPool of the process
The common pool is shared, which is why a heavy pipeline affects the others.
-
What is the safe way to build a list from a parallel stream?
Answer
collect(Collectors.toList()), which merges partial results
The collector combines per-thread results instead of sharing one list.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev