Javatpoint Logo
Javatpoint Logo

Java Parallel Stream Example

The parallel stream was introduced in Java 8 or later versions. It is a part of functional programming. Using the feature of parallel stream, we can use multiple cores of the processor. Any stream in Java can be easily transformed from sequential to parallel stream. In this section, we will discuss what is a parallel stream in Java and ways to implement a parallel stream in Java.

What is a parallel stream in Java?

Usually, Java code has one stream of processing that executes in sequence. On the other hand, by using the parallel stream, we can divide chunks of code into streams that execute in parallel on multiple cores. The final result is the grouping of the different result (outcomes). Note that the order of execution does not matters and is not in our hand. It is widely used in lambda expressions.

A parallel stream (flow) is a flow of objects that supports various functions and is intended to produce a reliable output. It is not a data structure so it does not allow users to enter input from Collection, Arrays, and other APIs.

Note that it does not change the functionality's actual behavior but provides the output based on the pipeline.

Java Parallel Stream Example

Therefore, it is advisable we must use parallel streams in such a case where the sequence of execution does not matter and the result will not be affected. Also, notable that the state of one element does not affect the other as well as the source of the data also remains unaffected.

Why use Parallel Stream?

The feature of parallel steams was introduced to increase the performance of a program. But is not always useful because there are various scenarios in which we require to execute the process in a certain order. In such a case, we can effectively use sequential order to perform the task at a cost-effective performance.

The only difference between the sequential and parallel stream is only of concern for large-scale programs or complex programs. It may not be useful for small-scale programs. Hence, we must consider parallel streams because the sequential stream behaves poorly.

When a stream executes in parallel, the process during the runtime partitions into multiple sub-streams, and aggregate operations iterate over the process of these substreams in parallel and then combine the result. Note that when we create a stream, it is always a serial stream unless we specified it.

  • It can be used in the case of aggregate functions.
  • It rapidly iterates over the large-sized collections.
  • It can be used if developers have performance implications with the Sequential Streams.
  • If the environment is not multi-threaded, then a parallel stream creates a thread and can affect the new requests coming in.

Properties of Parallel Stream

  • It does not affect other parallel streams.
  • It does not affect common Fort Join pool users.
  • It reduces unpredictable latency due to other common fork-join pool loads.
  • Invoking thread not used as a worker.

How to Create Parallel Stream in Java?

There are the following two ways to create a parallel stream in Java:

  1. Using the parallel() Method on a Stream
  2. Using parallelStream() on a Collection

Using the parallel() Method on a Stream

The method parallel() is defined in the BaseStream interface that belongs to the java.util.stream package. It performs the intermediate operation. It returns an equivalent stream in parallel. It may also return itself either the stream is already parallel or the stream state was modified to parallel.

Syntax:

Let's create a parallel stream through a Java program.

In the following program, we have created a constructor of the File class and passed a file to it. After that, we have created a Stream that reads the specified text file one line at a time. In the next statement, we have invoked the parallel() method that prints the text of the file on the console. Here, a point to note is that the order of execution at each run is different.

Note: Create a file with the name demo.txt and write something to it in order to get the proper output.

ParallelStreamExample1.java

Output:

Rahul
Anupam
Shubham
Nikhil
Ashish
Yuvan

Note: we have created the file named demo.txt at the location specified above. If you want the same result, create a file with the same name and write the data (as shown in the output) in the file.

Using parallelStream() on a Collection

The parallelStream() method is a pre-defined method in the Collections interface that belongs to java.util package. It returns the parallel stream with the collection as the source.

In the following Java program, we have done the same but, in this program, we have used List to read the text from the file. Instead of using the parallel() method, we have used paralleStream() method.

ParallelStreamExample2.java

Output:

Silver
Golden
Black
Green
Blue
Orange
Gray
Red	

Note: we have created the file named color.txt at the location specified above. If you want the same result, create a file with the same name and write the data (as shown in the output) in the file.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA