Java Stream vs Parallel Stream: When to Use Each
Understand the tradeoffs between sequential and parallel streams in Java, including performance, shared state, ordering, and when to use each.

The choice between a sequential stream and a parallel stream in Java is not just about performance; it's about understanding how the JVM splits work and what that means for correctness. A parallel stream can speed up a CPU-bound operation on a large collection, but it can also introduce subtle bugs and even degrade performance when used carelessly. This article explains what actually changes when you call .parallel(), when it helps, and when it hurts.
What Parallel Streams Actually Change
A sequential stream processes elements one at a time, in the order they appear in the source. A parallel stream, on the other hand, splits the source into multiple chunks, processes each chunk on a separate thread, and then combines the results. The Stream API handles this internally using a ForkJoinPool.
Consider a simple operation:
List<Integer> numbers = IntStream.rangeClosed(1, 1_000_000)
.boxed()
.collect(Collectors.toList());
long sum = numbers.stream()
.mapToInt(Integer::intValue)
.sum();To make it parallel, you simply add .parallel():
long sum = numbers.parallelStream()
.mapToInt(Integer::intValue)
.sum();The parallelStream() method is shorthand for stream().parallel(). The key difference is that the latter can be called at any point in the pipeline, but the behavior is the same: the entire pipeline becomes parallel.
The Cost of Parallelism: Splitting and Merging
Parallelism does not come for free. The JVM must split the source into subparts, distribute them across threads, and then merge the results. This overhead is significant for small collections or operations that are already fast.
The splitting process depends on the source. An ArrayList splits efficiently because it has a known size and random access. A LinkedList splits poorly because the splitter must traverse the list to find a midpoint. A HashSet splits reasonably well but not as predictably as an array-based list.
The merging step also adds cost. For operations like reduce or collect, the JVM must combine partial results. If the combining operation is cheap, the overhead is minimal. But if the combiner is expensive—for example, merging large maps—the parallel version can end up slower than the sequential one.
A common mistake is assuming that parallel streams automatically improve throughput. In practice, the overhead of splitting and merging can dominate the actual work, especially when the operation is trivial. For example, mapping an integer to its square is so fast that the parallel version often loses to the sequential version on a typical machine.
When Parallel Streams Make Sense
Parallel streams shine when all of the following conditions hold:
- The data source is large (thousands of elements or more).
- The operation is CPU-bound and takes meaningful time per element.
- The operation is stateless and does not depend on the order of elements.
- The result can be combined efficiently.
A typical example is a heavy computation on each element, such as parsing complex documents or calculating cryptographic hashes.
List<byte[]> documents = loadDocuments();
List<byte[]> hashes = documents.parallelStream()
.map(doc -> sha256(doc))
.collect(Collectors.toList());Here, sha256 is CPU-intensive and each call is independent. The parallel stream can distribute the work across available cores, and the final collection is straightforward.
Another good candidate is a reduce operation where the accumulator is associative and the combiner is cheap:
int sum = numbers.parallelStream()
.reduce(0, Integer::sum);The Integer::sum combiner is trivial, so the merge overhead is negligible.
When Parallel Streams Hurt: Shared State and Ordering
Parallel streams are dangerous when the operation touches shared mutable state. Because multiple threads execute the lambda concurrently, any shared variable can become a race condition.
Consider this example:
List<Integer> numbers = ...;
Map<Integer, Integer> counts = new HashMap<>();
numbers.parallelStream()
.forEach(n -> counts.merge(n % 10, 1, Integer::sum));This is broken. Multiple threads may call merge on the same HashMap concurrently, leading to corrupted state or lost updates. Switching to ConcurrentHashMap would make each merge call atomic, but mutating shared state from a parallel stream is still fragile and harder to reason about than a reduction. Prefer collect with a collector designed for the aggregation, or use an associative, stateless reduction.
Ordering is another subtle issue. A parallel stream does not automatically process elements in encounter order; whether the result preserves that order depends on the source and the terminal operation. Terminal operations such as forEach may work on elements as chunks complete. If your result depends on encounter order, use ordered operations such as forEachOrdered, or collect into a collection that preserves order. findFirst() is intentionally ordered: on a parallel stream over an ordered source, it still returns the first matching element in encounter order, though it may cost extra coordination. If you only need any matching element, findAny() gives the implementation more freedom and can be faster.
Choosing Between Sequential and Parallel Streams
The decision should be based on measurable criteria, not intuition. Here is a practical guideline:
| Condition | Sequential | Parallel |
|---|---|---|
| Small collection (< 10,000 elements) | Yes | No |
| CPU-bound, heavy per-element work | Maybe | Yes |
| I/O-bound (network, disk) | Yes | No |
| Shared mutable state | Yes | No |
| Order-sensitive result | Yes | No |
| Large, independent elements | Maybe | Yes |
These are rough thresholds; the actual crossover point depends on your hardware and the cost of the operation. The only reliable way to decide is to benchmark both versions with your actual workload and data size.
A common mistake is using a parallel stream for I/O-bound operations, such as reading files or making HTTP requests. Parallel streams use the common ForkJoinPool, which is sized based on the number of CPU cores, not the number of concurrent I/O tasks. If you block on I/O, you tie up threads that could be doing CPU work, and you may exhaust the pool. For I/O-bound work, consider using a dedicated executor with more threads, or use asynchronous APIs.
Another mistake is assuming that parallel streams are always faster. The overhead of splitting and merging can be higher than the time saved, especially for small data sets. For example, summing a list of 100 integers with a parallel stream will almost certainly be slower than the sequential version because the cost of spawning tasks and merging results exceeds the cost of adding 100 numbers.
Controlling the ForkJoinPool Behind Parallel Streams
By default, parallel streams execute on the common ForkJoinPool, which has a target parallelism usually equal to the number of available processors minus one (with a minimum of one). You can change this globally with the system property java.util.concurrent.ForkJoinPool.common.parallelism, but that affects all parallel streams in the application, which is rarely desirable.
A more controlled approach is to submit the parallel stream to a custom ForkJoinPool. This is not a documented Stream API feature, but it works because parallel streams internally use ForkJoinTask and will run within the current ForkJoinPool if one is active. The technique is to wrap the stream in a ForkJoinPool.submit call:
ForkJoinPool customPool = new ForkJoinPool(4);
try {
customPool.submit(() ->
numbers.parallelStream()
.map(expensiveOperation)
.collect(Collectors.toList())
).get();
} catch (InterruptedException | ExecutionException e) {
// handle
} finally {
customPool.shutdown();
}This gives you control over the number of threads and isolates the parallel stream from the common pool. However, this is an implementation detail and may change in future Java versions. It also introduces complexity, so use it only when you have a specific need, such as avoiding starvation of the common pool.
Another consideration is the Spliterator characteristics. The performance of a parallel stream depends heavily on how well the source can be split. Arrays and ArrayList have good spliterators that support SUBSIZED and SIZED, allowing efficient splitting. A LinkedList has a spliterator that splits by traversing, which is O(n) per split and can negate the benefits of parallelism. If you need parallelism, prefer array-based collections or use a custom spliterator.
Finally, remember that parallel streams are not a silver bullet. They are a convenience for CPU-bound, stateless operations on large data sets. For everything else, sequential streams are simpler and safer. When in doubt, measure with a proper microbenchmark like JMH, and only use parallel streams when they demonstrably improve throughput without sacrificing correctness.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.