Sunday, July 7, 2013

Multithreading in C# – Part2

Continuing from the previous post, here are some basic operations on threads.

Naming Threads

Each threads that we create, or in fact the main thread itself, can be named. That is to say, you can name the individual threads, so as to make it easier to manage multiple threads in case the number of threads increases. Continuing with our previous example, where we were having two threads; one main thread and another custom thread. Here is how we can name the two.

   1: class Program
   2:     {
   3:         static void Main(string[] args)
   4:         {
   5:             Program myProgram = new Program();
   6:             Thread.CurrentThread.Name = "Main";
   7:  
   8:             Thread th = new Thread(myProgram.ShowThread);
   9:             th.Name = "Second Thread";
  10:             th.Start();                        
  11:             for (int i = 0; i<=10 ; i++)
  12:             {
  13:                 Console.WriteLine(Thread.CurrentThread.Name 
  14:                                          + " " + i.ToString());                                
  15:                 Thread.Sleep(200);
  16:             }
  18:             Console.ReadLine();
  19:         }
  20:  
  21:         private void ShowThread()
  22:         {
  23:             for (int i = 0; i<=10; i++)
  24:             {
  25:                 Console.WriteLine(Thread.CurrentThread.Name 
  26:                                         + " " + i.ToString());
  27:                 Thread.Sleep(10);
  28:             } 
  29:         }
  30:     }

Thread.CurrentThread.Name give the name of the current thread being executed.

Thread Joining

Imagine the situation when you want to print the result of the calculation a thread processes. Now, if you just print the result just after you start the thread, the result would not display, unless of course, you are using a super computer which has enormous processing speed. 
To understand this concept, let’s go through a demo. We are going to create a thread, that increment the value of an integer variable by 1, 100 times. And each time it does, it sleeps for 10 seconds. Now, in the main thread, we displayed the value of the variable onto the screen, followed by starting the thread. Here is the code:

   1: class Program
   2:     {
   3:         static void Main(string[] args)
   4:         {
   5:             Program myProgram = new Program();            
   6:             int count = 0;
   7:  
   8:             Thread th = new Thread(() => {
   9:                 for (int i = 0; i <= 100; i++)
  10:                 {
  11:                     count++;
  12:                     Thread.Sleep(10);
  13:                 } 
  14:             });
  15:  
  16:             th.Start();
  17:             
  18:             Console.WriteLine(count.ToString());
  19:             Console.ReadLine();
  20:         }
  21:     }

If you just give it a shot, predicting the output of the program, you must have gotten right answer, that is 0. You might add in the explanation part, since the calculation is happening inside the thread other than the main thread, the main thread has no clue of when the calculation is going to complete. Unless of course the main thread knows that there is a thread which is processing some calculations, and I have to wait until it finishes with them.
In order to do this, we join the thread th with the main thread. This is to let main thread know that another thread exists, and it have to wait unless that thread completes it’s processing. So our code would be:

   1: class Program
   2:     {
   3:         static void Main(string[] args)
   4:         {
   5:             Program myProgram = new Program();            
   6:             int count = 0;
   7:  
   8:             Thread th = new Thread(() => {
   9:                 for (int i = 0; i <= 100; i++)
  10:                 {
  11:                     count++;
  12:                     Thread.Sleep(10);
  13:                 } 
  14:             });
  15:  
  16:             th.Start();
  17:             //Wait for th to complete it's calculations
  18:             th.Join();
  19:             Console.WriteLine(count.ToString());
  20:             Console.ReadLine();
  21:         }
  22:     }

This time the output would be, yes you are right, it’s 101. Stay tuned for more.

Happy Reading!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top