Hi everyone.
Welcome back to this tutorial.
In this tutorial we'll code another example of when not to use parallel streams.
So we are going to cover a mutable variable that is going to return you.
The result and how parallel stream can affect that behavior.
So the first step, I'm going to create a class called Sum, and this class is going to have a variable
called let me put this in presentation mode.
Private and total.
And then I'm going to create the getter and setter methods for this one.
Click on Generate.
Generate getters and setters and click okay.
What I'm going to do.
I'm going to have a method here called Public void perform sum.
So this method is going to accept an input and then it is going to perform the summation of all the
inputs that it is going to get and then return the result.
So we are going to use this total variable to get the actual result.
Okay.
Now what I'm going to do, I'm going to create a some client class.
So the some client class is going to call this perform some method from it.
Some client make this class executable.
We're adding public static void main.
Public Static.
Void Main.
The class is ready.
I'm going to create an instance of some
and this one is going to be in stream dot range, closed one comma, 1000.
And then first let's check the result.
What is that result that it is going to give you?
So I'm going to run it in sequential stream and then perform the.
Some perform, some basically this is going to pass the values one by one, like one, two, three until
1000, and it is going to perform.
It is going to call the perform some method.
I'm using the method reference syntax here.
So if you go to the perform some, it is accepting an input and it is going to keep updating this variable
and the result is going to be.
Some dot get total get total is going to give you the actual result.
I'm going to run this example.
There you go.
So the result that we're expecting is.
500, 500.
So that is a result which we are expecting.
What I'm going to do, I'm going to run this example in parallel mode, right?
So this is going to split this 1000 into multiple parts and then call this perform some method concurrently.
And we are going to see whether is it returning the result, 505 hundred as an output or not.
I'm gonna run this.
The result that it gave is 448461.
Let's run this again and then check what is the result.
The result that it gave now is 4 to 7 and 396.
I'm going to run it one more time and then check what is the result.
The result that it gave is 424733.
So every time we run this program, it is giving us a different result.
The reason for the different result every time we run is that if you take a look at the code, we are
concurrently accessing this variable and updating this variable.
That's one of the reason why we get different results.
We run the program every time.
So what happens here is that it is splitting that data input into multiple parts and concurrently updating
this variable.
So the recommendation here is that if you have any mutable variable, then do not use parallel streams
at all more than performance, it is going to give you the wrong results in these kind of scenarios.
So this is one thing which we have to keep in our mind when we are going to use parallel streams With
this, we came to the end of this tutorial.
Thank you for watching.