Parallel Streams

Java-Parallel Streams

Parallel programming

Parallel programming is an important trend in software development in recent years. Parallel programming is the name commonly given to the techniques that take advantage of computers that contain two or more processors (multicore). Today, mostly we all are using multi core computers, such as Intel Core i7,AMD Ryzen 9. The advantage that multiprocessor environments offer is the ability to significantly increase program performance. As a result, there had been a growing need for a mechanism that gives Java programmers a simple, yet effective way to make use of multiple processors in a clean, scalable manner. To answer this need, JDK 7 added several new classes and interfaces that support parallel programming.

One of the benefits that the stream library offers is the ability to easily—and reliably—parallel process certain operations. Parallel processing of a stream is quite simple to request: just use a parallel stream. One way to obtain a parallel stream is to use the parallelStream( ) method defined by Collection. Another way to obtain a parallel stream is to call the parallel( ) method on a sequential stream. The parallel( ) method is defined by BaseStream. It returns a parallel stream based on the sequential stream that invokes it. (If it is called on a stream that is already parallel, then the invoking stream is returned.) For example:

Parallel Streams

Understand, of course, that even with a parallel stream, parallelism will be achieved only if the environment supports it. The parallel stream enhances multithreaded programming in two important ways. First, each operation of parallel stream are executed by multiple threads that are automatically created. Second, it automatically makes use of multiple processors. In other words, by using the parallel stream you enable your applications to automatically scale to make use of the number of available processors. The following image shows how Sequential and Parallel Streams running in 4 cores:

Parallel Streams

You can switch a parallel stream to sequential by calling the sequential( ) method, which is specified by BaseStream. In general, a stream can be switched between parallel and sequential on an as-needed basis. Once a parallel stream has been obtained, operations on the stream can occur in parallel, assuming that parallelism is supported by the environment. For example, in the following program, the second reduce( ) operation is parallelized by substituting parallelStream( ) to stream( ). The results will be the same, but the multiplications can occur in different threads.

Program

proParallel Streams


Program Source

import java.util.ArrayList;

public class Javaapp {

    public static void main(String[] args) {

        ArrayList<Integer> arylist = new ArrayList<Integer>();
        arylist.add(2);
        arylist.add(4);
        arylist.add(6);
        arylist.add(8);
        arylist.add(10);
        arylist.add(12);
        arylist.add(14);
   
        System.out.println("Sequential Stream->Returns->"+arylist.stream().reduce((a,b)->{
            System.out.println("Sequential Stream->"+Thread.currentThread().getName());                                                                     
            return a*b;}).get());
        
        System.out.println();
        
        System.out.println("Parallel Stream->Returns->"+arylist.parallelStream().reduce((a,b)->{
            System.out.println("Parallel Stream->"+Thread.currentThread().getName());                                                                    
            return a*b;}).get());
    }
}

Leave a Comment