Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will build a real use case stream pipeline and then compare running it using the
sequential and parallel stream way.
And here I'm going to create another example class called Parallel.
Stream.
Example one.
Let's make this class executable by adding public static void main method.
So in here I'm going to make use of the one of the use case that we coded already.
I'm just going to copy and paste the code from there.
So in here we have this flat map example.
Then I'm going to take this class and run it in parallel and sequential and then compare the result.
So in here, I'm going to have this method here going to be.
Sequential.
Student activities.
And then let's copy the same method.
Going to be parallel.
In order to make the stream, a parallel stream can either call parallel stream directly or.
There is a method called stream dot parallel.
This is going to make you the stream to behave in parallel manner.
What I'm going to do, I'm going to make.
Long.
Runtime.
Are the start time here.
It's going to be system dot.
Runtime milliseconds and this is going to be system dot going to be in time basically.
Runtime milliseconds.
And I'm going to print the duration it took.
Duration to execute the pipeline.
The pipeline in.
Sequential manner.
Right.
That's going to be in time minus the start time.
Be minus.
What is it?
It is complaining.
We have to put it inside the braces so that it will evaluate as an expression.
So I'm going to copy the same thing here and then put it here.
That's the start time over here.
Here we go.
So we have the code reading.
I'm going to change this one as parallel.
Let's explore what is that happening in this use case and then run this program.
So enter the presentation mode.
So here in this example, what is happening is we are getting all the students from the student database
class and then we are calling the stream method and we are mapping the student activities, the student
activities.
If you take a look at it, it is returning a list of student activities.
And then what we are doing is we are performing a flatmap operation and then we are performing a distinct
sorted and collected list.
So these are the different operations that we are performing here.
Let's see how it is performing.
Well, here is it is a sequential stream which is going to perform better or the parallel stream which
is going to perform better.
I'm going to I don't have to put the session because I don't care the care about the result.
I want to check what is the duration it took in order to give you the output.
So I'm going to call sequential print.
Student activities and then parallel print student activities.
There you go.
Now we will run this and check the result.
Run this.
So if we take a look at it, the sequential stream took 97 milliseconds.
But if you take a look at the parallel stream, it just took the 14 milliseconds.
Ideally behind the scenes, it is splitting the data into multiple threads and then processing them
parallelly and give you the result as a output.
So the recommendation here is that always check the performance before we assume parallel is going to
perform better.
That is not the case.
There are certain stream functions which is going to hurt the performance of parallel.
I'll be covering that in the next tutorial.
With this, we came to the end of this tutorial.
Thank you for watching.