Multi-threading And Asynchronous Concepts

In this article you will learn about multi-threading and asynchronous concepts.

Earlier computers were based on batch processing. In batch processing, a central processing unit (CPU) was capable of doing one task at a time. If a CPU has to take long time to execute a task, the other processes would have paused. In that case the whole machine would freeze. Things could get worse if there is a bug in the running task. The only solution eas to restart the machine.
 
The solution of this problem is Thread. In modern operating systems each application runs in its own process. If this application affects anything the other processes will remain unaffected. Each process has its own "Virtual Memory." Each process is allowed to run for a particular time period. When this time period is over, thread is paused, its state is saved, and the operating system switches to another thread. It is called "Context Switching" Actually at one time, only one process runs on CPU. But today, as computing is so much faster and powerful and context switching happens so fast, we feel that computer is executing many tasks at a time. In .NET, for threading we use Thread Class. It can be found in System.Threading namespace.

Note: Always import System.Threading in your project before using Thread Class.
Example: This is a simple example of threading,

                                                                                   

Common Mistake: When you call a function in Thread class, people commonly call it parenthesis like (). Do not call it with parenthesis. Just write down the name of the method.

Output




The above example shows an example of using the Thread class to run a method. The Console class synchronizes the use of the output stream for you so you can write to it from multiple threads. Synchronization is the mechanism of ensuring that two threads don’t execute a specific portion of your program at the same time. In the case of a console application, this means that no two threads can write data to the screen at the exact same time. If one thread is working with the output stream, other threads will have to wait before it’s finished.
 
Join Method: 

Join is a synchronous method that blocks the other thread until the thread which is on the CPU terminates.

Example:

Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Threading;  
  7.   
  8. namespace SimpleThread  
  9. {  
  10.     class Program   
  11.     {  
  12.         //Thread Method  
  13.         public static void ThreadMethod()  
  14.       {  
  15.             //Loop  
  16.             for (int i = 0; i <= 5; i++)   
  17.             {  
  18.                 Console.WriteLine("Thread 0");  
  19.   
  20.             }  
  21.   
  22.         }  
  23.         public static void ThreadMethod1()  
  24.       {  
  25.             //Loop  
  26.             for (int i = 0; i <= 5; i++)  
  27.             {  
  28.                 Console.WriteLine("Thread 1");  
  29.   
  30.             }

  31.         }  
  32.   
  33.         static void Main(string[] args)  
  34.       {  
  35.   
  36.             //Thread Declartion and calling   
  37.             Thread thread = new Thread(new ThreadStart(ThreadMethod));  
  38.             thread.Start(); //Start Thread  
  39.             Thread thread1 = new Thread(new ThreadStart(ThreadMethod1)); //thread for Method2  
  40.             thread1.Start(); //Start Thread  
  41.             Console.ReadKey(); //For holding Screen  
  42.         }  
  43.     }  
  44.   
  45. }  
Output 1
 






















Output 2























Note

Every time you compile the code, you might geta different answer. So do not worry about that. Now let's look what happes with the use of Join method.

Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Threading;  
  7.   
  8. namespace SimpleThread  
  9. {  
  10.     class Program   
  11.     {  
  12.         //Thread Method  
  13.         public static void ThreadMethod()   
  14.       {  
  15.             //Loop  
  16.             for (int i = 0; i <= 5; i++)  
  17.             {  
  18.                 Console.WriteLine("Thread 0");  
  19.   
  20.             }  
  21.   
  22.         }  
  23.         public static void ThreadMethod1()  
  24.       {  
  25.             //Loop  
  26.             for (int i = 0; i <= 5; i++)  
  27.             {  
  28.                 Console.WriteLine("Thread 1");  
  29.   
  30.             }
  31.   
  32.         }  
  33.   
  34.         static void Main(string[] args)  
  35.       {  
  36.   
  37.             //Thread Declartion and calling   
  38.             Thread thread = new Thread(new ThreadStart(ThreadMethod));  
  39.             thread.Start(); //Start Thread  
  40.             thread.Join(); //Join Method  
  41.             Thread thread1 = new Thread(new ThreadStart(ThreadMethod1)); //thread for Method2  
  42.             thread1.Start(); //Start Thread  
  43.             Console.ReadKey(); //For holding Screen  
  44.         }  
  45.     }  
  46.   
  47. }  
