Welcome back.
In this video I would like to talk about join and is alive.
So join is a method that thread or the thread class has and that we can use and is alive as a property
that it has.
So let's create a thread in here or actually two threads and I have created two methods as well the
thread, one function and the thread to function.
And what they do is just say that they have started.
So third one function started and thread to function started.
So now you can go ahead and just copy that or build yourself real quick and then you can go ahead.
So in here I'm just going to say the domain thread started and at the end it's going to say main thread
ended.
So so far nothing new.
If we start that with Control five, for example, then the console stays awake.
Main thread started and main thread ended.
Press any key to continue.
Okay.
So you can see it started and it ended and that happened real, real quick.
So nothing much happening here.
So now let's change that.
Let's create some threads in there.
So let's create a new thread called thread one.
So thread.
And in order to use a thread we need to implement using system threading.
So now we can use threads and I'm going to call that one thread one.
And what it's going to do is it's simply going to be a new thread that calls the thread function one
or thread one function.
So just going to run this code here, it's just going to call them the method.
And next, another thread, I'm going to call it thread two, which is also going to be a thread and
here thread to function.
So as you can see, there are multiple overloads thread start, parameterized thread start and thread
start.
So the one that we do is just this thread start.
So start is a thread start delegate that represents the methods to be invoked when the thread begins
executing.
So this is going to be our method therefore.
So this thread one or this third one function, which is not really a method, it's a function because
it's not within an object oriented class.
But anyways, let's go ahead and create that function.
As you can see, you don't need the brackets here because we don't actually call it this a delegate
here.
All right.
So next, what we need to do is simply start those threads.
So let's do that thread one dot start and thread to start.
And I'm just going to press control f five to start it and do see main thread started, main thread
ended, thread one function started and third to function started.
So they didn't end because we haven't implemented any ending message yet.
So now let's go ahead and use the joint method on those two threads.
So thread one dot join and let's check out what joined us.
Joint blocks the calling thread until the red represented by this instance terminates while continuing
to perform standard com and send message pumping.
So what we will do is simply com or join those two threads.
So this thread with the main thread, that's what this joint method does and it blocks the main thread
for that time.
So it's called by the main thread.
That's why it blocks the main thread.
So it's generally called blocking the calling thread.
And in our case the calling thread is the main thread because we are within the main thread when we
call it.
So what we do then is also join the second thread.
So we join this to those two and let's write something onto the console.
CW And let's say thread one function
done.
And let's do the same for the second one.
So let's run it again and nothing much will happen actually.
Let me.
Press Controller five So my console stays awake.
Main thread started the third one function started to function started.
And here it should be called thread to function.
All right, so the we are now it says thread one and thread two is done and afterwards only main thread
is done.
So that's nothing fancy, nothing new.
It just executes dev stuff step by step.
So that's how it looks like here.
But then let's do something interesting.
Let's add a sleep in here.
So actually let's make the thread sleep for 3 seconds.
So thread sleep, 3000 milliseconds.
And what that will do is that this method is just going to say the method, the thread started and then
it's going to wait for 3 seconds.
So now let's run it again and actually let's run it again with control f five.
So here, function, start it.
And then after a while, thread one function.
Done.
Let me actually reproduce that real quick.
So third one and to start it and after 3 seconds, you can see third one function done, third to function
done and main thread ended.
So the main thread is waits until the main or the third one is done.
And that's happening because we join it here.
So we join thread one and we say, okay, please wait until we're done.
Now if we look at the joint method, there are other interesting thing to consider.
There are multiple overloads, so as you can see, there is only one or we have zero parameters here
or arguments that we've given, but there are multiple ones.
So we can add another argument in here, which is the time that we want to wait before we go ahead.
So in here, I'm just going to use if thread dot actually it's third one that join takes up a specific
amount of time and you can see milliseconds timeout.
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
time we say thread one function done, and otherwise we say something like thread one function wasn't
done within 1/2.
All right, so let's get rid of this joint here and let's actually replace all of that code with this.
So now let's run it again.
Control of five.
So front function one third wasn't done within 1/2 thread to function done main thread ended.
So as you can see, it wasn't done within the specified period of time.
But now let's add another code in here because we never saw this thread.
One function done.
So let's add something like thread one function coming back to main.
So that means it's pretty much done.
Or maybe not main.
But to color, let's run it again with controller five.
So threat to functions started let me actually call it once again like then OC main threat ended and
then you see only after that time so only after its 3 seconds it said third one function coming back
to color.
So the main threat was already done and only then it came back to the color, which was the main threat.
Only then it came to well, line 30 or actually afterwards.
So it was done with the main method and only then this code here was executed.
So as you can see, it only blocked the main thread for 1/2.
It didn't block the main threat until it was done.
So it says blocks the calling thread until the thread represented by this instance terminates or the
specified time elapses while continuing to perform static comments and merged pumping.
So what you can see here is you can say, okay, main thread, please wait for 1/2 and if it doesn't
happen or if the third one doesn't finish its stuff, then it's going to go on.
So it's going to join thread two and then it's just going to say, okay, I'm done, thread main threat
is done and well thread one function is still running or thread one is still running and it's still
sleeping and executing the code afterwards.
But it doesn't do that in time as you can see here.
So the main thread didn't wait anymore.
It just said, okay, I'm going to go on, I'm going to move on without you.
So that's the thing about joining threads.
You can see here, that's how it works.
And then now let's have a look at this is a live property.
So if thread one is alive so there is this property and as you can see gets a value indicating the execution
status of the current thread, which means is the thread done or is it not done?
So it says here if the thread is alive still.
So if it's still running at the point where this code is executed, then do something.
So I'm just going to write something like thread one is still doing stuff, so it's still executing
and running code and otherwise I'm just going to say thread one was completed so CW thread one completed.
All right.
And here of course, we need to finish the string now.
Let's check it out again.
And main thread ended and actually my console will close.
So let me start with control f five main thread started, thread one function started and so forth.
So it wasn't done within 1/2.
Then thread function two was done.
So it says, okay, I'm not going to wait anymore.
The main thread.
So it executes thread two then.
Third one is still doing stuff.
So it means that thread one is alive, is executed, and only afterwards main threat is ended.
So this code here is executed, line 38 and then our thread one function coming back to color is called
or is executed.
All right.
So that's what you can check.
You can simply check.
Okay.
Is the threat alive or not?
And based on that, you can execute stuff and that's pretty useful.
So now let's say we want to see that the threat one is still doing stuff on a regular basis.
So let's say we use a for loop here and I is equals zero I below ten I plus plus.
So within that for loop I want to run this code.
And what it's going to do is it's just going to say threat is still doing stuff and then it's going
to sleep for, let's say, 300 milliseconds.
So sleep for 300, which means we will have ten executions of that before our threat is completed.
So let's execute that once again.
Thread one is still doing stuff done, done, done.
And as you can see, it didn't execute the ten times even though threat sleep was there for 300 or 3000
seconds and milliseconds and this one was only for 300 milliseconds.
So what it then did is it executed thread one completed multiple times.
So as you can see there is a overlap where the thread function is coming back.
As you can see, this for loop only executed a little later.
So the order is the following.
We start thread one.
We start right to start thread one.
Waited for 1/2 so you can see we have this 1/2 join so we already lose three executions of our slip
here, which means this thread one still doing stuff is actually only executed six times here or seven
times.
And then the thread one function comes back because it started.
Then it waited for 1/2.
Nothing happened then.
Only then it started to do all of this beautiful stuff.
So because this for loop is executed ten times, it will run this sleep multiple times until it comes
back to color.
And this third one completed is executed super quickly.
So it's not going to wait because there is no sleep in there.
But that's generally what you can do with this alive.
So you can generally check out, is it the life?
Is it still alive?
Is it still alive and do stuff while it's alive or do other stuff while it's alive and then do nothing
or something else when it's done.
So that's a very basic example of using Join and is alive and I hope you could grasp the concept.
So see you in the next video.
We we'll check out tasks.