1 00:00:00,270 --> 00:00:02,730 One thread can interrupt another thread. 2 00:00:06,110 --> 00:00:09,530 In this lesson, you will interrupt a threat to from the main threat. 3 00:00:13,760 --> 00:00:15,230 Why would you interrupt a thread? 4 00:00:18,590 --> 00:00:23,030 Well, if something goes wrong in one thread, it might not be worth continuing the other thread. 5 00:00:23,600 --> 00:00:26,240 So the first thread can interrupt the other thread. 6 00:00:32,100 --> 00:00:36,690 When you call thread, don't interrupt the thread status becomes interrupted. 7 00:00:40,150 --> 00:00:42,760 So right after starting the thread, what I'm going to do is. 8 00:00:44,450 --> 00:00:45,410 I'm going to print. 9 00:00:46,800 --> 00:00:47,610 Oh, no. 10 00:00:48,330 --> 00:00:50,880 A big error occurred. 11 00:00:53,280 --> 00:00:54,510 In the main thread. 12 00:00:55,680 --> 00:00:58,950 Let's interrupt the background threat. 13 00:01:03,450 --> 00:01:07,890 And then here, what I'll say is thread to interrupt. 14 00:01:12,870 --> 00:01:13,880 OK, run the code. 15 00:01:16,190 --> 00:01:18,140 And everything proceeds as normal. 16 00:01:18,770 --> 00:01:22,010 But where shouldn't the threat have been interrupted as soon as we started it? 17 00:01:22,310 --> 00:01:23,480 Why did it keep going? 18 00:01:24,110 --> 00:01:29,810 Well, going back to this slide, all that happened is that the status of our threat became interrupted. 19 00:01:30,260 --> 00:01:30,830 That's it. 20 00:01:31,550 --> 00:01:35,600 But what we can do is use that interrupted status to our advantage. 21 00:01:36,290 --> 00:01:40,430 So right after we interrupt the thread, we can check the interrupted status. 22 00:01:40,760 --> 00:01:41,870 Here I can print. 23 00:01:43,270 --> 00:01:49,390 Threat to boycott is interrupted, so this basically returns a Boolean that reflects the status of a 24 00:01:49,390 --> 00:01:49,780 threat. 25 00:01:51,140 --> 00:01:51,860 If we print it. 26 00:01:53,580 --> 00:01:55,600 The status is that it is interrupted. 27 00:01:55,620 --> 00:01:56,280 That is true. 28 00:01:57,610 --> 00:02:02,140 So let me just honestly delete all of these print statements there, but in the way. 29 00:02:04,500 --> 00:02:09,630 So that's perfect, so the interrupted status of threat two is true, but that's kind of weird, so 30 00:02:09,630 --> 00:02:11,430 we're making progress on both threads. 31 00:02:13,000 --> 00:02:18,550 Eventually, we get to this line, which supposedly interrupts the second threat, but all it does is 32 00:02:18,550 --> 00:02:24,370 it changes its status and our threat to keeps getting processed with an interrupted status. 33 00:02:29,050 --> 00:02:33,850 What we need to do is actually interrupt threat to as soon as its status becomes interrupted. 34 00:02:35,530 --> 00:02:38,560 So here, what I'm going to say is elseif. 35 00:02:40,620 --> 00:02:46,800 And here what I want to do is check if the current thread that's performing this task thread, that 36 00:02:46,800 --> 00:02:49,740 current thread, if it's interrupted status is true. 37 00:02:52,690 --> 00:02:53,530 Then I'll print. 38 00:02:54,510 --> 00:02:58,410 This task has been interrupted. 39 00:02:59,370 --> 00:03:00,180 Prematurely. 40 00:03:02,160 --> 00:03:03,210 And break the one loop. 41 00:03:05,240 --> 00:03:06,200 We were running our coat. 42 00:03:09,210 --> 00:03:11,670 And that's perfect as soon as our threat starts. 43 00:03:13,200 --> 00:03:14,730 It gets interrupted right away. 44 00:03:15,860 --> 00:03:19,580 If I were to put break points here, here and here. 45 00:03:20,810 --> 00:03:21,950 And debug the runtime. 46 00:03:23,790 --> 00:03:27,540 And as soon as we start our background thread, we see two points of runtime. 47 00:03:27,870 --> 00:03:31,710 That's because our application is making progress on two threads at a time. 48 00:03:33,940 --> 00:03:40,030 And it seems that as soon as we started our second thread, something bad happened in Maine and were 49 00:03:40,030 --> 00:03:42,340 interrupting the background thread to right away. 50 00:03:43,000 --> 00:03:47,170 As soon as we interrupt the background thread, its status should change to interrupted. 51 00:03:47,650 --> 00:03:54,430 And it seems like for me, at least, the main thread keeps running and now the main threat is finished 52 00:03:54,430 --> 00:03:55,120 executing. 53 00:03:55,120 --> 00:03:56,650 We're exiting the main thread. 54 00:03:59,040 --> 00:03:59,520 OK. 55 00:04:01,010 --> 00:04:02,690 But the background threat is still running. 56 00:04:05,860 --> 00:04:10,870 And because it's interrupted state is true, we're breaking the wire loop. 57 00:04:12,850 --> 00:04:14,770 And here we are exiting the second thread. 58 00:04:17,680 --> 00:04:22,420 And by the way, just because my code ran this way doesn't mean your code is going to run the exact 59 00:04:22,420 --> 00:04:22,930 same way. 60 00:04:23,230 --> 00:04:28,810 The task scheduler is extremely random and it's hard to predict how it's going to organize your tasks. 61 00:04:29,230 --> 00:04:31,650 So don't expect the exact same runtime as me. 62 00:04:31,660 --> 00:04:33,430 Concurrent execution is random. 63 00:04:44,680 --> 00:04:47,650 Thread that sleep stops a thread for x milliseconds. 64 00:04:49,540 --> 00:04:52,840 And it throws an interrupted exception when it's interrupted. 65 00:04:55,160 --> 00:04:58,160 Inside the long task, I'm going to say thread sleep. 66 00:04:59,210 --> 00:05:05,840 For 3000 milliseconds, so whatever thread that's currently running this long task, it's going to sleep 67 00:05:05,840 --> 00:05:09,620 for three seconds and after it's done sleeping, we're going to print. 68 00:05:10,780 --> 00:05:11,740 Done sleeping. 69 00:05:15,010 --> 00:05:22,090 And notice that it forces us to try to run the code because this code throws an interrupted exception 70 00:05:22,090 --> 00:05:23,050 when it's interrupted. 71 00:05:24,740 --> 00:05:30,530 And we're going to catch the exception if the code fails or in this case, if the threat gets interrupted, 72 00:05:30,530 --> 00:05:31,640 it's not really a failure. 73 00:05:34,640 --> 00:05:36,320 And then I'm going to comment all of this out. 74 00:05:38,590 --> 00:05:39,850 And now if you rerun the app. 75 00:05:41,560 --> 00:05:43,900 Our application never gets the chance to sleep. 76 00:05:44,560 --> 00:05:45,760 Why do you think that is? 77 00:05:48,450 --> 00:05:54,080 Here's a visual of the runtime, we're making progress on both threads at the same time as threat to 78 00:05:54,090 --> 00:05:54,630 is sleeping. 79 00:05:54,630 --> 00:05:56,820 Eventually, Main interrupts that thread. 80 00:05:57,240 --> 00:06:00,900 The sleep method throws an interrupted exception and our thread finishes. 81 00:06:04,690 --> 00:06:06,280 Now, if we remove the interruption. 82 00:06:12,300 --> 00:06:16,140 Our main thread finishes right away, but our background throughout is still sleeping, and now it's 83 00:06:16,140 --> 00:06:16,410 done. 84 00:06:17,310 --> 00:06:19,140 Here's a visual of the runtime you just saw. 85 00:06:19,410 --> 00:06:24,570 Our main thread pretty much finished right away while our background thread was sleeping for three seconds. 86 00:06:32,100 --> 00:06:34,020 One thread can interrupt another thread. 87 00:06:36,990 --> 00:06:41,160 If something goes wrong in one thread, it might not be worth continuing the other thread.