X
    Categories: Java Core

Java Parallel Streams Example

Stream API allows developers to  take advatange of multi core architectures and improve the performance of Java program by  creating parallel streams and making your program perform operations faster.

There are two ways of creating a parallel stream:

  • by using the parallelStream() method
  • by using the parallel() method

When you are using parallel streams, effectively they use more CPU power which makes the whole output or processing if you will, faster overall. Way faster! Also, if you are not using parallel streams for big computations, your program will be only using 1 core out of, let’s say, 16 cores. How inefficient!

Order

Parallel streams divide the problem into small subsets (or subproblems) which they solve simultaneously (unlike sequential order where they execute each small problem one by one). After the subproblems have been computed, all the solutions to them are combined.



How to implement

Java allows you to implement Parallel streams by using aggregate operations like map for example.

import java.util.ArrayList;
import java.util.List;
 
public class Main {
    
    public static int compute(int n) {
        for (int i = 0; i < 10000; i++) {
            n = n + i;
        }
        return n;
    }

    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            arrayList.add(i*6);
        }

        System.out.println("Added all the numbers to the arraylist.");
        
        System.out.println("Starting to add numbers...");

        int result = arrayList.parallelStream().map(i -> compute(i)).reduce(Integer::sum).get();
        
        System.out.println(reuslt);
    }
}

In this example, we have an example method that just loops 10000 times and adds i to n. In the end, we simply return n. In our main function, we create an ArrayList that contains integers. Then we create an enormous loop, that loops 1000000 times and we are adding in each incrementation  i*6  to the arraylist. After we’ve added it, we have these two print statements that just point out that this big loop has finished.

Below these print statements, is the fun part. More specifically, it is where we are creating a parallel stream using the map function. We are calling parallelStream() on the arraylist and for each i in the arraylist, we are calling the compute function and passing the arraylist’s element as a parameter. In the end, we are combining all the results.

When should you use Parallel streams

  • when there are massive amounts of data to process
  • if sequential approach is costing you something
  • N(number of elements) Q(cost per element) should be large
4 2 votes
Article Rating
ym_coding:
Related Post