WEBVTT

00:01.140 --> 00:01.710
Hi everyone.

00:01.710 --> 00:03.030
Welcome back to this tutorial.

00:03.030 --> 00:09.930
In this tutorial we will code a simple example on when not to use parallel streams.

00:09.930 --> 00:15.420
I'm going to create a class in the parallel streams package the class name is going to be.

00:18.340 --> 00:24.820
Parallel stream boxed example.

00:25.420 --> 00:30.100
Then I'm going to make this class executable by adding public static void main method.

00:30.910 --> 00:31.720
And then.

00:34.010 --> 00:38.810
I'm going to create a list of integers instead of using a int stream.

00:38.810 --> 00:45.500
I'm going to use a list of integers, but I'm going to use the int stream to create that.

00:47.060 --> 00:52.310
INT stream dot range closed one comma.

00:54.200 --> 00:54.950
10,000.

00:55.310 --> 01:01.400
And then what I'm going to do, I'm going to do a boxed and then collectors.

01:02.990 --> 01:15.950
It's going to be to list you do this, it is going to give you a list of integers, basically wrapper

01:15.950 --> 01:20.780
classes, a list of 10,000 integer elements.

01:20.780 --> 01:24.020
But these are not primitive integers.

01:24.020 --> 01:28.160
These are wrapper integer objects.

01:28.160 --> 01:31.340
And then now I'm going to create a.

01:33.020 --> 01:38.750
Method called sequential sum.

01:41.210 --> 01:52.750
This is going to accept this list of integers as an input, and then this is going to perform the summation

01:52.760 --> 01:55.760
integer list, dot stream.

01:57.090 --> 01:57.530
Dot.

01:57.540 --> 02:04.020
I'm going to use a reduce function, which is going to take the initial value as zero, and then it

02:04.020 --> 02:11.460
is going to accept the binary operator, which is going to take two input as an input and then perform

02:11.460 --> 02:15.630
the summation of this two input and return the result.

02:17.190 --> 02:18.720
Basically this is.

02:19.850 --> 02:24.590
Nothing but performing summation of all the elements.

02:26.670 --> 02:32.280
Of the list of integer type with the name integer list.

02:33.270 --> 02:35.610
Now I'm going to perform the

02:38.310 --> 02:42.600
calculation of how long did it take in order to complete this process?

02:42.780 --> 02:45.540
System dot current milliseconds.

02:45.840 --> 02:48.510
And then I'm going to have the.

02:49.960 --> 02:55.690
Duration equal to system.currenttimemillis seconds minus the start.

02:57.630 --> 03:07.090
So I'm going to have this value as duration in sequential stream.

03:09.530 --> 03:12.980
Duration and then the return.

03:14.150 --> 03:15.560
Is equal to some.

03:18.650 --> 03:20.310
I'm going to create another method.

03:20.330 --> 03:24.200
It's going to be named as parallel sum.

03:27.390 --> 03:32.400
And in the place of cream, I'm going to use parallel stream.

03:32.400 --> 03:34.470
So now we have two methods.

03:34.470 --> 03:40.320
One is performing the sequential stream and another one is performing the parallel stream on the input

03:40.350 --> 03:42.660
of type list of integers.

03:42.690 --> 03:51.870
Let's go ahead and run this and then check what is the performance that it is going to value the duration.

03:53.070 --> 03:59.820
How long did it take in order to perform the summation of 10,000 elements in the parallel stream and

03:59.820 --> 04:01.380
the sequential stream?

04:05.020 --> 04:06.010
To do list

04:08.590 --> 04:11.990
and this is going to be parallel sum.

04:12.010 --> 04:12.760
There we go.

04:12.760 --> 04:13.930
So we have the code ready.

04:13.930 --> 04:15.780
Let's run this and check the result.

04:15.790 --> 04:19.300
How long did it take for the sequential stream to perform the summation?

04:19.300 --> 04:25.060
How long did it take for the parallel stream to perform the summation of 10,000 elements?

04:25.390 --> 04:31.750
If we take a look at it, the sequential stream took only four milliseconds here and the parallel stream

04:31.780 --> 04:34.120
took eight milliseconds.

04:34.120 --> 04:40.090
The reason here is if we take a look at the input type is of list of integers.

04:40.090 --> 04:48.790
So here there is an effort which is needed in order to perform the unboxing right.

04:48.790 --> 04:57.430
So it needs to convert unboxing from integer to int.

04:57.460 --> 05:02.530
So it needs to perform unboxing behind the scenes for each and every value.

05:02.530 --> 05:07.760
This parallel stream passes to it and then returns the result.

05:08.150 --> 05:14.870
It is always recommended to check the performance before going to the assumption that parallel stream

05:14.870 --> 05:16.280
is going to perform better.

05:16.280 --> 05:18.530
So we have looked at an example.

05:18.530 --> 05:24.680
So there is going to be many more examples which might look like parallel stream is going to perform

05:24.680 --> 05:32.180
better, but in reality parallel stream is not going to perform better than the sequential stream in

05:32.180 --> 05:33.620
all the scenarios.

05:34.360 --> 05:34.750
But this.

05:34.750 --> 05:36.430
We came to the end of this tutorial.

05:36.460 --> 05:37.720
Thank you for watching.
