1 00:00:00,470 --> 00:00:03,469 The goal of concurrency is to help your application perform better. 2 00:00:06,550 --> 00:00:09,820 Concurrency means making progress on more than one task at a time. 3 00:00:11,970 --> 00:00:14,760 And we can use multithreading to achieve concurrency. 4 00:00:18,890 --> 00:00:21,740 In this lesson, you're going to learn about concurrency and multithreading. 5 00:00:23,810 --> 00:00:27,890 First, open up the folder for this lesson by following this path in your course of resources. 6 00:00:33,420 --> 00:00:37,710 Serial execution means running tasks one at a time, one after another. 7 00:00:41,380 --> 00:00:46,870 This, as an example of serial execution tasks are being performed one by one. 8 00:00:57,570 --> 00:01:02,310 The problem with serial execution is that when you have heavy tasks, they're going to block the main 9 00:01:02,310 --> 00:01:05,580 thread for a long time before it can go to the next task. 10 00:01:16,790 --> 00:01:21,740 If we go to a single thread, dot Java and run the code Opper to break points first and foremost. 11 00:01:24,090 --> 00:01:28,500 We can see that this task, if we step over it, it's going to block the main thread for three whole 12 00:01:28,500 --> 00:01:29,160 seconds. 13 00:01:29,460 --> 00:01:32,190 Only then we can perform the other tasks. 14 00:01:32,460 --> 00:01:34,080 This is really, really bad. 15 00:01:39,600 --> 00:01:45,180 Long running tasks should not block the main thread, long running tasks should you run on another thread. 16 00:01:47,170 --> 00:01:50,020 And that is when you would need to use concurrent execution. 17 00:01:52,240 --> 00:01:56,410 Concurrent execution means making progress on more than one task at a time. 18 00:02:00,010 --> 00:02:02,170 We can achieve concurrency with multi-threading. 19 00:02:06,060 --> 00:02:11,310 An example would be one thread performing one set of tasks, while another thread performs another set 20 00:02:11,310 --> 00:02:16,530 of tasks at random, a scheduler is going to say, OK, I'll run some parts of this task. 21 00:02:16,800 --> 00:02:21,150 OK, now I'll switch over to another thread to run some of its tasks, and then we'll switch back to 22 00:02:21,150 --> 00:02:21,690 that thread. 23 00:02:21,960 --> 00:02:23,160 And it's totally random. 24 00:02:23,160 --> 00:02:26,640 It's extremely hard to predict in what order it's going to accomplish our tasks. 25 00:02:26,940 --> 00:02:32,280 But the take home message is that we can use concurrency to make progress on more than one task at a 26 00:02:32,280 --> 00:02:32,670 time. 27 00:02:33,510 --> 00:02:37,410 And in order to achieve concurrency, you need to learn how to create threads. 28 00:02:37,950 --> 00:02:41,810 Well, we're going to do is run code that isn't time intensive on the main thread. 29 00:02:42,300 --> 00:02:45,540 Run code that is time intensive on another thread. 30 00:02:51,180 --> 00:02:56,520 In Java, you can create a thread object as follows create a new thread objects and give the thread 31 00:02:56,520 --> 00:02:57,810 a runnable task. 32 00:03:04,140 --> 00:03:06,390 So inside multithreaded Java. 33 00:03:09,580 --> 00:03:15,100 Oh, create a new object of the thread class thread thread two is equal to a new thread. 34 00:03:16,690 --> 00:03:18,910 That invokes a runnable task. 35 00:03:21,740 --> 00:03:26,570 Now we have to define a reasonable task, and the way to do that is to create an object of a class that 36 00:03:26,570 --> 00:03:28,220 implements the runnable interface. 37 00:03:28,730 --> 00:03:32,330 The runnable interface forces our class to override the run method. 38 00:03:32,720 --> 00:03:35,290 And here is where you would call the long running task. 39 00:03:38,430 --> 00:03:45,750 So back here in our code, we can create a class static class long task. 40 00:03:46,750 --> 00:03:48,970 That implements the runnable interface. 41 00:03:51,360 --> 00:03:54,370 And here you can see we're being forced to override the run method. 42 00:03:54,510 --> 00:03:55,560 So we'll do just that. 43 00:03:57,010 --> 00:04:00,010 An inside run is where you would call the long running task. 44 00:04:01,790 --> 00:04:07,910 OK, now inside the main method, we're going to create a new object of the long task class, long task, 45 00:04:07,910 --> 00:04:09,020 we'll call it runnable. 46 00:04:09,920 --> 00:04:13,700 The runnable task is an equally new instance of long task. 47 00:04:14,780 --> 00:04:15,320 And we're good. 48 00:04:19,290 --> 00:04:22,730 We're going to be executing this runnable task on threat, too. 49 00:04:23,090 --> 00:04:28,670 And finally, in order to run this thread, we're just going to call threat to Dutch starts. 50 00:04:51,570 --> 00:04:55,680 So we can expect that this long task isn't going to block the main threat anymore. 51 00:04:56,500 --> 00:04:57,390 Let's run the app. 52 00:04:59,810 --> 00:05:00,710 And beautiful. 53 00:05:04,660 --> 00:05:08,800 This long task is running in the background, and it doesn't block the main threat anymore. 54 00:05:13,710 --> 00:05:16,440 We're making progress on more than one task at a time. 55 00:05:30,080 --> 00:05:33,530 Now, is there a better way to create a runnable that was really messy? 56 00:05:34,250 --> 00:05:35,610 And yes, there is. 57 00:05:35,630 --> 00:05:37,460 We can use an anonymous class. 58 00:05:37,880 --> 00:05:40,250 If you look at this class, it's just a placeholder. 59 00:05:40,250 --> 00:05:44,720 It doesn't have any fields, it doesn't have any special methods, its only purpose. 60 00:05:44,720 --> 00:05:49,130 It's only there so that we can create an object of a class that is of type runnable. 61 00:05:49,700 --> 00:05:52,490 What if I told you we can just remove the class altogether? 62 00:05:58,580 --> 00:06:00,590 And if we just have the interface of Runnable. 63 00:06:02,350 --> 00:06:05,360 Obviously, we can't create a runnable object, we'd get an error. 64 00:06:05,380 --> 00:06:06,460 It's just an interface. 65 00:06:06,760 --> 00:06:08,590 But if we add two brackets. 66 00:06:09,880 --> 00:06:16,780 This syntax means, OK, we're creating an object of an anonymous class that implements the runnable 67 00:06:16,780 --> 00:06:23,020 interface and this anonymous class, it doesn't have a name, but it has to override the method of the 68 00:06:23,020 --> 00:06:24,310 interface it implements. 69 00:06:24,700 --> 00:06:28,240 In this case, this anonymous class has to override the run method. 70 00:06:31,170 --> 00:06:33,150 Here is where you would invoke your long task. 71 00:06:35,830 --> 00:06:38,160 We were on the up and everything works beautifully. 72 00:06:39,950 --> 00:06:43,220 But I don't know about you, this is also a pretty ugly. 73 00:06:48,080 --> 00:06:49,310 Here's a rule of thumb for you. 74 00:06:49,490 --> 00:06:55,850 If the interface only holds one method, we can replace an anonymous class with a lambda expression. 75 00:06:56,720 --> 00:07:02,030 Here is where you would put in your task and magically what you put here is going to stand in for the 76 00:07:02,030 --> 00:07:02,690 run method. 77 00:07:03,020 --> 00:07:08,430 And so now, when our thread calls Runnable Run behind the scenes, it's not passing in any arguments. 78 00:07:08,660 --> 00:07:12,290 So it follows that the lambda expression receives no parameters. 79 00:07:14,030 --> 00:07:19,520 And the arrow key points to a block of code that's going to get executed in this case, what gets executed 80 00:07:19,520 --> 00:07:21,110 is our long running task. 81 00:07:23,400 --> 00:07:25,110 And this, to me, looks a lot better. 82 00:07:25,530 --> 00:07:31,680 So back here, we can delete this monstrosity and when possible, I try to avoid creating anonymous 83 00:07:31,680 --> 00:07:33,570 classes and just use a lambda expression. 84 00:07:34,050 --> 00:07:37,320 Thank goodness for Java eight and lambda expressions. 85 00:07:37,320 --> 00:07:41,010 They really were a milestone in programming, rewriting our code. 86 00:07:42,550 --> 00:07:43,690 Everything works really well. 87 00:07:47,240 --> 00:07:50,330 Concurrency makes progress on more than one task at a time. 88 00:07:52,270 --> 00:07:55,480 Time intensive tasks should never blocked the main thread. 89 00:07:55,960 --> 00:08:01,780 Time intensive tasks should run on a separate thread as our normal code ran on the main thread. 90 00:08:01,960 --> 00:08:05,320 We executed the long running code, any background thread.