When are parallel streams worth using in Java, and what makes them pay off?
They are worth it when there is a lot of data, the work is heavy on the processor, and the items are independent of each other.
All three of those conditions have to hold. If the data is small, the cost of setting up the parallel work is more than the work itself. If the work is mostly waiting, such as reading from a network, then more threads do not make it faster. And if the items depend on each other, then the work cannot be split up.
public class Main {
public static void main(String[] args) {
int count = 5_000_000;
// The same work, done one way and then the other.
long start = System.nanoTime();
long sequential = 0;
for (int index = 0; index < count; index++) {
sequential = sequential + (index % 7);
}
long sequentialTime = System.nanoTime() - start;
start = System.nanoTime();
long parallel = java.util.stream.IntStream.range(0, count)
.parallel()
.mapToLong(index -> index % 7)
.sum();
long parallelTime = System.nanoTime() - start;
System.out.println("the same answer : " + (sequential == parallel));
System.out.println("sequential : " + (sequentialTime / 1000000) + " ms");
System.out.println("parallel : " + (parallelTime / 1000000) + " ms");
}
}
So what makes them pay off? The usual rules are these. The data has to be big enough that the work is worth the setup, which usually means thousands of items or more. The work has to be on the processor rather than on the network or the disk. The items have to be independent, so that the work can be split without any coordination. And the data should already be in memory, because a parallel stream over data that has to be read in is limited by the reading.
The next question in this topic looks at how to make one and what it does to the order.