Explanation

The only change is the thread.Join() method. Now the second thread will not execute until first thread terminates.

Output



Now after every compilation you will get this result.

Foreground and Background Thread

Foreground thread keeps your application alive. Once foreground thread ends, application ends and background threads also terminates. By default all threads are foreground.

Note: 
To make a thread background just make the isBackground property true. If this property is false of a thread, it means it is foreground.

e.g

Thread t=new Thread();
t.IsBackground = true;

Code
  1. class Program  
  2. {  
  3.     public static void ThreadMethod()   
  4.   {  
  5.         for (int i = 0; i < 10; i++)  
  6.         {  
  7.             Console.WriteLine("Foreground Thread");  
  8.             Thread.Sleep(300);  
  9.         }  
  10.         Console.WriteLine("Foreground thread is terminated");  
  11.     }  
  12.     public static void ThreadMethod1()  
  13.   {  
  14.         for (int i = 0; i < 10; i++)  
  15.         {  
  16.             Console.WriteLine("Background thread");  
  17.             Thread.Sleep(200);  
  18.         }  
  19.     }
  20.   
  21.     static void Main(string[] args)  
  22.   {  
  23.         Thread thread = new Thread(new ThreadStart(ThreadMethod));  
  24.         thread.Start();  
  25.         thread.Join();  
  26.         Thread thread1 = new Thread(new ThreadStart(ThreadMethod1));  
  27.         thread1.IsBackground = true//to make a thread background  
  28.         thread1.Start();  
  29.   
  30.     }  
  31. }  
Output

Foreground thread will execute 10 times and application will terminate.

Explanation

I have used Join method with which foreground thread will execute until it terminates. As second thread is background and background thread executes till the execution of foreground thread. As foreground thread terminates, background thread also terminates. So when the above code executes, the first thread executes ten times and then application end.

Note

In Console Application, use Console.ReadKey() for screen .The background thread will also execute 10 times because Console.Readkey() behaves as foreground thread. And we know that background thread executes until foreground thread is alive.

ParameterizedThreadStart

We can pass data through start method of a thread. For this purpose, we use ParameterizedThreadStart delegate instead of simple ThreadStart delegate.

Example
  1. class Program  
  2. {  
  3.     public static void ThreadMethod(object o)  
  4.   {  
  5.         for (int i = 0; i < (int) o; i++)   
  6.         {  
  7.             Console.WriteLine("thread Proc{0}", i);  
  8.         }  
  9.     }  
  10.     static void Main(string[] args)  
  11.   {  
  12.         Thread thread = new Thread(new ParameterizedThreadStart(ThreadMethod));  
  13.         thread.Start(5);  
  14.         Console.ReadKey();  
  15.     }  
  16. }  
OutPut

thread proc0
thread proc1
thread proc2
thread proc3
thread proc4

Explanation:

In that case, value is passed to threadMethod as object, you can cast it to any other type.

Thread Static Attribute

By making a field or data member with the thread static attribute each thread gets its own copy of field.

Example

  1. class Program  
  2.   
  3. {    
  4.     [ThreadStatic] //for making a field with thread static attribute  
  5.       public static int field = 0;  
  6.       static void Main(string[] args)  
  7.       {  
  8.           new Thread(() =>  
  9.               {  
  10.                   for (int i = 0; i < 10; i++)  
  11.                   {  
  12.                       field++;  
  13.                       Console.WriteLine("thread 1:{0} ", field);  
  14.                   }  
  15.               }).Start();  
  16.           new Thread(() =>  
  17.               {  
  18.                   for (int i = 0; i < 10; i++)  
  19.                   {  
  20.                     field++;  
  21.                     Console.WriteLine("thread 2:{0} ", field);  
  22.                 }  
  23.             }).Start();  
  24.         Console.ReadKey();  
  25.     }  
  26. }  

Out put:

With the ThreadStaticAttribute applied, the maximum value of field becomes ten. If you remove it, you can see that both threads access the same value and it becomes twenty.