1 00:00:00,480 --> 00:00:01,470 ‫Welcome back. 2 00:00:01,500 --> 00:00:08,130 ‫In this video, we are going to look into threads and therefore we are just going to do the very basic 3 00:00:08,130 --> 00:00:13,350 ‫concept of threads where you will see how to create a thread and how to create multiple threads. 4 00:00:13,350 --> 00:00:17,880 ‫So let's get started by using console right line in a specific manner. 5 00:00:17,880 --> 00:00:23,970 ‫So I'm going to create multiple worlds, for example, four of them, and I'm going to say Hello World 6 00:00:23,970 --> 00:00:27,750 ‫one, hello world two, three and four. 7 00:00:29,310 --> 00:00:36,480 ‫So those worlds, if we run them, we of course, need, first of all, console red line so the console 8 00:00:36,480 --> 00:00:38,220 ‫stays open and stays awake. 9 00:00:38,220 --> 00:00:41,010 ‫If we run them, then it's very basic what's going to happen. 10 00:00:41,010 --> 00:00:45,420 ‫So they're just going to print Hello World four times on the screen. 11 00:00:45,420 --> 00:00:46,770 ‫And then we are Hello World. 12 00:00:46,770 --> 00:00:48,300 ‫One, two, three and four. 13 00:00:48,750 --> 00:00:51,450 ‫All right, so that's nothing new. 14 00:00:51,480 --> 00:00:56,640 ‫We have seen that a lot before, but there is something called thread, a class called thread. 15 00:00:56,640 --> 00:01:00,660 ‫And if we want to use it, we need to use the threading namespace. 16 00:01:00,660 --> 00:01:07,230 ‫So using system dot thread and that's it. 17 00:01:07,560 --> 00:01:09,420 ‫So now we can use threads. 18 00:01:09,420 --> 00:01:15,960 ‫So I can go ahead and use the thread class and as you can see, creates and controls a thread, sets 19 00:01:15,960 --> 00:01:18,120 ‫its priority and gets its status. 20 00:01:18,120 --> 00:01:25,140 ‫So what we can do now is for example, use thread dot sleep for a limited amount of time, so let's 21 00:01:25,140 --> 00:01:27,210 ‫say for 1000 milliseconds. 22 00:01:27,330 --> 00:01:33,780 ‫So that will do is it will pause our whole program or our thread in this case the main thread or the 23 00:01:33,780 --> 00:01:41,940 ‫thread in which it is for 1/2 after helloworld one was printed on the screen and we can actually do 24 00:01:41,940 --> 00:01:44,610 ‫that after every single statement. 25 00:01:44,910 --> 00:01:51,960 ‫So I'm going to say just sleep for a specific amount of time and then we are. 26 00:01:51,960 --> 00:01:56,130 ‫Hello world one, two, three and four. 27 00:01:56,130 --> 00:01:58,440 ‫So it stops if I click in it. 28 00:01:58,440 --> 00:02:04,950 ‫But generally you can see it waited for 1/2 and after that second it printed out the next thing. 29 00:02:04,950 --> 00:02:09,510 ‫So what it simply does that really stops the whole thread. 30 00:02:09,510 --> 00:02:14,250 ‫So suspends the current thread for a specific or specified number of milliseconds. 31 00:02:14,250 --> 00:02:19,830 ‫So that's something that you can do if you want to do something after a specific amount of time. 32 00:02:19,830 --> 00:02:25,710 ‫But be aware that this is really stopping the main thread. 33 00:02:25,710 --> 00:02:32,220 ‫So nothing that will have to do have to be done, let's say in the UI thread can be done. 34 00:02:32,220 --> 00:02:36,000 ‫We're not using UI here, we're using a console application in this case. 35 00:02:36,000 --> 00:02:42,570 ‫But if you use WPF, for example, to have a UI and you pause the main thread for 1/2, that means that 36 00:02:42,570 --> 00:02:48,090 ‫the UI will not respond for a whole second, and that's something that the user doesn't want, and that 37 00:02:48,090 --> 00:02:50,460 ‫will be very bad behavior that you should not do. 38 00:02:50,460 --> 00:02:54,330 ‫But that's generally just for you to understand what you can do with threads here, right? 39 00:02:54,330 --> 00:02:57,390 ‫So you can pose a threat for a specific amount of time. 40 00:02:57,390 --> 00:02:59,610 ‫But we are using one thread only. 41 00:02:59,610 --> 00:03:04,020 ‫It's the main thread and if you want to find out more about it, then you have to debug. 42 00:03:05,070 --> 00:03:07,920 ‫And in order to do so, we add a point here. 43 00:03:07,920 --> 00:03:15,210 ‫So just a debug point where you simply press on that bar here on the left, let's say in line nine, 44 00:03:15,210 --> 00:03:16,950 ‫where the main method starts. 45 00:03:16,950 --> 00:03:24,750 ‫And then you start the program and as you can see, nothing will be displayed because it pauses right 46 00:03:24,750 --> 00:03:25,080 ‫here. 47 00:03:25,080 --> 00:03:28,410 ‫So that's where we currently are, that's what the cursor is. 48 00:03:28,410 --> 00:03:35,700 ‫And in order to now see the tasks, we need to go to debug Windows and then select threads here. 49 00:03:35,700 --> 00:03:38,520 ‫Or you could press control alt h. 50 00:03:38,670 --> 00:03:43,500 ‫So I'm going to open up threads and as you can see, they're floating for me now and I will position 51 00:03:43,500 --> 00:03:46,320 ‫them just here at the left hand side. 52 00:03:46,320 --> 00:03:52,050 ‫And now you can see the threads that are running and in our case we only see the main thread. 53 00:03:52,050 --> 00:03:55,590 ‫So there is only one thread and it has a managed ID of zero. 54 00:03:55,590 --> 00:04:02,370 ‫So that's our main thread and it's generally doing everything that is within our main method. 55 00:04:02,370 --> 00:04:04,380 ‫So if I go to the next step. 56 00:04:05,780 --> 00:04:11,410 ‫As you can see, we are still in the main thread and we wait for 1/2. 57 00:04:11,420 --> 00:04:14,060 ‫We see how the world actually we open it up. 58 00:04:14,180 --> 00:04:15,020 ‫You can see it. 59 00:04:15,470 --> 00:04:17,210 ‫So now it's a tale of world three. 60 00:04:17,210 --> 00:04:21,500 ‫We sleep for 1/2 and then it waits for us to go for the next step. 61 00:04:21,500 --> 00:04:28,190 ‫So you can press F ten to go to the next step and then it displays hello world for just a second later. 62 00:04:28,190 --> 00:04:29,000 ‫Yeah that we are. 63 00:04:29,000 --> 00:04:29,960 ‫Hello World four. 64 00:04:29,960 --> 00:04:32,510 ‫And then this red line comes up. 65 00:04:32,510 --> 00:04:40,100 ‫So now I'm just going to press something like enter or whatever and here and go to the next step. 66 00:04:40,100 --> 00:04:42,470 ‫And as you can see, we're still in the main thread. 67 00:04:43,390 --> 00:04:46,420 ‫And our program closes, so no threads anymore. 68 00:04:46,540 --> 00:04:51,290 ‫So that's good and all, but that's just our main thread and that's a little boring. 69 00:04:51,310 --> 00:04:55,140 ‫We want to see how to create threads and that's what we're here for. 70 00:04:55,150 --> 00:05:01,330 ‫So let's go ahead and create multiple threads on our own, because what happens here is that it's done 71 00:05:01,330 --> 00:05:02,320 ‫sequentially. 72 00:05:02,320 --> 00:05:05,620 ‫So step by step by step, one row after the other. 73 00:05:05,620 --> 00:05:11,440 ‫But if we want to do things in a parallel, we can create our own thread because we don't want it to 74 00:05:11,440 --> 00:05:14,490 ‫run in the main thread, but we wanted to run Parallelly. 75 00:05:14,500 --> 00:05:17,750 ‫So what I'm going to do is I'm going to create a new threat here. 76 00:05:17,770 --> 00:05:22,060 ‫I'm just going to say new and then I'm going to call it thread. 77 00:05:22,060 --> 00:05:28,750 ‫So I'm just using the thread class here and you need brackets and brackets again. 78 00:05:28,750 --> 00:05:37,960 ‫And after those brackets, the second one, and here you use equal and bigger sine and curly brackets. 79 00:05:38,770 --> 00:05:45,250 ‫And then after the the bracket here closes, you need to start. 80 00:05:45,250 --> 00:05:47,980 ‫So that's how you start a thread. 81 00:05:47,980 --> 00:05:53,170 ‫So I've created a new thread and it starts straight away because I'm calling Start Here. 82 00:05:53,350 --> 00:05:59,260 ‫Just the Start method causes the operating system to change the state of the current instance to thread 83 00:05:59,260 --> 00:06:00,040 ‫state running. 84 00:06:00,040 --> 00:06:05,860 ‫So this thread will have this thread state running, which means it will simply execute whatever is 85 00:06:05,860 --> 00:06:07,720 ‫within its curly brackets. 86 00:06:07,720 --> 00:06:14,110 ‫So that's where we can add our code that we want to run whenever this thread is called. 87 00:06:14,110 --> 00:06:17,530 ‫So let's say this thread here is our thread one. 88 00:06:17,800 --> 00:06:28,390 ‫So I'm just going to write something like thread one, CW Thread one, and on top of that I'm going 89 00:06:28,390 --> 00:06:29,860 ‫to sleep for a second. 90 00:06:29,860 --> 00:06:35,410 ‫So sleep for 1000 milliseconds, which is 1/2. 91 00:06:35,500 --> 00:06:38,020 ‫So that's what this thread is going to do. 92 00:06:38,020 --> 00:06:42,400 ‫And what I'm going to do is I'm going to position multiple threads below that. 93 00:06:42,400 --> 00:06:46,030 ‫So thread one, two, three and four. 94 00:06:46,030 --> 00:06:51,940 ‫So I'm going to rename those and I'm going to comment out. 95 00:06:54,140 --> 00:06:55,790 ‫The ones that we have before. 96 00:06:55,790 --> 00:07:00,380 ‫So the console right lines and now let's run it. 97 00:07:03,420 --> 00:07:04,590 ‫And there we are. 98 00:07:04,620 --> 00:07:07,280 ‫As you can see, it came up all at the same time. 99 00:07:07,290 --> 00:07:11,520 ‫So it waited for 1/2 and then they all came up at the same time. 100 00:07:11,520 --> 00:07:13,260 ‫So what was happening there? 101 00:07:13,560 --> 00:07:21,120 ‫Let me actually go into that point here and maybe the point. 102 00:07:22,430 --> 00:07:23,400 ‫Yeah, right there. 103 00:07:23,420 --> 00:07:24,980 ‫So let's run it again. 104 00:07:27,080 --> 00:07:30,080 ‫Now we see our program stops right here. 105 00:07:30,080 --> 00:07:31,280 ‫So that's where it is. 106 00:07:31,280 --> 00:07:37,760 ‫But at the same time, here in the thread window here at the bottom, you can see that there are two 107 00:07:37,760 --> 00:07:45,890 ‫threads opened up already and a worker thread with no name and another worker thread with no name as 108 00:07:45,890 --> 00:07:46,400 ‫well. 109 00:07:46,700 --> 00:07:52,910 ‫So we have this new thread which is running already, and then one of the other threads is opened as 110 00:07:52,910 --> 00:07:53,360 ‫well. 111 00:07:53,360 --> 00:07:55,370 ‫And as you can see the main thread as well. 112 00:07:55,370 --> 00:07:57,740 ‫So there are three threads at the same time. 113 00:07:57,740 --> 00:07:59,930 ‫Now let's go to the next step. 114 00:08:00,560 --> 00:08:06,230 ‫As you can see at that point, suddenly there are four threads and thus that's the four ones that we 115 00:08:06,230 --> 00:08:06,860 ‫have here. 116 00:08:06,860 --> 00:08:13,370 ‫So as the first one started, only maybe the second one was ready already, but the other ones weren't 117 00:08:13,370 --> 00:08:14,210 ‫ready yet. 118 00:08:14,210 --> 00:08:21,500 ‫And now at that point where thread slips for 1000 milliseconds after one millisecond elapsed or even 119 00:08:21,500 --> 00:08:25,010 ‫less, you can see that the other threads have started already. 120 00:08:25,010 --> 00:08:28,040 ‫So we have four threads running at the same time. 121 00:08:28,040 --> 00:08:31,700 ‫So we have a multi threading solution here. 122 00:08:31,700 --> 00:08:40,340 ‫And if I do the next step, well, as you can see, thread two, three and four are already displayed, 123 00:08:40,340 --> 00:08:46,730 ‫but console right line thread one as you can see, is not displayed as yet. 124 00:08:46,730 --> 00:08:53,720 ‫So they do run in parallel, but they are not perfectly accurate as you can see. 125 00:08:53,720 --> 00:08:59,690 ‫So there can be slight differences in the time that they need to be executed. 126 00:08:59,690 --> 00:09:03,800 ‫So I'm going to go to the next step this row here. 127 00:09:04,160 --> 00:09:07,460 ‫And as you can see now, thread one is done as well. 128 00:09:07,490 --> 00:09:10,220 ‫So even though the threads. 129 00:09:11,200 --> 00:09:17,770 ‫Or in a specific order in here, in my code, they are started more or less at the same time. 130 00:09:17,950 --> 00:09:23,510 ‫And as you can see, it doesn't need to be in the exact order, as we thought. 131 00:09:23,530 --> 00:09:28,030 ‫So it won't be finished at the exact order that we have positioned them here. 132 00:09:28,600 --> 00:09:33,900 ‫But based on how fast they could finish or execute whatever the code was. 133 00:09:33,910 --> 00:09:40,090 ‫So it can happen that this code needs a little longer than that one or a lot longer than the other ones. 134 00:09:40,090 --> 00:09:45,610 ‫So that's what you have to take out of this so you can work with threads, but you have to be very, 135 00:09:45,610 --> 00:09:49,750 ‫very careful because there are potentially multiple things that could go wrong. 136 00:09:49,810 --> 00:09:55,480 ‫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 137 00:09:55,480 --> 00:09:55,750 ‫of them. 138 00:09:55,750 --> 00:09:59,840 ‫And I'm going to share links with you as well, where you can read a lot more into that. 139 00:09:59,860 --> 00:10:04,410 ‫So that's about the basic idea of how to create threads. 140 00:10:04,420 --> 00:10:06,670 ‫As you can see, that's how you can set things up. 141 00:10:07,210 --> 00:10:13,900 ‫So threads are generally a little piece of work or a piece of work that is executed, and you can execute 142 00:10:13,900 --> 00:10:18,750 ‫multiple things at the same time if you have multiple cores in your processor. 143 00:10:18,760 --> 00:10:25,540 ‫So let's say you have a four core processor, then four threads or four processors at the same time 144 00:10:25,540 --> 00:10:27,370 ‫can be executed. 145 00:10:27,610 --> 00:10:36,520 ‫If you have, let's say, only one processor, then only one thread or one task at the time can be executed, 146 00:10:36,520 --> 00:10:37,740 ‫will never happen to do it. 147 00:10:37,750 --> 00:10:45,130 ‫They will be executed in parallel because this one processor only does one specific thing at a time. 148 00:10:46,510 --> 00:10:53,800 ‫That means that if you know that you're working with a multicore processor or your application will 149 00:10:53,800 --> 00:11:00,970 ‫run on a multicore processor, you can you work with multiple threads, otherwise you run into issues. 150 00:11:00,970 --> 00:11:07,960 ‫But generally how it works, if you only have one core, then the main thread will run and only if the 151 00:11:07,960 --> 00:11:13,690 ‫main thread is idle, then it will go ahead and do other things. 152 00:11:13,690 --> 00:11:15,850 ‫So it will run other threats. 153 00:11:15,880 --> 00:11:21,010 ‫Let's say you have a UI and on that UI you just display things. 154 00:11:21,010 --> 00:11:22,030 ‫You have a button. 155 00:11:22,030 --> 00:11:26,670 ‫Then if you hover over the button, it will change its appearance, stuff like that. 156 00:11:26,680 --> 00:11:28,960 ‫That's something that is done by the main thread. 157 00:11:28,960 --> 00:11:32,830 ‫But if the main thread is not busy, then the other threads will start. 158 00:11:32,830 --> 00:11:38,950 ‫So what if you have something that should be run in the background, then that will be done whenever 159 00:11:38,950 --> 00:11:43,300 ‫thread one the main thread is not busy or thread zero. 160 00:11:43,300 --> 00:11:44,350 ‫Actually it is here. 161 00:11:44,620 --> 00:11:46,840 ‫So that's about threads. 162 00:11:46,930 --> 00:11:51,220 ‫We will look into threads a little more of course, but generally that's what I wanted to show you to 163 00:11:51,220 --> 00:11:51,880 ‫start off.