Parallel streaming in Java 8

Java Fusion
2 min readFeb 2, 2024

--

Parallel streaming in Java 8

In Java 8, parallel streaming allows you to parallelize the processing of streams. parallel streaming refers to the ability to process elements of a stream concurrently, leveraging multiple threads to improve the performance of processors.

This is achieved by invoking the stream().parallel() or .parallelStream() method on a stream.

list.parallelStream() or list.stream().parallel()

Here’s a simple example demonstrating parallel streaming in Java 8

package com.streamapi;

import java.util.Arrays;
import java.util.List;

public class ParallelStreamingDemo {

public void pStream() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

System.out.println("***** Example of normal stream ********");
list.stream()
.map(num -> num * 5)
.forEach(n -> System.out.println("Value ->" + n + " Thread name ->" + Thread.currentThread().getName()));

System.out.println("***** Example of stream parallel() ********");
list.parallelStream()
.map(num -> num * 5)
.forEach(n -> System.out.println("Value ->" + n + " Thread name ->" + Thread.currentThread().getName()));

System.out.println("***** Example of stream parallel() ********");
list.stream().parallel()
.map(num -> num * 5)
.forEach(n -> System.out.println("Value ->" + n + " Thread name ->" + Thread.currentThread().getName()));
}

public static void main(String[] args) {
System.out.println("This is parallel stream example");
ParallelStreamingDemo pd = new ParallelStreamingDemo();
pd.pStream();
}
}

In the above example, we have a list of integers, and we demonstrate both sequential and parallel streams. The stream() method creates a sequential stream, and the parallelStream() method creates a parallel stream.

When you run this program, you may notice that the order of output can be different for the parallel stream due to the parallel processing.

Output

***** Example of normal stream ********
Value ->5 Thread name ->main
Value ->10 Thread name ->main
Value ->15 Thread name ->main
Value ->20 Thread name ->main
Value ->25 Thread name ->main
***** Example of stream parallel() ********
Value ->15 Thread name ->main
Value ->25 Thread name ->main
Value ->20 Thread name ->main
Value ->5 Thread name ->ForkJoinPool.commonPool-worker-2
Value ->10 Thread name ->ForkJoinPool.commonPool-worker-1
***** Example of stream parallel() ********
Value ->15 Thread name ->main
Value ->10 Thread name ->ForkJoinPool.commonPool-worker-4
Value ->25 Thread name ->ForkJoinPool.commonPool-worker-1
Value ->5 Thread name ->ForkJoinPool.commonPool-worker-2
Value ->20 Thread name ->ForkJoinPool.commonPool-worker-3

Note:

Keep in mind that not all operations are suitable for parallelization, and using parallel streams doesn’t always lead to better performance. It depends on various factors such as the size of the data set, the complexity of the operations being performed, and the hardware resources available.

Thank you

--

--

Java Fusion

Java enthusiast crafting code with passion. enthusiastic about creating robust, scalable applications. Let's code, innovate, and build the future together!