1 00:00:00,900 --> 00:00:01,770 ‫Welcome back. 2 00:00:01,800 --> 00:00:04,680 ‫In this video, we'll look into threat pools. 3 00:00:04,680 --> 00:00:09,800 ‫So therefore, we are going to create multiple threads and see what's going to happen. 4 00:00:09,810 --> 00:00:15,870 ‫Therefore, let's go ahead first to simply create a very basic thread and I'm going to get rid of this 5 00:00:15,870 --> 00:00:18,660 ‫completion source stuff. 6 00:00:18,660 --> 00:00:24,030 ‫And so we are at a very generic thread here. 7 00:00:24,030 --> 00:00:26,580 ‫So that's a generic thread that we have used so far. 8 00:00:26,580 --> 00:00:33,660 ‫And here we wait for 1/2, for example, and it has a thread number and in order to start it of course 9 00:00:33,660 --> 00:00:37,890 ‫we need to call the start method here towards the end. 10 00:00:37,890 --> 00:00:41,310 ‫So now this is just going to be a thread and it's going to start. 11 00:00:41,310 --> 00:00:42,360 ‫So nothing new here. 12 00:00:42,360 --> 00:00:44,640 ‫We have done that multiple times there. 13 00:00:44,940 --> 00:00:53,040 ‫So the only difference is that now I would like to use something called a thread pool, but before I 14 00:00:53,040 --> 00:01:03,090 ‫do that, I'm just going to use a enumerable a numeral both like that, and I'm going to run a range 15 00:01:03,090 --> 00:01:16,050 ‫from zero to let's say 100 for now to list and for each entry, I would like to do something. 16 00:01:16,050 --> 00:01:19,950 ‫So I'm just going to say just run specific code. 17 00:01:20,760 --> 00:01:23,700 ‫So this is just one line of code pretty much. 18 00:01:23,970 --> 00:01:27,720 ‫And in there I want to create threads. 19 00:01:28,080 --> 00:01:35,670 ‫So what it's going to do is this I enumerable or this enumerable, actually, it's going to create multiple 20 00:01:35,670 --> 00:01:38,970 ‫threads and it's going to create 100 of them. 21 00:01:38,970 --> 00:01:41,130 ‫So I'm going to use a link here. 22 00:01:41,130 --> 00:01:47,760 ‫So using system link in order to use the enumerable and it should be called range with the E at the 23 00:01:47,760 --> 00:01:48,150 ‫end. 24 00:01:48,150 --> 00:01:49,470 ‫So there we are. 25 00:01:49,500 --> 00:01:56,190 ‫So that will do is it will simply create 100 threads and we will see all of those threads on the console. 26 00:01:56,190 --> 00:01:57,510 ‫So let's get started with that. 27 00:01:59,600 --> 00:02:01,250 ‫And there we are. 28 00:02:01,250 --> 00:02:06,560 ‫And as you can see, it's starting the threads and it's ending the threads. 29 00:02:06,560 --> 00:02:09,320 ‫And 102 is the last thread. 30 00:02:09,320 --> 00:02:11,900 ‫And as you can see, it started with three. 31 00:02:11,900 --> 00:02:14,630 ‫So it starts of three, six, five and so forth. 32 00:02:14,630 --> 00:02:19,040 ‫So the order, as you can see, is very different to what we would expect. 33 00:02:19,040 --> 00:02:26,120 ‫So the threads, they have a different ID then the order in which we would expect them to be, but then 34 00:02:26,120 --> 00:02:29,360 ‫they are started and ended and maybe that was a little too fast. 35 00:02:29,360 --> 00:02:31,130 ‫So let's create a little more thread. 36 00:02:31,140 --> 00:02:40,730 ‫So let's say 1000 threads and as you can see, it creates tons of threads, but threads are ended before 37 00:02:40,730 --> 00:02:41,990 ‫others are started. 38 00:02:41,990 --> 00:02:49,520 ‫So this enumerable it starts them or it creates them as quick as it can, as quickly as it can. 39 00:02:49,520 --> 00:02:51,410 ‫But as you can see, it takes a little while. 40 00:02:51,410 --> 00:03:01,130 ‫So let's say roughly 100 milliseconds or so to create a thread that we are, it creates them and then 41 00:03:01,220 --> 00:03:02,540 ‫it starts to finish them. 42 00:03:02,540 --> 00:03:08,750 ‫So after 1/2 it created six the threads only then thread five. 43 00:03:08,750 --> 00:03:11,240 ‫So one of the first ones was ended. 44 00:03:11,540 --> 00:03:18,380 ‫So as you can see, it creates those threads and that's not very efficient because well, we can't control 45 00:03:18,380 --> 00:03:21,020 ‫those threads, we can't control this amount of threads. 46 00:03:21,150 --> 00:03:27,320 ‫And that's a very important aspect of threads that you have to be very, very careful and very precise 47 00:03:27,320 --> 00:03:28,340 ‫when working with threads. 48 00:03:28,340 --> 00:03:34,100 ‫You need to know what you want to do with that thread and why it's there and how to generally work with 49 00:03:34,100 --> 00:03:35,600 ‫everything that is within the thread. 50 00:03:35,600 --> 00:03:43,220 ‫So that's just an example of threads, how you can create multiple of them, and it just created 1000 51 00:03:43,220 --> 00:03:44,150 ‫threads like that. 52 00:03:44,150 --> 00:03:49,170 ‫And now let's have a look at thread pools, because thread pools are a slightly different approach at 53 00:03:49,190 --> 00:03:55,880 ‫using threads and they are a little more secure, let's say, because they don't create a 1000 threads 54 00:03:55,880 --> 00:03:57,050 ‫that are not required. 55 00:03:57,050 --> 00:03:59,780 ‫It only has the threads that are currently required. 56 00:03:59,780 --> 00:04:08,360 ‫So let's create a thread pool here and queue user work item Q A method for execution. 57 00:04:08,360 --> 00:04:14,270 ‫The method executes when a thread pool thread becomes available, so only if it's available, then this 58 00:04:14,270 --> 00:04:18,560 ‫will be executed and it needs a object here. 59 00:04:18,560 --> 00:04:21,110 ‫So there are two different ones here. 60 00:04:21,110 --> 00:04:26,220 ‫You see callback and object and I'm just going to give it an oh like an object. 61 00:04:26,570 --> 00:04:33,560 ‫Just going to call it oh nothing special here and what I'm going to execute in the thread pool. 62 00:04:33,560 --> 00:04:43,490 ‫So I'm just going to say give me the ID of the thread or the thread number and well, let's say when 63 00:04:43,490 --> 00:04:45,200 ‫it started and when it ended. 64 00:04:45,200 --> 00:04:48,620 ‫So that's pretty much everything that I want to do with this thread pool. 65 00:04:48,800 --> 00:04:54,470 ‫And as you can see in here, we don't create specifically, we don't create threads in there, but the 66 00:04:54,500 --> 00:04:56,420 ‫thread pool will do so for us. 67 00:04:56,420 --> 00:05:00,110 ‫So there is plenty of work to do, like 1000 threads to handle. 68 00:05:00,110 --> 00:05:05,450 ‫And as you can see, it only creates a specific amount of threads. 69 00:05:05,450 --> 00:05:10,340 ‫And as you can see, it starts and ends the threads more or less at the same time. 70 00:05:10,340 --> 00:05:17,510 ‫And you can see also that I don't have many cores on my CPU here because it only can work with a specific 71 00:05:17,510 --> 00:05:19,280 ‫amount of threads at the same time. 72 00:05:19,280 --> 00:05:23,630 ‫So thread pool will handle things a lot smoother as you can see. 73 00:05:24,350 --> 00:05:30,770 ‫But of course it takes a lot of time to get through all of those 1000 threads. 74 00:05:32,150 --> 00:05:35,210 ‫But as I said, if you use threats, you have to control them. 75 00:05:35,210 --> 00:05:37,460 ‫So don't randomly use threats. 76 00:05:37,580 --> 00:05:42,740 ‫Pretty much the main thing where you would use threats is when you do something in the background and 77 00:05:42,740 --> 00:05:44,260 ‫you do something in the foreground. 78 00:05:44,270 --> 00:05:47,260 ‫So in the foreground is usually something like the UI. 79 00:05:47,270 --> 00:05:55,520 ‫So when you use WPF or if you use apps with Xamarin or create apps with Xamarin, then everything that's 80 00:05:55,520 --> 00:06:03,530 ‫happening in the front end, so in the UI is done by the main thread and then or by the UI thread and 81 00:06:03,530 --> 00:06:10,940 ‫then something like downloading videos or downloading data in general from the internet or accessing 82 00:06:10,940 --> 00:06:16,640 ‫data from the harddrive or from the SD card or whatever, everything. 83 00:06:16,640 --> 00:06:18,360 ‫That could take a little while. 84 00:06:18,380 --> 00:06:24,980 ‫Even if it's just a couple of milliseconds or not a couple, but several milliseconds, then that's 85 00:06:24,980 --> 00:06:32,570 ‫already something that should be done in the background, because otherwise your UI will be frozen because 86 00:06:32,570 --> 00:06:37,430 ‫well, it's not ready to do something because the threads are all busy. 87 00:06:39,050 --> 00:06:39,410 ‫All right. 88 00:06:39,410 --> 00:06:42,740 ‫So that's about creating thread pools. 89 00:06:42,740 --> 00:06:47,030 ‫And if you have a lot of stuff to do, you can use thread pools. 90 00:06:48,390 --> 00:06:52,070 ‫So there are several things to consider when using threat pools. 91 00:06:52,080 --> 00:07:00,630 ‫So generally threat pools are there to run specific work or do specific works or work items, and they 92 00:07:00,630 --> 00:07:07,020 ‫will pretty much wait until the next thread is ready or one thread is done before it starts the next 93 00:07:07,020 --> 00:07:09,390 ‫threat or before it starts with the next work item. 94 00:07:09,390 --> 00:07:15,360 ‫So that's the advantage of threat pools that they don't have idle time or sleeping time where nothing 95 00:07:15,360 --> 00:07:15,750 ‫happens. 96 00:07:15,750 --> 00:07:21,600 ‫Pretty much so because there can be situations where simply nothing has to be done with your or by your 97 00:07:21,630 --> 00:07:22,410 ‫CPU. 98 00:07:22,410 --> 00:07:27,840 ‫And that's totally fine because otherwise it would be pretty heavy on the CPU if it has to run all the 99 00:07:27,840 --> 00:07:30,630 ‫time and do stuff all the time or heavy weight work. 100 00:07:30,630 --> 00:07:36,180 ‫But generally this threat pool, it simply says, okay, if there is nothing or if there's enough space 101 00:07:36,180 --> 00:07:41,070 ‫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 102 00:07:41,070 --> 00:07:41,940 ‫that has to be done. 103 00:07:42,840 --> 00:07:48,750 ‫The threads in the managed thread pool, by the way, are background threads that is there. 104 00:07:48,750 --> 00:07:51,540 ‫Thread is background, property is true. 105 00:07:51,540 --> 00:07:54,570 ‫So we have not seen that before. 106 00:07:54,570 --> 00:08:01,290 ‫But if I create a thread like so, so here let me get this new thread from here. 107 00:08:04,440 --> 00:08:08,220 ‫And if I want to set that into the background, I can do so. 108 00:08:09,150 --> 00:08:14,100 ‫So is background is equal true 109 00:08:16,680 --> 00:08:17,580 ‫for example. 110 00:08:19,380 --> 00:08:22,260 ‫So that's how I can set this thread to your background. 111 00:08:22,260 --> 00:08:27,780 ‫And in the queue in this thread pool queue, there are only background threads. 112 00:08:30,030 --> 00:08:34,980 ‫Another point to consider is when the threat pool reuses a threat, what we have seen here. 113 00:08:34,980 --> 00:08:37,440 ‫So it reuses the threat ID that we have seen. 114 00:08:37,440 --> 00:08:37,590 ‫Right. 115 00:08:37,650 --> 00:08:41,140 ‫This threat managed ID, it was reused a lot. 116 00:08:41,160 --> 00:08:47,220 ‫Then the threat pool does not clear the data in the threat local storage. 117 00:08:47,520 --> 00:08:54,630 ‫Therefore, when a method examines the threat local storage, the values it finds might be left over 118 00:08:54,630 --> 00:08:57,260 ‫from an earlier use of the threat pool threat. 119 00:08:57,270 --> 00:08:59,730 ‫So that's something that you have to consider. 120 00:09:00,660 --> 00:09:04,410 ‫I'll share a link with you can read more about threat pools in general.