Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will explore how parallel stream works behind the scenes.
Parallel Stream uses the fork join framework under the hood to process the streams.
Parallelly and the fork.
Join framework got introduced as part of Java seven.
Fork join creates multiple threads for you, but the question here is how many threads that gets created.
The number of threads that gets created is equal to the number of processors that are available in the
machine.
The main reason for this way of spawning the threads is the advancement of technology in the world today.
Today, even smartphones that you have in your hand has multiple processors.
The idea is to use those resources available to perform the concurrent processing.
Let's go ahead and check how many processors are available in this machine.
I'm going to go to the IntelliJ and then what I'm going to do, I'm going to do a Sis out and run time
dot get runtime dot available processors.
So this is going to give you how many processors are available in this machine run.
This is going to give you the value here as eight So this machine has eight processors.
So as soon as this parallel method call happened and then because the terminal operation is the one
which is going to start the whole pipeline as soon as the some method is called and it checks, okay.
Is it a parallel stream or a sequential stream?
If it is a parallel stream, this is going to split the data into multiple parts and process them concurrently
and it is going to accumulate the result and give you the result as a output.
I would like to show you what happens behind the scenes using using the presentation here.
So if you go here, this machine has eight processors.
That's what we checked here.
So sequential stream, what it is going to do, it is going to perform the complete summation operation
using one processor.
It is not going to leverage the multiple processors that you have in your machine.
But in the case of parallel stream, this is going to split the whole data source into multiple parts
and then assign each part to a processor.
And this processor is going to take care of performing the summation for you.
Parallelly And towards the end it is going to sum all the result and give you the result as a output.
So this is all about what happens behind the scenes when you call the parallel stream method.
With this.
We came to the end of this tutorial.
Thank you for watching.