Welcome back.
In this video, we'll look into threat pools.
So therefore, we are going to create multiple threads and see what's going to happen.
Therefore, let's go ahead first to simply create a very basic thread and I'm going to get rid of this
completion source stuff.
And so we are at a very generic thread here.
So that's a generic thread that we have used so far.
And here we wait for 1/2, for example, and it has a thread number and in order to start it of course
we need to call the start method here towards the end.
So now this is just going to be a thread and it's going to start.
So nothing new here.
We have done that multiple times there.
So the only difference is that now I would like to use something called a thread pool, but before I
do that, I'm just going to use a enumerable a numeral both like that, and I'm going to run a range
from zero to let's say 100 for now to list and for each entry, I would like to do something.
So I'm just going to say just run specific code.
So this is just one line of code pretty much.
And in there I want to create threads.
So what it's going to do is this I enumerable or this enumerable, actually, it's going to create multiple
threads and it's going to create 100 of them.
So I'm going to use a link here.
So using system link in order to use the enumerable and it should be called range with the E at the
end.
So there we are.
So that will do is it will simply create 100 threads and we will see all of those threads on the console.
So let's get started with that.
And there we are.
And as you can see, it's starting the threads and it's ending the threads.
And 102 is the last thread.
And as you can see, it started with three.
So it starts of three, six, five and so forth.
So the order, as you can see, is very different to what we would expect.
So the threads, they have a different ID then the order in which we would expect them to be, but then
they are started and ended and maybe that was a little too fast.
So let's create a little more thread.
So let's say 1000 threads and as you can see, it creates tons of threads, but threads are ended before
others are started.
So this enumerable it starts them or it creates them as quick as it can, as quickly as it can.
But as you can see, it takes a little while.
So let's say roughly 100 milliseconds or so to create a thread that we are, it creates them and then
it starts to finish them.
So after 1/2 it created six the threads only then thread five.
So one of the first ones was ended.
So as you can see, it creates those threads and that's not very efficient because well, we can't control
those threads, we can't control this amount of threads.
And that's a very important aspect of threads that you have to be very, very careful and very precise
when working with threads.
You need to know what you want to do with that thread and why it's there and how to generally work with
everything that is within the thread.
So that's just an example of threads, how you can create multiple of them, and it just created 1000
threads like that.
And now let's have a look at thread pools, because thread pools are a slightly different approach at
using threads and they are a little more secure, let's say, because they don't create a 1000 threads
that are not required.
It only has the threads that are currently required.
So let's create a thread pool here and queue user work item Q A method for execution.
The method executes when a thread pool thread becomes available, so only if it's available, then this
will be executed and it needs a object here.
So there are two different ones here.
You see callback and object and I'm just going to give it an oh like an object.
Just going to call it oh nothing special here and what I'm going to execute in the thread pool.
So I'm just going to say give me the ID of the thread or the thread number and well, let's say when
it started and when it ended.
So that's pretty much everything that I want to do with this thread pool.
And as you can see in here, we don't create specifically, we don't create threads in there, but the
thread pool will do so for us.
So there is plenty of work to do, like 1000 threads to handle.
And as you can see, it only creates a specific amount of threads.
And as you can see, it starts and ends the threads more or less at the same time.
And you can see also that I don't have many cores on my CPU here because it only can work with a specific
amount of threads at the same time.
So thread pool will handle things a lot smoother as you can see.
But of course it takes a lot of time to get through all of those 1000 threads.
But as I said, if you use threats, you have to control them.
So don't randomly use threats.
Pretty much the main thing where you would use threats is when you do something in the background and
you do something in the foreground.
So in the foreground is usually something like the UI.
So when you use WPF or if you use apps with Xamarin or create apps with Xamarin, then everything that's
happening in the front end, so in the UI is done by the main thread and then or by the UI thread and
then something like downloading videos or downloading data in general from the internet or accessing
data from the harddrive or from the SD card or whatever, everything.
That could take a little while.
Even if it's just a couple of milliseconds or not a couple, but several milliseconds, then that's
already something that should be done in the background, because otherwise your UI will be frozen because
well, it's not ready to do something because the threads are all busy.
All right.
So that's about creating thread pools.
And if you have a lot of stuff to do, you can use thread pools.
So there are several things to consider when using threat pools.
So generally threat pools are there to run specific work or do specific works or work items, and they
will pretty much wait until the next thread is ready or one thread is done before it starts the next
threat or before it starts with the next work item.
So that's the advantage of threat pools that they don't have idle time or sleeping time where nothing
happens.
Pretty much so because there can be situations where simply nothing has to be done with your or by your
CPU.
And that's totally fine because otherwise it would be pretty heavy on the CPU if it has to run all the
time and do stuff all the time or heavy weight work.
But generally this threat pool, it simply says, okay, if there is nothing or if there's enough space
for me to run a thread or create a new thread, I'm going to do so and I'm going to execute the stuff
that has to be done.
The threads in the managed thread pool, by the way, are background threads that is there.
Thread is background, property is true.
So we have not seen that before.
But if I create a thread like so, so here let me get this new thread from here.
And if I want to set that into the background, I can do so.
So is background is equal true
for example.
So that's how I can set this thread to your background.
And in the queue in this thread pool queue, there are only background threads.
Another point to consider is when the threat pool reuses a threat, what we have seen here.
So it reuses the threat ID that we have seen.
Right.
This threat managed ID, it was reused a lot.
Then the threat pool does not clear the data in the threat local storage.
Therefore, when a method examines the threat local storage, the values it finds might be left over
from an earlier use of the threat pool threat.
So that's something that you have to consider.
I'll share a link with you can read more about threat pools in general.