Javatpoint Logo
Javatpoint Logo

Parallel Stream in Java

One of the prominent features of Java 8 (or higher) is Java Parallel Stream. It is meant for utilizing the various cores of the processor. Usually, any Java code that has only one processing stream, where it is sequentially executed. However, by using parallel streams, one can separate the Java code into more than one stream, which is executed in parallel on their separate cores, and the end result is the combination of the individual results. The order in which they are executed is not in our control. Hence, it is suggested to use a parallel stream when the order of execution of individual items does not affect the final result.

Analysis of Parallel Stream

For increasing the performance of a program, parallel streams are introduced. However, it is not a guarantee that applying a parallel stream will enhance the result. For example, there can be a scenario where code must be executed in a certain order. There are certain instances in which we need the code to be executed in a certain order, and in such a case, it is required to use sequential streams instead of parallel streams.

Different Ways to Create Stream

There are two ways we can create, which are listed below and described later as follows:

  1. Using the parallel() method on a stream
  2. Using parallelStream() on a Collection

Using parallel() method on a stream

The parallel() method of the BaseStream interface returns an equivalent parallel stream. Let's understand its working through an example.

FileName: ParallelStream.java

Output 1:

Vestibulum urna lacus, eleifend venenatis ipsum at, venenatis fringilla mauris.
Fusce nulla augue, convallis at velit ac, pulvinar convallis eros.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed ut ipsum molestie dolor dictum luctus.
Maecenas interdum erat feugiat rhoncus mattis. Phasellus facilisis ex non magna faucibus mollis.
Vestibulum eu tellus nec lectus rutrum ornare ac tincidunt sem.

Explanation: In the above program, we have made a file object that points to a parallelstream.txt file that is already present in the system. After that, a stream is created that does reading from the text file (only one line at a time). Then we invoke the parallel() method to display the content of the parallelstream.txt on the console. Note that the order of the execution is different each time we execute the above code. The code is executed again; the following output is displayed on the console.

Output 2:

Fusce nulla augue, convallis at velit ac, pulvinar convallis eros.
Vestibulum urna lacus, eleifend venenatis ipsum at, venenatis fringilla mauris.
Maecenas interdum erat feugiat rhoncus mattis. Phasellus facilisis ex non magna faucibus mollis.
Vestibulum eu tellus nec lectus rutrum ornare ac tincidunt sem.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed ut ipsum molestie dolor dictum luctus.

Using parallelStream() on a Collection

The parallelStream() method is part of the Collection interface and returns a parallel stream with the collection as a source. It's working of it is explained in the following example.

FileName: ParallelStream.java

Output 1:

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed ut ipsum molestie dolor dictum luctus.
Vestibulum urna lacus, eleifend venenatis ipsum at, venenatis fringilla mauris.
Maecenas interdum erat feugiat rhoncus mattis. Phasellus facilisis ex non magna faucibus mollis.
Fusce nulla augue, convallis at velit ac, pulvinar convallis eros.
Vestibulum eu tellus nec lectus rutrum ornare ac tincidunt sem.

Explanation: In the above-mentioned code, we are using a parallel stream. However, we are using the List to read from the parallelstream.txt file. Hence, the parallelstream() method is required.

Other Examples of Parallel Execution of Stream

Let's see a few more examples of parallel execution of streams.

FileName: ParallelStream1.java

Output:

In Normal
1
2
3
4
5
In Parallel
3
5
4
2
1

Let's see another example.

FileName: ParallelStream2.java

Output:

In Normal
1
2
3
4
5
In Parallel
3
5
4
2
1

Java Program to Check Stream is Running Parallel or Not

We can also check whether the stream is running in parallel or not.

FileName: ParallelStream3.java

Output:

In Normal
The stream is not running parallelly.
1
2
3
4
5
In Parallel
The stream is running parallelly.
3
5
4
1
2






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