1 00:00:00,480 --> 00:00:01,290 ‫Welcome back. 2 00:00:01,320 --> 00:00:05,400 ‫In this video I would like to talk about join and is alive. 3 00:00:05,400 --> 00:00:12,750 ‫So join is a method that thread or the thread class has and that we can use and is alive as a property 4 00:00:12,750 --> 00:00:13,770 ‫that it has. 5 00:00:13,770 --> 00:00:19,560 ‫So let's create a thread in here or actually two threads and I have created two methods as well the 6 00:00:19,560 --> 00:00:22,170 ‫thread, one function and the thread to function. 7 00:00:22,170 --> 00:00:25,020 ‫And what they do is just say that they have started. 8 00:00:25,020 --> 00:00:29,070 ‫So third one function started and thread to function started. 9 00:00:29,070 --> 00:00:34,770 ‫So now you can go ahead and just copy that or build yourself real quick and then you can go ahead. 10 00:00:34,770 --> 00:00:48,210 ‫So in here I'm just going to say the domain thread started and at the end it's going to say main thread 11 00:00:48,210 --> 00:00:49,080 ‫ended. 12 00:00:50,160 --> 00:00:51,810 ‫So so far nothing new. 13 00:00:51,810 --> 00:00:58,350 ‫If we start that with Control five, for example, then the console stays awake. 14 00:00:58,350 --> 00:01:01,050 ‫Main thread started and main thread ended. 15 00:01:01,050 --> 00:01:02,520 ‫Press any key to continue. 16 00:01:02,520 --> 00:01:02,820 ‫Okay. 17 00:01:02,880 --> 00:01:07,230 ‫So you can see it started and it ended and that happened real, real quick. 18 00:01:07,230 --> 00:01:09,690 ‫So nothing much happening here. 19 00:01:09,930 --> 00:01:10,950 ‫So now let's change that. 20 00:01:10,950 --> 00:01:12,540 ‫Let's create some threads in there. 21 00:01:12,540 --> 00:01:16,170 ‫So let's create a new thread called thread one. 22 00:01:17,520 --> 00:01:19,710 ‫So thread. 23 00:01:19,710 --> 00:01:26,490 ‫And in order to use a thread we need to implement using system threading. 24 00:01:26,490 --> 00:01:30,030 ‫So now we can use threads and I'm going to call that one thread one. 25 00:01:30,510 --> 00:01:38,010 ‫And what it's going to do is it's simply going to be a new thread that calls the thread function one 26 00:01:38,010 --> 00:01:39,540 ‫or thread one function. 27 00:01:39,540 --> 00:01:44,370 ‫So just going to run this code here, it's just going to call them the method. 28 00:01:44,820 --> 00:01:53,940 ‫And next, another thread, I'm going to call it thread two, which is also going to be a thread and 29 00:01:53,940 --> 00:01:56,400 ‫here thread to function. 30 00:01:56,490 --> 00:02:02,370 ‫So as you can see, there are multiple overloads thread start, parameterized thread start and thread 31 00:02:02,370 --> 00:02:02,760 ‫start. 32 00:02:02,760 --> 00:02:05,460 ‫So the one that we do is just this thread start. 33 00:02:05,460 --> 00:02:11,730 ‫So start is a thread start delegate that represents the methods to be invoked when the thread begins 34 00:02:11,730 --> 00:02:12,480 ‫executing. 35 00:02:12,480 --> 00:02:15,990 ‫So this is going to be our method therefore. 36 00:02:15,990 --> 00:02:21,240 ‫So this thread one or this third one function, which is not really a method, it's a function because 37 00:02:21,240 --> 00:02:24,270 ‫it's not within an object oriented class. 38 00:02:24,270 --> 00:02:27,960 ‫But anyways, let's go ahead and create that function. 39 00:02:27,960 --> 00:02:33,780 ‫As you can see, you don't need the brackets here because we don't actually call it this a delegate 40 00:02:33,780 --> 00:02:34,230 ‫here. 41 00:02:34,440 --> 00:02:34,770 ‫All right. 42 00:02:34,770 --> 00:02:39,660 ‫So next, what we need to do is simply start those threads. 43 00:02:39,660 --> 00:02:47,340 ‫So let's do that thread one dot start and thread to start. 44 00:02:49,080 --> 00:02:55,770 ‫And I'm just going to press control f five to start it and do see main thread started, main thread 45 00:02:55,770 --> 00:02:59,730 ‫ended, thread one function started and third to function started. 46 00:02:59,730 --> 00:03:04,170 ‫So they didn't end because we haven't implemented any ending message yet. 47 00:03:05,350 --> 00:03:08,660 ‫So now let's go ahead and use the joint method on those two threads. 48 00:03:08,680 --> 00:03:13,600 ‫So thread one dot join and let's check out what joined us. 49 00:03:13,690 --> 00:03:19,510 ‫Joint blocks the calling thread until the red represented by this instance terminates while continuing 50 00:03:19,510 --> 00:03:22,320 ‫to perform standard com and send message pumping. 51 00:03:22,330 --> 00:03:27,190 ‫So what we will do is simply com or join those two threads. 52 00:03:27,190 --> 00:03:32,980 ‫So this thread with the main thread, that's what this joint method does and it blocks the main thread 53 00:03:32,980 --> 00:03:34,000 ‫for that time. 54 00:03:34,000 --> 00:03:36,040 ‫So it's called by the main thread. 55 00:03:36,040 --> 00:03:37,690 ‫That's why it blocks the main thread. 56 00:03:37,690 --> 00:03:40,720 ‫So it's generally called blocking the calling thread. 57 00:03:40,720 --> 00:03:45,310 ‫And in our case the calling thread is the main thread because we are within the main thread when we 58 00:03:45,310 --> 00:03:45,940 ‫call it. 59 00:03:46,840 --> 00:03:53,620 ‫So what we do then is also join the second thread. 60 00:03:53,620 --> 00:03:58,540 ‫So we join this to those two and let's write something onto the console. 61 00:03:58,540 --> 00:04:03,310 ‫CW And let's say thread one function 62 00:04:05,320 --> 00:04:06,040 ‫done. 63 00:04:09,270 --> 00:04:11,340 ‫And let's do the same for the second one. 64 00:04:12,180 --> 00:04:16,200 ‫So let's run it again and nothing much will happen actually. 65 00:04:16,200 --> 00:04:16,560 ‫Let me. 66 00:04:16,560 --> 00:04:19,470 ‫Press Controller five So my console stays awake. 67 00:04:19,470 --> 00:04:23,850 ‫Main thread started the third one function started to function started. 68 00:04:23,850 --> 00:04:27,330 ‫And here it should be called thread to function. 69 00:04:27,690 --> 00:04:34,290 ‫All right, so the we are now it says thread one and thread two is done and afterwards only main thread 70 00:04:34,290 --> 00:04:34,890 ‫is done. 71 00:04:34,890 --> 00:04:37,560 ‫So that's nothing fancy, nothing new. 72 00:04:37,560 --> 00:04:40,410 ‫It just executes dev stuff step by step. 73 00:04:40,410 --> 00:04:42,180 ‫So that's how it looks like here. 74 00:04:42,180 --> 00:04:44,910 ‫But then let's do something interesting. 75 00:04:44,910 --> 00:04:46,830 ‫Let's add a sleep in here. 76 00:04:47,220 --> 00:04:54,180 ‫So actually let's make the thread sleep for 3 seconds. 77 00:04:54,180 --> 00:04:58,950 ‫So thread sleep, 3000 milliseconds. 78 00:05:00,450 --> 00:05:06,420 ‫And what that will do is that this method is just going to say the method, the thread started and then 79 00:05:06,420 --> 00:05:08,160 ‫it's going to wait for 3 seconds. 80 00:05:08,160 --> 00:05:13,560 ‫So now let's run it again and actually let's run it again with control f five. 81 00:05:14,340 --> 00:05:16,410 ‫So here, function, start it. 82 00:05:18,230 --> 00:05:21,710 ‫And then after a while, thread one function. 83 00:05:21,710 --> 00:05:22,290 ‫Done. 84 00:05:22,310 --> 00:05:25,670 ‫Let me actually reproduce that real quick. 85 00:05:26,990 --> 00:05:33,680 ‫So third one and to start it and after 3 seconds, you can see third one function done, third to function 86 00:05:33,680 --> 00:05:35,040 ‫done and main thread ended. 87 00:05:35,060 --> 00:05:40,850 ‫So the main thread is waits until the main or the third one is done. 88 00:05:40,850 --> 00:05:42,800 ‫And that's happening because we join it here. 89 00:05:42,800 --> 00:05:46,730 ‫So we join thread one and we say, okay, please wait until we're done. 90 00:05:47,720 --> 00:05:52,340 ‫Now if we look at the joint method, there are other interesting thing to consider. 91 00:05:52,730 --> 00:05:59,510 ‫There are multiple overloads, so as you can see, there is only one or we have zero parameters here 92 00:05:59,510 --> 00:06:02,060 ‫or arguments that we've given, but there are multiple ones. 93 00:06:02,060 --> 00:06:10,280 ‫So we can add another argument in here, which is the time that we want to wait before we go ahead. 94 00:06:10,400 --> 00:06:20,840 ‫So in here, I'm just going to use if thread dot actually it's third one that join takes up a specific 95 00:06:20,840 --> 00:06:24,170 ‫amount of time and you can see milliseconds timeout. 96 00:06:24,170 --> 00:06:32,690 ‫So let's say we wait for 1/2 and we'll give this thread 1/2 to terminate and if it does that within 97 00:06:32,690 --> 00:06:46,610 ‫time we say thread one function done, and otherwise we say something like thread one function wasn't 98 00:06:47,300 --> 00:06:50,180 ‫done within 1/2. 99 00:06:52,410 --> 00:07:02,550 ‫All right, so let's get rid of this joint here and let's actually replace all of that code with this. 100 00:07:03,630 --> 00:07:05,220 ‫So now let's run it again. 101 00:07:06,870 --> 00:07:08,160 ‫Control of five. 102 00:07:09,320 --> 00:07:16,060 ‫So front function one third wasn't done within 1/2 thread to function done main thread ended. 103 00:07:16,070 --> 00:07:20,390 ‫So as you can see, it wasn't done within the specified period of time. 104 00:07:20,390 --> 00:07:25,580 ‫But now let's add another code in here because we never saw this thread. 105 00:07:25,580 --> 00:07:26,870 ‫One function done. 106 00:07:26,870 --> 00:07:38,420 ‫So let's add something like thread one function coming back to main. 107 00:07:39,650 --> 00:07:41,660 ‫So that means it's pretty much done. 108 00:07:41,780 --> 00:07:44,060 ‫Or maybe not main. 109 00:07:44,060 --> 00:07:50,600 ‫But to color, let's run it again with controller five. 110 00:07:52,810 --> 00:08:01,210 ‫So threat to functions started let me actually call it once again like then OC main threat ended and 111 00:08:01,210 --> 00:08:08,110 ‫then you see only after that time so only after its 3 seconds it said third one function coming back 112 00:08:08,110 --> 00:08:08,620 ‫to color. 113 00:08:08,620 --> 00:08:13,960 ‫So the main threat was already done and only then it came back to the color, which was the main threat. 114 00:08:14,050 --> 00:08:18,340 ‫Only then it came to well, line 30 or actually afterwards. 115 00:08:18,340 --> 00:08:23,170 ‫So it was done with the main method and only then this code here was executed. 116 00:08:25,020 --> 00:08:29,210 ‫So as you can see, it only blocked the main thread for 1/2. 117 00:08:29,220 --> 00:08:31,260 ‫It didn't block the main threat until it was done. 118 00:08:31,260 --> 00:08:36,810 ‫So it says blocks the calling thread until the thread represented by this instance terminates or the 119 00:08:36,810 --> 00:08:41,610 ‫specified time elapses while continuing to perform static comments and merged pumping. 120 00:08:41,610 --> 00:08:48,060 ‫So what you can see here is you can say, okay, main thread, please wait for 1/2 and if it doesn't 121 00:08:48,090 --> 00:08:52,560 ‫happen or if the third one doesn't finish its stuff, then it's going to go on. 122 00:08:52,560 --> 00:08:57,600 ‫So it's going to join thread two and then it's just going to say, okay, I'm done, thread main threat 123 00:08:57,600 --> 00:09:04,530 ‫is done and well thread one function is still running or thread one is still running and it's still 124 00:09:04,530 --> 00:09:07,230 ‫sleeping and executing the code afterwards. 125 00:09:07,350 --> 00:09:10,530 ‫But it doesn't do that in time as you can see here. 126 00:09:10,830 --> 00:09:12,420 ‫So the main thread didn't wait anymore. 127 00:09:12,420 --> 00:09:15,330 ‫It just said, okay, I'm going to go on, I'm going to move on without you. 128 00:09:16,290 --> 00:09:19,260 ‫So that's the thing about joining threads. 129 00:09:19,260 --> 00:09:21,240 ‫You can see here, that's how it works. 130 00:09:21,240 --> 00:09:24,330 ‫And then now let's have a look at this is a live property. 131 00:09:24,330 --> 00:09:33,330 ‫So if thread one is alive so there is this property and as you can see gets a value indicating the execution 132 00:09:33,330 --> 00:09:38,850 ‫status of the current thread, which means is the thread done or is it not done? 133 00:09:38,850 --> 00:09:42,300 ‫So it says here if the thread is alive still. 134 00:09:42,300 --> 00:09:48,120 ‫So if it's still running at the point where this code is executed, then do something. 135 00:09:48,120 --> 00:09:56,640 ‫So I'm just going to write something like thread one is still doing stuff, so it's still executing 136 00:09:56,640 --> 00:10:07,170 ‫and running code and otherwise I'm just going to say thread one was completed so CW thread one completed. 137 00:10:08,850 --> 00:10:09,450 ‫All right. 138 00:10:10,920 --> 00:10:14,220 ‫And here of course, we need to finish the string now. 139 00:10:14,220 --> 00:10:15,810 ‫Let's check it out again. 140 00:10:18,470 --> 00:10:22,580 ‫And main thread ended and actually my console will close. 141 00:10:22,580 --> 00:10:28,700 ‫So let me start with control f five main thread started, thread one function started and so forth. 142 00:10:28,700 --> 00:10:30,590 ‫So it wasn't done within 1/2. 143 00:10:31,100 --> 00:10:33,530 ‫Then thread function two was done. 144 00:10:33,530 --> 00:10:36,050 ‫So it says, okay, I'm not going to wait anymore. 145 00:10:36,050 --> 00:10:36,680 ‫The main thread. 146 00:10:36,680 --> 00:10:39,050 ‫So it executes thread two then. 147 00:10:40,010 --> 00:10:41,900 ‫Third one is still doing stuff. 148 00:10:42,050 --> 00:10:47,830 ‫So it means that thread one is alive, is executed, and only afterwards main threat is ended. 149 00:10:47,840 --> 00:10:56,510 ‫So this code here is executed, line 38 and then our thread one function coming back to color is called 150 00:10:56,960 --> 00:10:58,220 ‫or is executed. 151 00:10:59,060 --> 00:10:59,450 ‫All right. 152 00:10:59,450 --> 00:11:00,630 ‫So that's what you can check. 153 00:11:00,650 --> 00:11:01,430 ‫You can simply check. 154 00:11:01,430 --> 00:11:01,670 ‫Okay. 155 00:11:01,700 --> 00:11:02,930 ‫Is the threat alive or not? 156 00:11:02,930 --> 00:11:06,650 ‫And based on that, you can execute stuff and that's pretty useful. 157 00:11:07,280 --> 00:11:13,430 ‫So now let's say we want to see that the threat one is still doing stuff on a regular basis. 158 00:11:13,430 --> 00:11:22,550 ‫So let's say we use a for loop here and I is equals zero I below ten I plus plus. 159 00:11:23,120 --> 00:11:26,930 ‫So within that for loop I want to run this code. 160 00:11:26,930 --> 00:11:31,280 ‫And what it's going to do is it's just going to say threat is still doing stuff and then it's going 161 00:11:31,280 --> 00:11:34,160 ‫to sleep for, let's say, 300 milliseconds. 162 00:11:34,160 --> 00:11:42,080 ‫So sleep for 300, which means we will have ten executions of that before our threat is completed. 163 00:11:42,080 --> 00:11:44,510 ‫So let's execute that once again. 164 00:11:45,260 --> 00:11:47,480 ‫Thread one is still doing stuff done, done, done. 165 00:11:47,480 --> 00:11:55,430 ‫And as you can see, it didn't execute the ten times even though threat sleep was there for 300 or 3000 166 00:11:55,430 --> 00:11:59,000 ‫seconds and milliseconds and this one was only for 300 milliseconds. 167 00:12:00,150 --> 00:12:05,240 ‫So what it then did is it executed thread one completed multiple times. 168 00:12:05,250 --> 00:12:11,150 ‫So as you can see there is a overlap where the thread function is coming back. 169 00:12:11,160 --> 00:12:16,020 ‫As you can see, this for loop only executed a little later. 170 00:12:16,020 --> 00:12:18,840 ‫So the order is the following. 171 00:12:18,840 --> 00:12:19,920 ‫We start thread one. 172 00:12:19,920 --> 00:12:22,680 ‫We start right to start thread one. 173 00:12:22,680 --> 00:12:31,200 ‫Waited for 1/2 so you can see we have this 1/2 join so we already lose three executions of our slip 174 00:12:31,200 --> 00:12:40,410 ‫here, which means this thread one still doing stuff is actually only executed six times here or seven 175 00:12:40,410 --> 00:12:41,100 ‫times. 176 00:12:41,640 --> 00:12:45,840 ‫And then the thread one function comes back because it started. 177 00:12:45,840 --> 00:12:47,250 ‫Then it waited for 1/2. 178 00:12:47,250 --> 00:12:48,120 ‫Nothing happened then. 179 00:12:48,120 --> 00:12:50,850 ‫Only then it started to do all of this beautiful stuff. 180 00:12:50,850 --> 00:12:59,880 ‫So because this for loop is executed ten times, it will run this sleep multiple times until it comes 181 00:12:59,880 --> 00:13:00,780 ‫back to color. 182 00:13:00,780 --> 00:13:04,530 ‫And this third one completed is executed super quickly. 183 00:13:04,620 --> 00:13:08,040 ‫So it's not going to wait because there is no sleep in there. 184 00:13:08,310 --> 00:13:10,650 ‫But that's generally what you can do with this alive. 185 00:13:10,650 --> 00:13:12,930 ‫So you can generally check out, is it the life? 186 00:13:12,930 --> 00:13:13,740 ‫Is it still alive? 187 00:13:13,770 --> 00:13:20,490 ‫Is it still alive and do stuff while it's alive or do other stuff while it's alive and then do nothing 188 00:13:20,490 --> 00:13:22,410 ‫or something else when it's done. 189 00:13:23,850 --> 00:13:31,560 ‫So that's a very basic example of using Join and is alive and I hope you could grasp the concept. 190 00:13:31,560 --> 00:13:33,150 ‫So see you in the next video. 191 00:13:33,150 --> 00:13:34,980 ‫We we'll check out tasks.