Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code a simple example on when not to use parallel streams.
I'm going to create a class in the parallel streams package the class name is going to be.
Parallel stream boxed example.
Then I'm going to make this class executable by adding public static void main method.
And then.
I'm going to create a list of integers instead of using a int stream.
I'm going to use a list of integers, but I'm going to use the int stream to create that.
INT stream dot range closed one comma.
10,000.
And then what I'm going to do, I'm going to do a boxed and then collectors.
It's going to be to list you do this, it is going to give you a list of integers, basically wrapper
classes, a list of 10,000 integer elements.
But these are not primitive integers.
These are wrapper integer objects.
And then now I'm going to create a.
Method called sequential sum.
This is going to accept this list of integers as an input, and then this is going to perform the summation
integer list, dot stream.
Dot.
I'm going to use a reduce function, which is going to take the initial value as zero, and then it
is going to accept the binary operator, which is going to take two input as an input and then perform
the summation of this two input and return the result.
Basically this is.
Nothing but performing summation of all the elements.
Of the list of integer type with the name integer list.
Now I'm going to perform the
calculation of how long did it take in order to complete this process?
System dot current milliseconds.
And then I'm going to have the.
Duration equal to system.currenttimemillis seconds minus the start.
So I'm going to have this value as duration in sequential stream.
Duration and then the return.
Is equal to some.
I'm going to create another method.
It's going to be named as parallel sum.
And in the place of cream, I'm going to use parallel stream.
So now we have two methods.
One is performing the sequential stream and another one is performing the parallel stream on the input
of type list of integers.
Let's go ahead and run this and then check what is the performance that it is going to value the duration.
How long did it take in order to perform the summation of 10,000 elements in the parallel stream and
the sequential stream?
To do list
and this is going to be parallel sum.
There we go.
So we have the code ready.
Let's run this and check the result.
How long did it take for the sequential stream to perform the summation?
How long did it take for the parallel stream to perform the summation of 10,000 elements?
If we take a look at it, the sequential stream took only four milliseconds here and the parallel stream
took eight milliseconds.
The reason here is if we take a look at the input type is of list of integers.
So here there is an effort which is needed in order to perform the unboxing right.
So it needs to convert unboxing from integer to int.
So it needs to perform unboxing behind the scenes for each and every value.
This parallel stream passes to it and then returns the result.
It is always recommended to check the performance before going to the assumption that parallel stream
is going to perform better.
So we have looked at an example.
So there is going to be many more examples which might look like parallel stream is going to perform
better, but in reality parallel stream is not going to perform better than the sequential stream in
all the scenarios.
But this.
We came to the end of this tutorial.
Thank you for watching.