WEBVTT

00:00.290 --> 00:00.830
Hi everyone.

00:00.830 --> 00:02.180
Welcome back to this tutorial.

00:02.180 --> 00:08.810
In this tutorial, we will build a real use case stream pipeline and then compare running it using the

00:08.810 --> 00:11.990
sequential and parallel stream way.

00:13.250 --> 00:18.740
And here I'm going to create another example class called Parallel.

00:19.880 --> 00:20.480
Stream.

00:20.480 --> 00:21.980
Example one.

00:22.700 --> 00:27.320
Let's make this class executable by adding public static void main method.

00:27.410 --> 00:32.990
So in here I'm going to make use of the one of the use case that we coded already.

00:33.020 --> 00:35.750
I'm just going to copy and paste the code from there.

00:35.780 --> 00:39.290
So in here we have this flat map example.

00:39.400 --> 00:45.590
Then I'm going to take this class and run it in parallel and sequential and then compare the result.

00:46.500 --> 00:50.930
So in here, I'm going to have this method here going to be.

00:52.390 --> 00:53.440
Sequential.

00:55.220 --> 00:56.570
Student activities.

00:56.570 --> 00:59.060
And then let's copy the same method.

01:00.420 --> 01:01.740
Going to be parallel.

01:04.640 --> 01:13.280
In order to make the stream, a parallel stream can either call parallel stream directly or.

01:14.730 --> 01:17.920
There is a method called stream dot parallel.

01:17.940 --> 01:24.720
This is going to make you the stream to behave in parallel manner.

01:24.750 --> 01:27.180
What I'm going to do, I'm going to make.

01:28.030 --> 01:28.810
Long.

01:31.460 --> 01:32.390
Runtime.

01:33.400 --> 01:35.680
Are the start time here.

01:35.830 --> 01:38.260
It's going to be system dot.

01:39.430 --> 01:45.580
Runtime milliseconds and this is going to be system dot going to be in time basically.

01:47.020 --> 01:48.250
Runtime milliseconds.

01:48.370 --> 01:51.070
And I'm going to print the duration it took.

01:52.850 --> 01:55.190
Duration to execute the pipeline.

01:57.640 --> 01:58.930
The pipeline in.

02:00.050 --> 02:01.250
Sequential manner.

02:01.250 --> 02:01.900
Right.

02:02.720 --> 02:06.680
That's going to be in time minus the start time.

02:08.910 --> 02:09.670
Be minus.

02:11.760 --> 02:12.200
What is it?

02:12.210 --> 02:13.440
It is complaining.

02:14.610 --> 02:19.200
We have to put it inside the braces so that it will evaluate as an expression.

02:19.760 --> 02:24.000
So I'm going to copy the same thing here and then put it here.

02:24.340 --> 02:27.240
That's the start time over here.

02:29.290 --> 02:29.790
Here we go.

02:29.800 --> 02:31.300
So we have the code reading.

02:31.300 --> 02:33.100
I'm going to change this one as parallel.

02:33.880 --> 02:39.430
Let's explore what is that happening in this use case and then run this program.

02:39.740 --> 02:41.410
So enter the presentation mode.

02:41.500 --> 02:49.060
So here in this example, what is happening is we are getting all the students from the student database

02:49.060 --> 02:55.330
class and then we are calling the stream method and we are mapping the student activities, the student

02:55.330 --> 02:55.900
activities.

02:55.900 --> 03:00.940
If you take a look at it, it is returning a list of student activities.

03:00.940 --> 03:06.880
And then what we are doing is we are performing a flatmap operation and then we are performing a distinct

03:06.940 --> 03:09.700
sorted and collected list.

03:09.940 --> 03:13.090
So these are the different operations that we are performing here.

03:13.130 --> 03:15.130
Let's see how it is performing.

03:15.130 --> 03:21.790
Well, here is it is a sequential stream which is going to perform better or the parallel stream which

03:21.790 --> 03:23.560
is going to perform better.

03:23.770 --> 03:27.370
I'm going to I don't have to put the session because I don't care the care about the result.

03:27.370 --> 03:34.910
I want to check what is the duration it took in order to give you the output.

03:35.760 --> 03:40.140
So I'm going to call sequential print.

03:41.190 --> 03:46.080
Student activities and then parallel print student activities.

03:46.770 --> 03:47.320
There you go.

03:47.340 --> 03:49.710
Now we will run this and check the result.

03:52.540 --> 03:53.350
Run this.

03:56.130 --> 04:01.200
So if we take a look at it, the sequential stream took 97 milliseconds.

04:01.200 --> 04:07.260
But if you take a look at the parallel stream, it just took the 14 milliseconds.

04:07.260 --> 04:12.990
Ideally behind the scenes, it is splitting the data into multiple threads and then processing them

04:12.990 --> 04:15.990
parallelly and give you the result as a output.

04:16.140 --> 04:22.980
So the recommendation here is that always check the performance before we assume parallel is going to

04:22.980 --> 04:24.090
perform better.

04:24.120 --> 04:25.410
That is not the case.

04:25.410 --> 04:29.730
There are certain stream functions which is going to hurt the performance of parallel.

04:29.760 --> 04:32.490
I'll be covering that in the next tutorial.

04:32.760 --> 04:34.980
With this, we came to the end of this tutorial.

04:35.010 --> 04:36.300
Thank you for watching.
