Threading Simplified - Part 3 (Threads type)

I am again here to continue the discussion around Threading. Today we will explain types of threads related concepts.

In case you haven't looked at the previous articles, you can go through the following.

Let's begin the discussion with the types of threads we have in C#.

Foreground Threads

Foreground threads are those that live even after the main thread exits. The application will continue to run if a foreground thread is alive or executing.

By default a thread is created as a foreground thread.

Background Threads

Background threads die when the main thread exists and no foreground threads are alive. These are not created by default and we need to set the property to make a thread a background thread.

The difference can be visualized using the following diagram.

In the diagram we can see that at instance t1, when the main thread (in Red) exists, the background thread (in Blue) also dies.

background thread

In another diagram below, we can see that even if main thread (in Red) exists and a foreground thread (in Green) is alive, the background thread (in Blue) can also be alive. This diagram also explains that a foreground thread can continue executing even when the main thread dies.

main thread

Now let's see the implementation using a simple example. We will go through the three possible cases here and analyze the behavior.

Case 1: Only with background thread

Here we can see that the moment the main thread quits, the background thread also quits since it is able to execute only one iteration out of five (see the Output 1).

  1. static void Main(string[] args)          
  2. {    
  3.     Console.Title = "Foreground & Background Thread Example";  
  4.     Thread t2 = new Thread(PrintThread2);  
  5.     t2.Name = "Background";  
  6.     t2.IsBackground = true;  
  7.     t2.Start();  
  8.   
  9.     Console.WriteLine("Main thread Exits");  
  10. }  
  11.   
  12. static void PrintThread2()  
  13. {              
  14.     for (int i = 0; i < 5; i++)  
  15.     {  
  16.         Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);  
  17.         Thread.Sleep(1000);  
  18.     }  
  19. }  

Output 1

Only with background thread

Case 2: Only with foreground thread

In this case, we can see that even if the main thread quits, the foreground thread continues to execute since it completes all five iterations (see the Output 2).

  1. static void Main(string[] args)          
  2. {  
  3.     Console.Title = "Foreground & Background Thread Example";  
  4.   
  5.     Thread t1 = new Thread(PrintThread1);  
  6.     t1.Name = "Foreground";  
  7.     t1.Start();  
  8.   
  9.     Console.WriteLine("Main thread Exits");  
  10. }  
  11.   
  12. static void PrintThread1()  
  13. {             
  14.     for (int i = 0; i < 5; i++)  
  15.     {  
  16.         Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);  
  17.         Thread.Sleep(1000);  
  18.     }  
  19. }

Output 2

Output


Case 3: Background & foreground thread together

In this case, when executing both types of threads together, we can observe the following two points:

  • Foreground thread continues to execute even if the main thread exits or dies.

  • Even if the main thread exits but a foreground thread is alive, the background thread will continue to execute.
  1. static void Main(string[] args)          
  2. {  
  3.     Console.Title = "Foreground & Background Thread Example";  
  4.   
  5.     Thread t1 = new Thread(PrintThread1);  
  6.     t1.Name = "Foreground";  
  7.     t1.Start();  
  8.   
  9.     Thread t2 = new Thread(PrintThread2);  
  10.     t2.Name = "Background";  
  11.     t2.IsBackground = true;  
  12.     t2.Start();  
  13.   
  14.     Console.WriteLine("Main thread Exits");  
  15. }  
  16.   
  17. static void PrintThread1()  
  18. {             
  19.     for (int i = 0; i < 5; i++)  
  20.     {  
  21.         Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);  
  22.         Thread.Sleep(1000);  
  23.     }  
  24. }  
  25.   
  26. static void PrintThread2()  
  27. {              
  28.     for (int i = 0; i < 5; i++)  
  29.     {  
  30.         Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);  
  31.         Thread.Sleep(1000);  
  32.     }  
  33. }
Output 3a (instance1)

Output 3a


Output 3b (instance2)

Output 3b


I hope you have liked the article, please share your comments/suggestions in the section below.

Up Next
    Ebook Download
    View all
    Learn
    View all