1 00:00:00,810 --> 00:00:01,710 ‫Welcome back. 2 00:00:01,740 --> 00:00:07,110 ‫In the last video, you saw how to use threads and how you can set up threats and simply start them. 3 00:00:07,320 --> 00:00:11,970 ‫And in this video, we are going to have a look at completion. 4 00:00:11,970 --> 00:00:20,100 ‫So we want to use a thread to do something and only if a specific task is done, then we want to go 5 00:00:20,100 --> 00:00:20,460 ‫ahead. 6 00:00:20,490 --> 00:00:22,450 ‫So let's have a look at that. 7 00:00:22,470 --> 00:00:33,570 ‫I will create a new variable called task completion source and therefore I need new task completion 8 00:00:33,570 --> 00:00:36,210 ‫source and a boolean. 9 00:00:36,360 --> 00:00:41,760 ‫So as you can see, it's complaining because it doesn't know what this task completion source is. 10 00:00:41,760 --> 00:00:48,570 ‫And in order to fix it I need to use sys threading tasks. 11 00:00:48,570 --> 00:00:50,280 ‫So I'm going to do that for a second. 12 00:00:50,610 --> 00:00:51,690 ‫And then here. 13 00:00:52,640 --> 00:00:55,760 ‫What I'm going to do next is I'm just going to create a test variable. 14 00:00:55,770 --> 00:01:01,130 ‫I'm just going to call a task, which is a task completion source, or we just call this task completion 15 00:01:01,130 --> 00:01:01,910 ‫source. 16 00:01:02,420 --> 00:01:06,860 ‫And we set its task to the result. 17 00:01:06,860 --> 00:01:09,200 ‫So we want to have the result of that task. 18 00:01:09,200 --> 00:01:14,060 ‫So let's say there was a specific task that should run and do something, and then we want to see the 19 00:01:14,060 --> 00:01:14,450 ‫result. 20 00:01:14,450 --> 00:01:20,060 ‫So if it worked out or it didn't work out and to test that, I'm going to get rid of this red line here. 21 00:01:20,210 --> 00:01:25,460 ‫So I'm just going to run the code as if there is nothing else in this main method, only this code here. 22 00:01:25,460 --> 00:01:27,020 ‫So I'm going to run it. 23 00:01:28,160 --> 00:01:30,260 ‫And as you can see, nothing happens. 24 00:01:30,260 --> 00:01:36,860 ‫So the console is kept alive or open, even though we don't have this console red line, which we usually 25 00:01:36,860 --> 00:01:44,450 ‫always use if we want to keep the console open, that means that there is something that is not finished. 26 00:01:44,450 --> 00:01:50,360 ‫So this here is never actually finished because there is no result. 27 00:01:50,360 --> 00:01:55,400 ‫So this completion source is never actually complete, so it's never set to true. 28 00:01:56,090 --> 00:01:58,100 ‫And that's something that we can do manually. 29 00:01:58,190 --> 00:02:05,240 ‫So let's say we have a task and we want to have the result as we have it here, but we actually want 30 00:02:05,240 --> 00:02:06,740 ‫to finish that task at one point. 31 00:02:06,740 --> 00:02:11,390 ‫So what do we need to do for that is to create a thread. 32 00:02:11,870 --> 00:02:15,830 ‫So I'm going to create a var thread 33 00:02:18,320 --> 00:02:28,190 ‫is equal to new thread and that thread will not be started straight away. 34 00:02:28,190 --> 00:02:31,130 ‫So I'm using a little different approach than we did here. 35 00:02:31,130 --> 00:02:36,530 ‫So here we just said new thread and then we had the body of the thread. 36 00:02:36,530 --> 00:02:42,020 ‫So to code that should be executed whenever this thread is initiated and we initiated straight away, 37 00:02:42,080 --> 00:02:43,520 ‫we started straight away. 38 00:02:43,760 --> 00:02:49,370 ‫But here we only create the thread and we can do stuff with it later on. 39 00:02:49,370 --> 00:02:53,510 ‫So here I'm just going to say thread sleep. 40 00:02:53,510 --> 00:03:01,880 ‫So just sleep for let's say 1000 milliseconds and afterwards try to set that task completion source 41 00:03:02,000 --> 00:03:06,680 ‫to true so tri set result. 42 00:03:06,980 --> 00:03:13,100 ‫What that will do is attempt to transition the underlying task into the task status ran to completion 43 00:03:13,100 --> 00:03:19,190 ‫state, which means it's done right so it's going to try to set its done to true. 44 00:03:19,760 --> 00:03:23,330 ‫So that means that this task completion source will be set to true. 45 00:03:23,330 --> 00:03:28,520 ‫And only then we will get this VAR, which is the result of that task. 46 00:03:30,020 --> 00:03:32,720 ‫So let's create a little. 47 00:03:34,000 --> 00:03:34,990 ‫Step in here. 48 00:03:34,990 --> 00:03:39,700 ‫So debug step and let's check the thread IDs. 49 00:03:39,760 --> 00:03:45,610 ‫And now in order to start the thread we actually need to call thread start. 50 00:03:45,610 --> 00:03:54,520 ‫And as you can see this thread object here which is a thread which will do all of that is going to be 51 00:03:54,520 --> 00:03:55,120 ‫started. 52 00:03:55,120 --> 00:04:00,610 ‫So I'm going to start the thread like this and while we're at it, let's have a look at what thread 53 00:04:00,610 --> 00:04:01,360 ‫has to offer. 54 00:04:01,360 --> 00:04:08,680 ‫So whenever you create a thread object, there are multiple properties and methods to use. 55 00:04:08,680 --> 00:04:10,120 ‫Or we can use multiple ones here. 56 00:04:10,120 --> 00:04:16,990 ‫So you can see a board is one where you can abort the thread so you can stop the thread, but that will 57 00:04:17,290 --> 00:04:19,780 ‫run an exception or it will cause an exception. 58 00:04:19,780 --> 00:04:22,450 ‫So that's very dangerous to do actually. 59 00:04:22,450 --> 00:04:26,800 ‫So you can crash your application with that based on how security have built it. 60 00:04:26,800 --> 00:04:32,530 ‫But a board is something that you can of course use to stop a thread, but you never know how far it 61 00:04:32,530 --> 00:04:33,580 ‫is in its execution. 62 00:04:33,580 --> 00:04:40,540 ‫So it can happen that it is not done with the tasks that you wanted to be done with and that will cause 63 00:04:40,540 --> 00:04:43,030 ‫to, well, your program not working correctly. 64 00:04:43,270 --> 00:04:49,180 ‫And then of course, there are plenty of other methods that are pretty interesting like or properties 65 00:04:49,180 --> 00:04:54,610 ‫like this is alive, which means it's this thread currently alive then is background, which means we 66 00:04:54,610 --> 00:04:59,260 ‫can say that a thread should run in the background and we can check with this property. 67 00:04:59,260 --> 00:05:02,470 ‫Whether it's in background or not is a threat to thread. 68 00:05:02,470 --> 00:05:04,270 ‫That's something that we will check out later on. 69 00:05:04,270 --> 00:05:05,950 ‫So what is a thread pool? 70 00:05:05,950 --> 00:05:08,560 ‫That's what we'll see later on and so forth. 71 00:05:08,560 --> 00:05:15,130 ‫So the managed thread ID is something that is pretty interesting, which will tell us about the ID of 72 00:05:15,130 --> 00:05:16,720 ‫the thread that we are currently running. 73 00:05:16,720 --> 00:05:20,230 ‫So let's use that, let's print that onto the console. 74 00:05:20,230 --> 00:05:26,620 ‫So see W and I'm going to use the thread number as this. 75 00:05:26,620 --> 00:05:35,590 ‫So you use the dollar sign and then you have the thread number or thread ID and so you can call it. 76 00:05:35,590 --> 00:05:43,600 ‫And then in curly brackets you use the thread managed ID, so. 77 00:05:45,430 --> 00:05:47,950 ‫Thread managed thread ID. 78 00:05:48,280 --> 00:05:50,800 ‫And of course you need to close the string. 79 00:05:52,470 --> 00:05:53,860 ‫All right, now let's test it. 80 00:05:53,880 --> 00:05:54,840 ‫Let's run the code. 81 00:05:57,860 --> 00:06:00,920 ‫And we are we have our main thread. 82 00:06:01,070 --> 00:06:03,740 ‫And so far we have something on the console. 83 00:06:03,740 --> 00:06:06,220 ‫As you can see, thread number is three. 84 00:06:06,230 --> 00:06:13,010 ‫So the number of this thread that we have here that we've created ourselves is actually three and the 85 00:06:13,010 --> 00:06:16,160 ‫main thread it's managed ID is one. 86 00:06:16,280 --> 00:06:21,830 ‫So as you can see, the numbers don't actually have to be in the manner that you would expect them. 87 00:06:21,860 --> 00:06:28,220 ‫So even though we haven't created our own additional thread in here, it goes from 1 to 3. 88 00:06:28,220 --> 00:06:32,660 ‫So there have to be other threads that are running that we don't see here. 89 00:06:32,690 --> 00:06:33,380 ‫All right. 90 00:06:35,650 --> 00:06:41,330 ‫And by the way, if you want to see whether a thread has started and end it, you can do the following. 91 00:06:41,350 --> 00:06:42,220 ‫So. 92 00:06:42,400 --> 00:06:46,750 ‫So let's cut that here and put it in this line here. 93 00:06:46,840 --> 00:06:50,500 ‫And actually, let's say started and here. 94 00:06:50,500 --> 00:06:54,790 ‫We cannot use thread because thread is this thread that we create here. 95 00:06:54,790 --> 00:07:01,930 ‫But if we want to use it in here within the thread body itself, then we need to use thread dot current 96 00:07:01,930 --> 00:07:02,650 ‫thread. 97 00:07:03,580 --> 00:07:08,470 ‫So if we do that now, we will have the thread that we are currently in, which is exactly that. 98 00:07:08,470 --> 00:07:14,020 ‫And here we say it started and after it's done we say it's ended. 99 00:07:14,980 --> 00:07:16,720 ‫And now let's run it. 100 00:07:18,490 --> 00:07:19,630 ‫It's run the code. 101 00:07:19,630 --> 00:07:23,560 ‫And as you can see, nothing is displayed on the console. 102 00:07:23,830 --> 00:07:25,750 ‫Let's go to the next step. 103 00:07:27,110 --> 00:07:28,790 ‫And then it started and ended. 104 00:07:28,790 --> 00:07:31,040 ‫So it happened quite quickly. 105 00:07:31,130 --> 00:07:32,990 ‫But as you can see, there are. 106 00:07:33,500 --> 00:07:35,130 ‫So let's give it a little more time. 107 00:07:35,150 --> 00:07:37,310 ‫Let's change it to 5 seconds. 108 00:07:37,310 --> 00:07:41,540 ‫And let's also get rid of this debug point here and let's run it. 109 00:07:42,380 --> 00:07:49,730 ‫So thread number three started and then it took a while and it went to finished. 110 00:07:49,910 --> 00:07:57,800 ‫So now you see that this line of code is actually executed correctly and it simply stops our console. 111 00:07:57,950 --> 00:08:04,910 ‫So let's get the console read line to keep the console open. 112 00:08:05,960 --> 00:08:12,800 ‫So thread number three started and then 5 seconds later it should end so that we are thread number three 113 00:08:12,800 --> 00:08:13,400 ‫ended. 114 00:08:16,780 --> 00:08:19,780 ‫Now let's say we want to see the task actually worked out. 115 00:08:19,780 --> 00:08:22,670 ‫So if it worked out to set it to true or not. 116 00:08:22,690 --> 00:08:25,810 ‫Well, we know because otherwise we wouldn't get to the next step. 117 00:08:25,810 --> 00:08:29,230 ‫But let's use console right line in here. 118 00:08:29,500 --> 00:08:38,230 ‫Task was done and here I'm just going to write the result which will be test. 119 00:08:40,020 --> 00:08:45,060 ‫So test is just a bowl, as you can see here, because either it worked or it didn't work. 120 00:08:45,060 --> 00:08:47,040 ‫So we sell this either true or false. 121 00:08:47,370 --> 00:08:48,810 ‫So let's check it out. 122 00:08:50,440 --> 00:08:53,470 ‫And now we need to wait for 5 seconds. 123 00:08:53,680 --> 00:09:00,670 ‫Number three started 5 seconds and then number three ended and it says task was done is equal to true. 124 00:09:00,940 --> 00:09:03,520 ‫So that's what's happening here with this line. 125 00:09:03,520 --> 00:09:06,370 ‫So it simply says it's done with this specific task. 126 00:09:08,230 --> 00:09:08,560 ‫All right. 127 00:09:08,560 --> 00:09:13,510 ‫So in this video, you saw how you can use the task completion source, which indicates whether a task 128 00:09:13,510 --> 00:09:14,260 ‫is done or not. 129 00:09:14,270 --> 00:09:18,460 ‫You can set it to true or it can simply do something and be done. 130 00:09:18,490 --> 00:09:23,530 ‫After a while, you also saw that you can create a thread in a different manner, so you can actually 131 00:09:23,530 --> 00:09:30,100 ‫create a thread variable, which you then have to start of course, and you can simply use like an object 132 00:09:30,100 --> 00:09:32,350 ‫of the class thread. 133 00:09:32,740 --> 00:09:40,270 ‫And then we saw how we can use the manage thread ID so we know the ID of that specific thread. 134 00:09:40,270 --> 00:09:45,610 ‫And that's something that we'll look into a little more in the next video where we create multiple threads 135 00:09:45,610 --> 00:09:51,220 ‫at the same time and then we will see how it's going to be handled by our machine. 136 00:09:51,220 --> 00:09:52,660 ‫So see you in the next video.