Introduction to Java 8 Parallel Stream — Java2Blog

Introduction to Java 8 Parallel Stream — Java2BlogArpit MandliyaBlockedUnblockFollowFollowingApr 22In this post, we will see about Parallel Stream in java.

Java 8 introduces the concept of the parallel stream to do parallel processing.

As we have a number of CPU cores nowadays due to cheap hardware costs, parallel processing can be used to perform operation faster.

Let’s understand with the help of a simple examplepackage org.

arpit.

java2blog.

java8;import java.

util.

Arrays;import java.

util.

stream.

IntStream;public class Java8ParallelStreamMain { public static void main(String[] args) { System.

out.

println("================================="); System.

out.

println("Using Sequential Stream"); System.

out.

println("================================="); int[] array= {1,2,3,4,5,6,7,8,9,10}; IntStream intArrStream=Arrays.

stream(array); intArrStream.

forEach(s-> { System.

out.

println(s+" "+Thread.

currentThread().

getName()); } ); System.

out.

println("================================="); System.

out.

println("Using Parallel Stream"); System.

out.

println("================================="); IntStream intParallelStream=Arrays.

stream(array).

parallel(); intParallelStream.

forEach(s-> { System.

out.

println(s+" "+Thread.

currentThread().

getName()); } ); }}When you run the above program, you will get below output=================================Using Sequential Stream=================================1 main2 main3 main4 main5 main6 main7 main8 main9 main10 main=================================Using Parallel Stream=================================7 main6 ForkJoinPool.

commonPool-worker-33 ForkJoinPool.

commonPool-worker-19 ForkJoinPool.

commonPool-worker-22 ForkJoinPool.

commonPool-worker-35 ForkJoinPool.

commonPool-worker-110 ForkJoinPool.

commonPool-worker-21 ForkJoinPool.

commonPool-worker-38 ForkJoinPool.

commonPool-worker-24 ForkJoinPool.

commonPool-worker-1If you notice the output, the main thread is doing all the work in case of the sequential stream.

It waits for current iteration to complete and then work on the next iteration.

In the case of Parallel stream,4 threads are spawned simultaneously and it internally using Fork and Join pool to create and manage threads.

Parallel streams create ForkJoinPool instance via static ForkJoinPool.

commonPool() method.

Parallel Stream takes benefits of all available CPU cores and processes the tasks in parallel.

If the number of tasks exceeds the number of cores, then remaining tasks wait for currently running task to complete.

Parallel Streams are cool, so should you use it always?A big No!!It is easy to convert sequential Stream to parallel Stream just by adding .

parallel, does not mean you should always use it.

There are lots of factors you need to consider while using parallel streams otherwise you will suffer from negative impacts of parallel Streams.

Parallel Stream has much higher overhead than sequential Stream and it takes a good amount of time to coordinate between threads.

You need to consider parallel Stream if and only if:You have a large dataset to process.

As you know that Java uses ForkJoinPool to achieve parallelism, ForkJoinPool forks sources stream and submit for execution, so your source stream should be splittable.

For example:ArrayList is very easy to split, as we can find a middle element by its index and split it but LinkedList is very hard to split and does not perform very well in most of the cases.

You are actually suffering from performance issues.

You need to make sure that all the shared resources between threads need to be synchronized properly otherwise it might produce unexpected results.

The simplest formula for measuring parallelism is “NQ” model as provided by Brian Goetz in his presentation.

NQ Model:N x Q >10000where,N = number of items in the datasetQ = amount of work per itemIt means if you have a large number of datasets and less work per item(For example: Sum), parallelism might help you run program faster and vice versa is also true.

So if you have less number of datasets and more work per item(doing some computational work), then also parallelism might help you in achieving results faster.

Let’s see with the help of another example.

In this example, we are going to see how CPU behaves when you perform long computations in case of parallel Stream and sequential stream.

We are doing some arbit calculations to make the CPU busy.

package org.

arpit.

java2blog.

java8;import java.

util.

ArrayList;import java.

util.

List;public class PerformanceComparisonMain { public static void main(String[] args) { long currentTime=System.

currentTimeMillis(); List<Integer> data=new ArrayList<Integer>(); for (int i = 0; i < 100000; i++) { data.

add(i); } long sum=data.

stream() .

map(i ->(int)Math.

sqrt(i)) .

map(number->performComputation(number)) .

reduce(0,Integer::sum); System.

out.

println(sum); long endTime=System.

currentTimeMillis(); System.

out.

println("Time taken to complete:"+(endTime-currentTime)/(1000*60)+" minutes"); } public static int performComputation(int number) { int sum=0; for (int i = 1; i < 1000000; i++) { int div=(number/i); sum+=div; } return sum; }}When you run the above program, you will get below output.

117612733Time taken to complete:6 minutesBut we are not interested in output here, but how CPU behaved when the above operation performed.

As you can see CPU is not fully utilized in case of Sequential Stream.

Let’s change at 16 lines no.

and make the stream parallel and run the program again.

long sum=data.

stream() .

parallel() .

map(i ->(int)Math.

sqrt(i)) .

map(number->performComputation(number)) .

reduce(0,Integer::sum);You will get below output when you run Stream in parallel.

117612733Time taken to complete:3 minutesLet’s check CPU history when we ran the program using a parallel stream.

As you can see parallel stream used all 4 CPU cores to perform computation.

That’s all about the parallel stream in java.

You may also like following Java 8 tutorialsJava 8 tutorialJava 8 interview questionsLambda expression in Java 8Core java tutorial5 Books to Learn Java 8 from Scratch5 Free Courses to Learn Java 8 and beyondOriginally published at https://java2blog.

com on April 23, 2019.

.

. More details

Leave a Reply