Java 8 Streams - Sequential vs Parallel streams - (Parallel thru Fork & Join)

Parallel streams divide the provided task into many and run them in different threads, utilizing multiple cores of the computer. On the other hand sequential streams work just like for-loop using a single core.

The tasks provided to the streams are typically the iterative operations performed on the elements of a collection or array or from other dynamic sources. Parallel execution of streams run multiple iterations simultaniously in different available core

In parallel execution, if number of tasks are more than available cores at a given time, the remaining tasks are queued waiting for currently running task to finish.

It is also important to know that iterations are only performed at a terminal operation, that's because streams are deisnged to be lazy.

Below is the program that in which clearly shows that in sequential stream each iteration waits for currently running one to finish, whereas, in parallel stream, eight threads are spawn simultaneously, remaining two, wait for others. Also notice the name of threads. In parallel stream, Fork and Join framework is used to create multiple threads. Parallel streams create ForkJoinPool instance via static ForkJoinPool.commonPool() method.




Shyam Sunder Patri

SDE 2 at Hitachi | Ex-Oracle | Java Expert | Spring Boot | Apache Kafka | AWS | RESTful APIs | Microservices | Angular | DSA Enthusiast | LLD & HLD Design Specialist |

6y

very nic tq understood

Like
Reply

To view or add a comment, sign in

More articles by 🧿 🟨Saral Saxena 🧑‍💻🏆

  • Run .http Files in a Intellij idea

    When developing APIs in a Spring Boot project, testing endpoints is a crucial part of the workflow. Tools like Postman…

  • Mastering API Versioning in Spring Boot: Stop Breaking Your Endpoints

    Introduction API versioning is one of the most overlooked yet critical aspects of API development. Many developers…

  • Blazing Fast Performance in Spring Boot 3 with CDS

    Spring Boot 3 brings several performance optimizations, but one of the most exciting ones is Class Data Sharing (CDS)…

  • Java 21 Docker Optimization

    Java 21 introduces significant improvements and features, particularly in containerization. This article explores five…

  • Java Optimization: 7000ms → 90ms

    When working with two separate lists and trying to match data based on a common id, many developers often default to…

  • Spring Boot Red Flags

    Spring Boot is a powerful framework designed to simplify Java application development by offering production-ready…

  • Stop Writing != null

    Null pointer exceptions are a familiar pain point in Java development. The go-to solution for many developers? Add a…

  • Spring boot Apps getting optimized

    Before making any changes, established clear performance baselines. Here’s what our initial metrics looked like:…

  • Validating Payloads with Spring Boot 3.4.0

    First, let’s examine a controller that receives a object. This object contains fields such as: first name, last name…

  • Limitations of Java Executor Framework.

    The Java Executor Framework has inherent limitations that affect its performance in high-throughput, low-latency…

Insights from the community

Others also viewed

Explore topics