Java 8 Parallel Streams Example

By Dhiraj Ray, 24 May,2017  
561

When it comes to stream in Java 8, there are 2 different types of stream operation available. They are serial stream and parallel stream. In this article we will be discussing about parallel stream in java 8. We will look into different examples of using parallel stream, differences between them, performance improvement with parallel stream and also discuss about some aspects of using parallel stream.

What is ParalelStream

Parallel stream enables parallel computing that involves processing elements concurrently in parallel with each element in a seperate thread. But this does not guarantee high performance and faster execution everytime. It again depends on the number of CPU cores available.

Parallelstream has a much higher overhead compared to a sequential one.Parallel stream should be used only when there is a huge amount of elements to process and there is a performance problem with processing these elements. Also the parallel stream operations should be independent.

Creating Parallel Streams

We can create a parallel stream from existing stream by using parallel(). Following is an example:

Stream stream = Stream.of("John", "Mike", "Ryan","Donald", "Matthew"); Stream parallelStream = stream.parallel();

Another way to create parallelstream is using parallelStream() method.

Stream parallelStream = Arrays.asList("John", "Mike", "Ryan","Donald", "Matthew").parallelStream();

Parallel Stream Example

Following is an example using parallel stream that counts number of prime numbers between 1 and 50.Stream of numbers is created by a range method and then the stream is converted to parallel stream.

public void count() { final long count = IntStream.range(1, 50) .parallel() .filter(number -> isPrime(number)).count(); System.out.println("Count - " + count); } public boolean isPrime(final int number) { return number > 1 && IntStream.rangeClosed(2, (int) Math.sqrt(number)).noneMatch( divisor -> number % divisor == 0); }

Output : Count - 15

Serial vs Parallel Stream Processing

If we use serial stream the order is guaranted as below:

Stream.of("John", "Mike", "Ryan","Donald", "Matthew").forEach(System.out::println);

Output:

John Mike Ryan Donald Matthew

Whereas, the order is not guaranted while using parallel stream.

Stream.of("John", "Mike", "Ryan","Donald", "Matthew").parallel().forEach(System.out::println);

Output:

Ryan Mike Matthew John Donald

Perform Check with Parallel Stream Processing

Parallel stream providdes significant performance improvement for larger streams. Following is an example that processes large streams and compares the time difference between processing time of sequential stream and parallel stream.

ParallelStreamPerformanceCheck.java
package com.devglan; import java.util.ArrayList; import java.util.List; /** * @author only2dhir * */ public class ParallelStreamPerformanceCheck { public static void main(String[] args) { // TODO Auto-generated method stub List numList = new ArrayList(); for (int i = 0; i < 1000; i++) { numList.add(i); } // Processing sequentially long startTime = System.currentTimeMillis(); numList.stream().forEach(i -> processData(i)); long endTime = System.currentTimeMillis(); double sequentialStreamTimetaken = (endTime - startTime) / 1000; System.out.println("Time required with stream() : " + sequentialStreamTimetaken); // Parallel processing startTime = System.currentTimeMillis(); numList.parallelStream().forEach(i -> processData(i)); endTime = System.currentTimeMillis(); long parallelStreamTimetaken = (endTime - startTime) / 1000; System.out.println("Time required with parallelStream() : " + parallelStreamTimetaken); System.out.println("Differential time : " + (sequentialStreamTimetaken - parallelStreamTimetaken)); } private void processData(int num) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Output
Time required with stream() : 11.0 Time required with parallelStream() : 2 Differential time : 9.0

Conclusion

I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

References

Parallel Stream

Parallelism

Parallel Stream

Suggest more topics in suggestion section or write your own article and share with your colleagues.

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.

Further Reading: