
Let’s look at the differences between Sequential and parallel stream in Java 8, as well as when to use parallel streams in Java 8. Sequential stream is based on a single core and works similar as for loop. Parallel stream makes use of the computer’s numerous cores and divides the job into multiple subtasks, each of which is executed in its own thread.
By default, any stream action in Java is performed sequentially. Sequential stream never makes use of the computer’s multi-core. This is the major drawback of sequential stream.
When it comes to Parallel Streams, Java provides some methods for achieving parallelism. The parallel stream is prone to errors and is quite complex. However, Java Streams provides a reliable library that takes care of all the complexity, and we just need to use a few methods to achieve parallelism.
It is true that Java 8 supports both parallel and sequential stream. We can use both types of streams according to our need.
Table of Contents
Sequential Stream in Java example
By default the stream is sequential. If a stream operation is not marked as parallel, it is considered as a sequential stream. When we employ a sequential stream, we are not taking use of a multi-core system, even though the underlying system allows for parallel processing.
The stream() method in Java 8 returns a sequential stream.
import java.util.Arrays;
import java.util.List;
public class SequentialStreamExample {
public static void main(String[] args) {
List inputList = Arrays.asList("Welcome ", "to ", "Geekcer ", "Education");
inputList.stream().forEach(System.out::print);
}
}
Output: Welcome to Geekcer Education
Parallel Stream in Java example
When we use the parallel stream functionality in our code, it is sliced into many streams that run in parallel on the system’s different cores.
Due to the use of multiple cores, parallel streams improve performance. It is not necessary that all programs/code be parallelized. However, in parts of the code where there is a lot of processing and we need to increase speed, we can use parallel stream.
There are mainly two options for accomplishing parallel functionality.
- By using the collection’s parallelStream() method to create a parallel stream.
- By applying BaseStream’s parallel() method to a sequential stream.
import java.util.Arrays;
import java.util.List;
public class ParallelStreamExample {
public static void main(String[] args) {
List inputList = Arrays.asList("Welcome ", "to ", "Geekcer ", "Education ");
inputList.parallelStream().forEach(System.out::print);
}
}
Output: Geekcer Education to Welcome
Example using the forEachOrdered() method to get the data in order.
import java.util.Arrays;
import java.util.List;
public class ParallelStreamExample {
public static void main(String[] args) {
List inputList = Arrays.asList("Welcome ", "to ", "Geekcer ", "Education ");
inputList.parallelStream().forEachOrdered(System.out::print);
}
}
Output: Welcome to Geekcer Education
Sequential Stream Vs Parallel Stream
Sequential Stream | Parallel Stream |
---|---|
Utilize single core of the system. | Utilize multi-core of the system. |
Cares about the order. | Doesn’t care about the order. |
Sequential Stream is platform Independent. | Parallel Stream is platform dependent. |
Sequential Stream is more reliable and more error-free. | In a sequential stream, one iteration waits for the current iteration to finish before proceeding. |
In a sequential stream, one iteration waits for the current iteration to finish before proceeding. | In a parallel stream, an iteration does not wait for the current iteration to finish in order to proceed. |
When to use parallel streams in java 8?
- You have a large dataset in which you want to perform operations.
- If sequential streams have a performance impact, parallel streams can be used.
- If you want to use all the cores of your system.
Disadvantages of parallel stream in java 8
Parallel stream divides them into sub-problems, which are then processed on separate threads, which can be shifted to other cores and then merged after they’re finished. If you don’t use it correctly, it might actually slow down your code.
Unexpected effects may occur if all shared resources between threads are not properly synchronized.
Recommended Articles
- What is Lambda expression with Example
- map() function of Stream in Java 8
- Runnable Vs Callable in Java
- Fork/Join for the Processing in Java 8
- Java Concurrent Collections with example