Updated February 10, 2023
Introduction to Java 8 Parallel Stream
Java 8 parallel stream is a feature of java 8 and higher version in that we are utilizing cores of multiple processors. Normally java code contains a single stream of processing, which will execute sequentially. While using parallel streams, we can divide our code into many streams which execute code in parallel core and separate. The final result contains the combination of each outcome.
In our application, using parallel streams is recommended in cases where there is no issue with order execution, the result of our code is affected, and the state of a specified element is not affected. Parallel streams were introduced in Java 8 to improve program performance, but we believe that using parallel streams is the best option. In many cases, we need our code to be executed in a specific order; in these cases, we use a parallel stream.
The performance difference between sequential and parallel streams is only relevant to large-scale programs or applications. When the sequential stream fails to function properly, we can use the parallel stream in our project. There are two ways in which we are creating the parallel stream using the parallel method on a stream and parallelStream on collection.
Key Takeaways
- In java 8 new concept is introduced named as a parallel stream which was used for parallel processing. We can process multiple threads parallel by using a parallel stream.
- The parallel method onto the base interface is returning a parallel stream, which was equivalent to the method of ParallelStream.
How to Create Java 8 Parallel Stream?
We can create a parallel stream by using two methods. Below methods shows how we can create the parallel stream as follows:
- By using parallel method onto the stream: The parallel method on the base interface returns an equivalent parallel stream. In the code below, we create a file object that points to the pre-existing txt file system. Then we’re creating the stream that was reading from the txt file one line at a time, and then we’re using parallel methods to read the print file onto the console. The execution order is different on each run; we can check the same in the output.
- By using parallel stream onto the collection: The parallel stream methods onto the collection interface return the parallel stream by using the collection as a source. In the below example, we are giving the parallel stream by using the text file. We are using example.txt file to read the data from the file.
Java 8 Parallel Stream Processing
At the time of increasing the performance of the program, we are using the parallel stream in our application. By using parallel processing, it is very easy to create the stream which executes parallel and makes the multiple cores of processing. The processing is faster while dividing the work into multiple cores.
The parallel stream processing in java 8 is a wrapper around of the data source, which allows the bulk operations on data on which we are working. It is a very convenient way to divide our operation in multiple threads.
Below example shows parallel stream processing as follows:
Code:
import java.util.stream.IntStream;
public class ParallelStream {
public static void main(String[] args) {
System.out.println ("Normal Execution");
IntStream ra = IntStream.rangeClosed (1, 5);
ra.forEach (System.out::println);
System.out.println ("Parallel Execution");
IntStream ra2 = IntStream.rangeClosed (1, 5);
ra2.parallel ().forEach(System.out::println);
}
}
Output:
In the below example, we are running the stream in a parallel mode as follows:
We are using the normal and parallel execution as follows:
Code:
import java.util.stream.IntStream;
public class ParallelStream {
public static void main(String[] args) {
System.out.println ("Normal Execution");
IntStream ra = IntStream.rangeClosed (1, 5);
System.out.println (ra.isParallel());
ra.forEach (System.out::println);
System.out.println ("Parallel Execution");
IntStream ra2 = IntStream.rangeClosed (1, 5);
IntStream range2Parallel = ra2.parallel ();
System.out.println (range2Parallel.isParallel ());
range2Parallel.forEach (System.out::println);
}
}
Output:
Examples of Java 8 Parallel Stream
Given below are the examples mentioned:
Example #1
In the below example, we are using the parallel method on stream and using Example.txt file to get the output as follows.
Code:
import java.io.IOException;
import java.util.stream.Stream;
import java.io.File;
import java.nio.file.Files;
public class Example
{
public static void main(String[] args) throws IOException
{
File f = new File("C:\\Users\\OMSAI\\Desktop\\Example.txt");
Stream<String> text = Files.lines (f.toPath());
text.parallel ().forEach(System.out::println);
text.close ();
}
}
Output:
Example #2
In the below example, we are using parallelStream method on stream and using Example.txt file to get the output as follows.
Code:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
public class Example {
public static void main(String[] args) throws IOException
{
File f
= new File("C:\\Users\\OMSAI\\Desktop\\Example.txt");
List<String> t
= Files.readAllLines (f.toPath());
t.parallelStream ().forEach(System.out::println);
}
}
Output:
FAQs
Given below are the FAQs mentioned:
Q1. What is the use of java 8 parallel stream?
Answer: Java 8 parallel stream is used to increase the performance of our application. It is recommended to use the parallel stream in our application.
Q2. Which method we are using to create a parallel stream?
Answer: We are using two methods for creating the parallel stream i.e. parallel method and the parallel stream method.
Q3. Why we are using the parallel stream in our application?
Answer: We are using parallel stream in our application to increase the performance of our application. Use a parallel stream is the best choice for our application.
Conclusion
The performance difference between sequential and parallel stream is only concerned with large-scale programs or large applications. Java 8 parallel stream is a feature of java 8 and higher version in that we are utilizing cores of multiple processors. Normally java code will contain a single stream of processing, where it will execute sequentially.
Recommended Articles
This is a guide to Java 8 Parallel Stream. Here we discuss the introduction, how to create java 8 parallel stream, and examples. You may also have a look at the following articles to learn more –