Creating Parallel Streams
parallel() and parallelStream().
Creating Parallel Streams is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Parallel Streams Do
A parallel stream splits its data and processes chunks on multiple threads, then combines results. The goal is faster throughput on large workloads.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sum = IntStream.rangeClosed(1, 1000)
.parallel()
.sum();
System.out.println(sum);
}
}parallel() on an Existing Stream
Calling parallel() turns a sequential stream into a parallel one. It can appear anywhere in the pipeline.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3, 4, 5);
int total = nums.stream()
.parallel()
.mapToInt(Integer::intValue)
.sum();
System.out.println(total);
}
}parallelStream() on Collections
Collection.parallelStream() creates a parallel stream directly, a shortcut over stream().parallel().
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("a", "bb", "ccc");
int chars = words.parallelStream()
.mapToInt(String::length)
.sum();
System.out.println(chars);
}
}Back to Sequential
sequential() reverts a stream to single-threaded execution. The last call to parallel() or sequential() wins for the whole pipeline.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sum = IntStream.rangeClosed(1, 100)
.parallel()
.sequential()
.sum();
System.out.println(sum);
}
}Checking isParallel
isParallel() reports whether a stream will execute in parallel.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
System.out.println(IntStream.range(0, 10).isParallel());
System.out.println(IntStream.range(0, 10).parallel().isParallel());
}
}Same Result, Different Execution
For associative operations like sum, parallel and sequential streams return the same result. Only the execution strategy differs.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int seq = IntStream.rangeClosed(1, 50).sum();
int par = IntStream.rangeClosed(1, 50).parallel().sum();
System.out.println(seq == par);
}
}The Common ForkJoinPool
Parallel streams use the shared ForkJoinPool.commonPool(). Its default size is the number of available processors minus one.
public class Main {
public static void main(String[] args) {
System.out.println(Runtime.getRuntime().availableProcessors());
}
}Order of Threads Is Not Guaranteed
With forEach, parallel streams may print in any order because chunks finish at different times. Use forEachOrdered if order matters.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.rangeClosed(1, 5)
.parallel()
.forEachOrdered(System.out::println);
}
}Reduction Combines Partial Results
Under the hood, parallel reduction computes partial results per chunk and merges them with the combiner. The operation must be associative for correctness.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int product = IntStream.rangeClosed(1, 6)
.parallel()
.reduce(1, (a, b) -> a * b);
System.out.println(product);
}
}Collecting in Parallel
The collector framework supports parallel collection by merging partial containers. groupingBy works correctly in parallel.
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("apple", "banana", "avocado", "cherry");
Map<Character, List<String>> byFirst = words.parallelStream()
.collect(Collectors.groupingBy(w -> w.charAt(0)));
System.out.println(byFirst);
}
}Not Every Stream Becomes Faster
Switching on parallelism is trivial, but it is not always beneficial. Small datasets or cheap operations can be slower in parallel due to splitting and merging overhead.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long count = IntStream.rangeClosed(1, 10)
.parallel()
.filter(n -> n % 2 == 0)
.count();
System.out.println(count);
}
}Quick Check
Which thread pool executes parallel streams by default?
Recap
You created parallel streams:
parallel()converts any stream;parallelStream()is a collection shortcut.sequential()reverts; the last call wins.- They run on the shared
ForkJoinPool.commonPool(). - Results match sequential for associative operations, but order and speed are not guaranteed.
Frequently asked questions
Is the “Creating Parallel Streams” lesson free?
Yes — the full text of “Creating Parallel Streams” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Creating Parallel Streams”?
parallel() and parallelStream(). You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating Parallel Streams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating Parallel Streams
- When Parallelism Helps
- Thread Safety and Side Effects
- Common Pitfalls