1 00:00:00,460 --> 00:00:03,400 In this workbook, we're going to monitor the thread states. 2 00:00:03,430 --> 00:00:06,640 First, we're going to place comments on this scanner related code. 3 00:00:07,450 --> 00:00:11,650 And now from the main thread, we're going to print the state of threat to when it's new. 4 00:00:12,160 --> 00:00:13,270 So here's what I'll say. 5 00:00:13,450 --> 00:00:16,030 Or before the thread starts, we'll have to print. 6 00:00:17,210 --> 00:00:19,430 Threat to get states. 7 00:00:20,960 --> 00:00:21,950 Now, not that. 8 00:00:23,440 --> 00:00:24,700 And if we run the application. 9 00:00:26,300 --> 00:00:30,290 The threat has yet to be created at this point in the runtime, so at this point it is new. 10 00:00:31,070 --> 00:00:33,860 But after we start the thread, if we print the state here. 11 00:00:36,820 --> 00:00:37,840 It's currently running. 12 00:00:38,380 --> 00:00:38,740 OK. 13 00:00:40,130 --> 00:00:47,310 Now, for a task for we want to print the state of thread to when it's terminated, and the requirement 14 00:00:47,310 --> 00:00:52,800 was that we can only sleep the main thread for 10 seconds or 10 milliseconds, I should say. 15 00:00:53,550 --> 00:01:00,930 If I do that thread, not sleep, if I sleep the main thread for a 10 milliseconds and then after sleeping 16 00:01:00,930 --> 00:01:01,470 the thread. 17 00:01:03,680 --> 00:01:08,270 Printing the state of threat to war that give threat to enough time to finish. 18 00:01:09,860 --> 00:01:10,430 I wonder. 19 00:01:11,150 --> 00:01:13,610 Oh, it throws and interrupted exception. 20 00:01:13,850 --> 00:01:16,190 So we're going to have to catch this exception as well. 21 00:01:16,650 --> 00:01:20,800 Catch interrupted exception in case the main thread gets interrupted. 22 00:01:22,850 --> 00:01:25,130 We're going to print the exceptions message. 23 00:01:27,010 --> 00:01:29,020 OK, if we run the code. 24 00:01:31,320 --> 00:01:34,050 And it's still a runnable at this point. 25 00:01:34,440 --> 00:01:39,690 So even though we slept the main thread for 10 milliseconds, Thread two was still running. 26 00:01:40,530 --> 00:01:44,370 Clearly, this task takes a lot more than 10 milliseconds to process. 27 00:01:44,790 --> 00:01:47,790 So what we need to do is interrupt the thread right here. 28 00:01:48,660 --> 00:01:51,880 We're going to say thread to interrupt. 29 00:01:52,650 --> 00:01:54,630 And as soon as we interrupt, thread to. 30 00:01:56,920 --> 00:02:03,070 We need to stop this function from performing its task, so we'll say if the current thread that's running 31 00:02:03,070 --> 00:02:08,229 this code, if it's interrupted, then just return right away and just, you know, return zero. 32 00:02:10,860 --> 00:02:11,910 What's wrong one? 33 00:02:16,090 --> 00:02:20,500 And now it should definitely not take more than 10 milliseconds for this to run. 34 00:02:20,920 --> 00:02:24,400 So the tasks should be relatively short. 35 00:02:25,570 --> 00:02:26,260 Nice. 36 00:02:26,290 --> 00:02:31,240 And at this point, it's runnable, and at this point it's terminated. 37 00:02:31,300 --> 00:02:32,050 Perfect. 38 00:02:33,100 --> 00:02:40,480 OK, now in preparation for part three, what I want you to do is Command Z Mac or Control Z Windows. 39 00:02:40,780 --> 00:02:42,970 Everything we did for this workbook. 40 00:02:45,470 --> 00:02:50,630 This was only for knowledge purposes we're not going to be going through with it in the remaining exercises. 41 00:02:51,410 --> 00:02:53,990 So until your scanner code gets on, commented. 42 00:02:56,230 --> 00:02:59,050 Keep reversing your changes and voila.