Welcome back.
In this video, we are going to look into threads and therefore we are just going to do the very basic
concept of threads where you will see how to create a thread and how to create multiple threads.
So let's get started by using console right line in a specific manner.
So I'm going to create multiple worlds, for example, four of them, and I'm going to say Hello World
one, hello world two, three and four.
So those worlds, if we run them, we of course, need, first of all, console red line so the console
stays open and stays awake.
If we run them, then it's very basic what's going to happen.
So they're just going to print Hello World four times on the screen.
And then we are Hello World.
One, two, three and four.
All right, so that's nothing new.
We have seen that a lot before, but there is something called thread, a class called thread.
And if we want to use it, we need to use the threading namespace.
So using system dot thread and that's it.
So now we can use threads.
So I can go ahead and use the thread class and as you can see, creates and controls a thread, sets
its priority and gets its status.
So what we can do now is for example, use thread dot sleep for a limited amount of time, so let's
say for 1000 milliseconds.
So that will do is it will pause our whole program or our thread in this case the main thread or the
thread in which it is for 1/2 after helloworld one was printed on the screen and we can actually do
that after every single statement.
So I'm going to say just sleep for a specific amount of time and then we are.
Hello world one, two, three and four.
So it stops if I click in it.
But generally you can see it waited for 1/2 and after that second it printed out the next thing.
So what it simply does that really stops the whole thread.
So suspends the current thread for a specific or specified number of milliseconds.
So that's something that you can do if you want to do something after a specific amount of time.
But be aware that this is really stopping the main thread.
So nothing that will have to do have to be done, let's say in the UI thread can be done.
We're not using UI here, we're using a console application in this case.
But if you use WPF, for example, to have a UI and you pause the main thread for 1/2, that means that
the UI will not respond for a whole second, and that's something that the user doesn't want, and that
will be very bad behavior that you should not do.
But that's generally just for you to understand what you can do with threads here, right?
So you can pose a threat for a specific amount of time.
But we are using one thread only.
It's the main thread and if you want to find out more about it, then you have to debug.
And in order to do so, we add a point here.
So just a debug point where you simply press on that bar here on the left, let's say in line nine,
where the main method starts.
And then you start the program and as you can see, nothing will be displayed because it pauses right
here.
So that's where we currently are, that's what the cursor is.
And in order to now see the tasks, we need to go to debug Windows and then select threads here.
Or you could press control alt h.
So I'm going to open up threads and as you can see, they're floating for me now and I will position
them just here at the left hand side.
And now you can see the threads that are running and in our case we only see the main thread.
So there is only one thread and it has a managed ID of zero.
So that's our main thread and it's generally doing everything that is within our main method.
So if I go to the next step.
As you can see, we are still in the main thread and we wait for 1/2.
We see how the world actually we open it up.
You can see it.
So now it's a tale of world three.
We sleep for 1/2 and then it waits for us to go for the next step.
So you can press F ten to go to the next step and then it displays hello world for just a second later.
Yeah that we are.
Hello World four.
And then this red line comes up.
So now I'm just going to press something like enter or whatever and here and go to the next step.
And as you can see, we're still in the main thread.
And our program closes, so no threads anymore.
So that's good and all, but that's just our main thread and that's a little boring.
We want to see how to create threads and that's what we're here for.
So let's go ahead and create multiple threads on our own, because what happens here is that it's done
sequentially.
So step by step by step, one row after the other.
But if we want to do things in a parallel, we can create our own thread because we don't want it to
run in the main thread, but we wanted to run Parallelly.
So what I'm going to do is I'm going to create a new threat here.
I'm just going to say new and then I'm going to call it thread.
So I'm just using the thread class here and you need brackets and brackets again.
And after those brackets, the second one, and here you use equal and bigger sine and curly brackets.
And then after the the bracket here closes, you need to start.
So that's how you start a thread.
So I've created a new thread and it starts straight away because I'm calling Start Here.
Just the Start method causes the operating system to change the state of the current instance to thread
state running.
So this thread will have this thread state running, which means it will simply execute whatever is
within its curly brackets.
So that's where we can add our code that we want to run whenever this thread is called.
So let's say this thread here is our thread one.
So I'm just going to write something like thread one, CW Thread one, and on top of that I'm going
to sleep for a second.
So sleep for 1000 milliseconds, which is 1/2.
So that's what this thread is going to do.
And what I'm going to do is I'm going to position multiple threads below that.
So thread one, two, three and four.
So I'm going to rename those and I'm going to comment out.
The ones that we have before.
So the console right lines and now let's run it.
And there we are.
As you can see, it came up all at the same time.
So it waited for 1/2 and then they all came up at the same time.
So what was happening there?
Let me actually go into that point here and maybe the point.
Yeah, right there.
So let's run it again.
Now we see our program stops right here.
So that's where it is.
But at the same time, here in the thread window here at the bottom, you can see that there are two
threads opened up already and a worker thread with no name and another worker thread with no name as
well.
So we have this new thread which is running already, and then one of the other threads is opened as
well.
And as you can see the main thread as well.
So there are three threads at the same time.
Now let's go to the next step.
As you can see at that point, suddenly there are four threads and thus that's the four ones that we
have here.
So as the first one started, only maybe the second one was ready already, but the other ones weren't
ready yet.
And now at that point where thread slips for 1000 milliseconds after one millisecond elapsed or even
less, you can see that the other threads have started already.
So we have four threads running at the same time.
So we have a multi threading solution here.
And if I do the next step, well, as you can see, thread two, three and four are already displayed,
but console right line thread one as you can see, is not displayed as yet.
So they do run in parallel, but they are not perfectly accurate as you can see.
So there can be slight differences in the time that they need to be executed.
So I'm going to go to the next step this row here.
And as you can see now, thread one is done as well.
So even though the threads.
Or in a specific order in here, in my code, they are started more or less at the same time.
And as you can see, it doesn't need to be in the exact order, as we thought.
So it won't be finished at the exact order that we have positioned them here.
But based on how fast they could finish or execute whatever the code was.
So it can happen that this code needs a little longer than that one or a lot longer than the other ones.
So that's what you have to take out of this so you can work with threads, but you have to be very,
very careful because there are potentially multiple things that could go wrong.
And I'm going to cover some of them and I'm going to show you or give you, and I'm going to cover some
of them.
And I'm going to share links with you as well, where you can read a lot more into that.
So that's about the basic idea of how to create threads.
As you can see, that's how you can set things up.
So threads are generally a little piece of work or a piece of work that is executed, and you can execute
multiple things at the same time if you have multiple cores in your processor.
So let's say you have a four core processor, then four threads or four processors at the same time
can be executed.
If you have, let's say, only one processor, then only one thread or one task at the time can be executed,
will never happen to do it.
They will be executed in parallel because this one processor only does one specific thing at a time.
That means that if you know that you're working with a multicore processor or your application will
run on a multicore processor, you can you work with multiple threads, otherwise you run into issues.
But generally how it works, if you only have one core, then the main thread will run and only if the
main thread is idle, then it will go ahead and do other things.
So it will run other threats.
Let's say you have a UI and on that UI you just display things.
You have a button.
Then if you hover over the button, it will change its appearance, stuff like that.
That's something that is done by the main thread.
But if the main thread is not busy, then the other threads will start.
So what if you have something that should be run in the background, then that will be done whenever
thread one the main thread is not busy or thread zero.
Actually it is here.
So that's about threads.
We will look into threads a little more of course, but generally that's what I wanted to show you to
start off.