jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Thread Class

Obviously, c sharp has everything you might need for multi-threaded programming. That is where the Thread class comes into the picture. We already discussed the basics of thread, the concept in a previous post. Do look into that before you read through this.  

Whenever you wish to create a new thread, you simply create a 'Thread' object. After you create a thread object, you can then assign a method to it by sending it as a parameter. After that, when you are ready, you simply start the thread by using the ThreadStart method. After that, the thread simply starts.  

Now, if you really want to try out the whole multi thread thing, you will become familiar with the 'Thread.Sleep(sleep_duration)' method. This method takes an integer value, measured in microseconds, as parameter. Through this, you tell the current thread to go to sleep. When you put a thread to sleep, that’s like telling the CPU that, "okay, the thread no longer needs the CPU". The thread takes a back seat for the duration mentioned in the sleep parameter.  

You should also become familiar with this method 't.Join()' where 't' is the thread object you are working with. At this point, you might run into a confusion. The confusion is, you already started the thread when you did ThreadStart. What is this Join thing now?  

Well, the thing is, the moment you start a program the program itself runs as a thread. When you create a new thread, the total threads created by your program becomes 2. At this point, your thread can start running the first chance it gets i.e the default thread of the program stops or the CPU says it is available. Or, you can specifically instruct when it should start running.  

That is where t.Join() comes into picture. The exact location where you place t.Join is the place where your thread waits. It waits to see if the other thread stops. I also wish to add here that when I say thread 'stops' it means, the thread has either completed execution. Or, it has gone to sleep. Either one of these is taken to understand that the thread has stopped. One way to look at this, is to think that when a thred has stopped, it usually means the thread has 'stopped' asking the CPU for cycles. The CPU or the program itself need not know if the thread actually stopped or paused or finished.  

Also, notice that when you do thread sleep, you use Thread.Sleep. When you do stuff like start or join, you use the thread object like t.Join(). The reasoning behind this is that, with sleep, you simply put whichever thread is currently active to go to sleep. This makes sense because, while your program can be multithreaded, only one thread can be active at any given point of time. However, with Join, you could have multiple thread objects waiting for instructions when to join.  

Another thing is the sleep duration. Even if you are working on the most basic of laptops or computers, a simple thread demo (like you will find in our cod repo) may not give you the idea of a multithreaded environment. In fact, you may notice that despite asking the current thread to go to sleep, it still manages to finishes its work and then go to sleep. Or rather, it simply is done with all its work. This is because, your computer (old or new) is simply too fast. That is why, when learning you will use a larger number when putting a thread to sleep.  

Since CPU cycles vary from one computer to another, the output you see on one computer vary from another when working with threads. When playing around with threads, ensure that you play around with the sleep time duration and get the results you want.  

As always, find the relevant code in our repo. 

Comments are closed