Lifecycle and States of a Thread in C#

A thread in C# at any point of time exists in any one of the following states. A thread lies only in one of the shown states at any instant:

  1. Unstarted
  2. Runnable
  3. Running
  4. Not Runnable
  5. Dead

Flow Chart:

Life Cycle of a thread
  1. Unstarted state: When an instance of a Thread class is created, it is in the unstarted state, means the thread has not yet started to run when the thread is in this state. Or in other words Start() method is not called.
    Thread thr = new Thread(); 

    Here, thr is at unstarted state.

  2. Runnable State: A thread that is ready to run is moved to runnable state. In this state, a thread might actually be running or it might be ready to run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run. Or in other words, the Start() method is called.
  3. Running State: A thread that is running. Or in other words, the thread gets the processor.
  4. Not Runnable State: A thread that is not executable because
    • Sleep() method is called.
    • Wait() method is called.
    • Due to I/O request.
    • Suspend() method is called.
  5. Dead State: When the thread completes its task, then thread enters into dead, terminates, abort state.
Implementing Thread States in C#

In C#, to get the current state of the thread, use ThreadState or IsAlive property provided by the Thread class.

Synatx:

public ThreadState ThreadState{ get; }

OR

public bool IsAlive { get; }

Thread class provides different types of methods to implement the states of the threads.

  • Sleep() method is used to temporarily suspend the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution.
  • Join() method is used to make all the calling thread to wait until the main thread, i.e. joined thread complete its work.
  • Abort() method is used to abort the thread.
  • Suspend() method is called to suspend the thread.
  • Resume() method is called to resume the suspended thread.
  • Start() method is used to send a thread into runnable State.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// states of the thread
using System;
using System.Threading;
  
public class MyThread {
  
    // Non-Static method
    public void thread()
    {
        for (int x = 0; x < 2; x++) {
            Console.WriteLine("My Thread");
        }
    }
}
  
public class ThreadExample {
  
    // Main method
    public static void Main()
    {
  
        // Creating instance for 
        // mythread() method
        MyThread obj = new MyThread();
  
        // Creating and initializing 
        // threads Unstarted state
        Thread thr1 = new Thread(new ThreadStart(obj.thread));
  
        Console.WriteLine("ThreadState: {0}"
                          thr1.ThreadState);
  
        // Running state
        thr1.Start();
        Console.WriteLine("ThreadState: {0}"
                           thr1.ThreadState);
  
        // thr1 is in suspended state
        thr1.Suspend();
        Console.WriteLine("ThreadState: {0}"
                           thr1.ThreadState);
  
        // thr1 is resume to running state
        thr1.Resume();
        Console.WriteLine("ThreadState: {0}",
                          thr1.ThreadState);
    }
}

chevron_right


Output:

ThreadState: Unstarted
ThreadState: Running
ThreadState: SuspendRequested
ThreadState: Running
My Thread
My Thread

Explanation: The above example shows the different states of thr1 thread. These states of a thr1 thread are determined by using the ThreadState property of the Thread class. Also, we use Suspend() and Resume() method to suspend the current execution of the thread and resume the suspended thread by using the Resume method.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.