WEBVTT

00:00.230 --> 00:00.770
Hi everyone.

00:00.770 --> 00:02.120
Welcome back to this tutorial.

00:02.120 --> 00:08.120
In this tutorial we'll code another example of when not to use parallel streams.

00:08.120 --> 00:12.950
So we are going to cover a mutable variable that is going to return you.

00:12.950 --> 00:17.420
The result and how parallel stream can affect that behavior.

00:17.420 --> 00:26.390
So the first step, I'm going to create a class called Sum, and this class is going to have a variable

00:26.390 --> 00:29.960
called let me put this in presentation mode.

00:32.880 --> 00:36.120
Private and total.

00:37.770 --> 00:41.400
And then I'm going to create the getter and setter methods for this one.

00:44.510 --> 00:47.060
Click on Generate.

00:48.710 --> 00:52.190
Generate getters and setters and click okay.

00:53.640 --> 00:54.360
What I'm going to do.

00:54.390 --> 01:01.890
I'm going to have a method here called Public void perform sum.

01:02.640 --> 01:12.810
So this method is going to accept an input and then it is going to perform the summation of all the

01:12.810 --> 01:15.750
inputs that it is going to get and then return the result.

01:18.020 --> 01:21.920
So we are going to use this total variable to get the actual result.

01:22.640 --> 01:23.150
Okay.

01:23.150 --> 01:27.260
Now what I'm going to do, I'm going to create a some client class.

01:33.030 --> 01:37.350
So the some client class is going to call this perform some method from it.

01:41.950 --> 01:45.550
Some client make this class executable.

01:46.630 --> 01:49.780
We're adding public static void main.

01:50.980 --> 01:51.880
Public Static.

01:51.880 --> 01:52.870
Void Main.

01:52.900 --> 01:54.340
The class is ready.

01:54.610 --> 01:57.340
I'm going to create an instance of some

02:00.250 --> 02:08.140
and this one is going to be in stream dot range, closed one comma, 1000.

02:09.670 --> 02:13.650
And then first let's check the result.

02:13.660 --> 02:18.220
What is that result that it is going to give you?

02:18.910 --> 02:23.470
So I'm going to run it in sequential stream and then perform the.

02:25.980 --> 02:33.300
Some perform, some basically this is going to pass the values one by one, like one, two, three until

02:33.540 --> 02:36.510
1000, and it is going to perform.

02:36.510 --> 02:38.250
It is going to call the perform some method.

02:38.250 --> 02:40.290
I'm using the method reference syntax here.

02:40.290 --> 02:45.810
So if you go to the perform some, it is accepting an input and it is going to keep updating this variable

02:45.810 --> 02:47.970
and the result is going to be.

02:50.170 --> 02:54.580
Some dot get total get total is going to give you the actual result.

02:54.670 --> 02:56.230
I'm going to run this example.

02:57.490 --> 02:57.980
There you go.

02:58.000 --> 03:00.520
So the result that we're expecting is.

03:03.560 --> 03:04.890
500, 500.

03:04.910 --> 03:07.100
So that is a result which we are expecting.

03:07.100 --> 03:12.260
What I'm going to do, I'm going to run this example in parallel mode, right?

03:12.260 --> 03:20.210
So this is going to split this 1000 into multiple parts and then call this perform some method concurrently.

03:20.210 --> 03:27.140
And we are going to see whether is it returning the result, 505 hundred as an output or not.

03:28.770 --> 03:29.900
I'm gonna run this.

03:31.790 --> 03:36.410
The result that it gave is 448461.

03:36.650 --> 03:39.530
Let's run this again and then check what is the result.

03:40.910 --> 03:44.900
The result that it gave now is 4 to 7 and 396.

03:45.650 --> 03:48.410
I'm going to run it one more time and then check what is the result.

03:50.830 --> 03:55.050
The result that it gave is 424733.

03:55.050 --> 03:59.400
So every time we run this program, it is giving us a different result.

03:59.910 --> 04:06.510
The reason for the different result every time we run is that if you take a look at the code, we are

04:06.510 --> 04:11.220
concurrently accessing this variable and updating this variable.

04:11.250 --> 04:14.520
That's one of the reason why we get different results.

04:14.520 --> 04:16.440
We run the program every time.

04:16.440 --> 04:23.280
So what happens here is that it is splitting that data input into multiple parts and concurrently updating

04:23.280 --> 04:24.200
this variable.

04:24.210 --> 04:31.290
So the recommendation here is that if you have any mutable variable, then do not use parallel streams

04:31.290 --> 04:38.370
at all more than performance, it is going to give you the wrong results in these kind of scenarios.

04:38.370 --> 04:44.430
So this is one thing which we have to keep in our mind when we are going to use parallel streams With

04:44.430 --> 04:46.350
this, we came to the end of this tutorial.

04:46.350 --> 04:47.520
Thank you for watching.
