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.
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.
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).
- static void Main(string[] args)
- {
- Console.Title = "Foreground & Background Thread Example";
- Thread t2 = new Thread(PrintThread2);
- t2.Name = "Background";
- t2.IsBackground = true;
- t2.Start();
-
- Console.WriteLine("Main thread Exits");
- }
-
- static void PrintThread2()
- {
- for (int i = 0; i < 5; i++)
- {
- Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);
- Thread.Sleep(1000);
- }
- }
Output 1
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).
- static void Main(string[] args)
- {
- Console.Title = "Foreground & Background Thread Example";
-
- Thread t1 = new Thread(PrintThread1);
- t1.Name = "Foreground";
- t1.Start();
-
- Console.WriteLine("Main thread Exits");
- }
-
- static void PrintThread1()
- {
- for (int i = 0; i < 5; i++)
- {
- Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);
- Thread.Sleep(1000);
- }
- }
Output 2
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.
- static void Main(string[] args)
- {
- Console.Title = "Foreground & Background Thread Example";
-
- Thread t1 = new Thread(PrintThread1);
- t1.Name = "Foreground";
- t1.Start();
-
- Thread t2 = new Thread(PrintThread2);
- t2.Name = "Background";
- t2.IsBackground = true;
- t2.Start();
-
- Console.WriteLine("Main thread Exits");
- }
-
- static void PrintThread1()
- {
- for (int i = 0; i < 5; i++)
- {
- Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);
- Thread.Sleep(1000);
- }
- }
-
- static void PrintThread2()
- {
- for (int i = 0; i < 5; i++)
- {
- Console.WriteLine("I am {0} Thread", Thread.CurrentThread.Name);
- Thread.Sleep(1000);
- }
- }
Output 3a (instance1)
Output 3b (instance2)
I hope you have liked the article, please share your comments/suggestions in the section below.