Open In App

C# | Thread Priority in Multithreading

Last Updated : 24 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In a Multithreading environment, each thread has their own priority. A thread’s priority shows how frequently a thread gains the access to CPU resources. Whenever we create a thread in C#, it always has some priority assigned to it.

Important Points:

  • A programmer can explicitly assign priority to a thread.
  • The by default priority of a thread is Normal.
  • Operating system does not assign the priority of threads.
  • If a thread has reached a final state, such as Aborted, then this will give ThreadStateException.
  • If the value specified for a set operation is not a valid ThreadPriority value, then this will give ArgumentException
  • It is not guaranteed that the thread whose priority is high will executes first and the thread whose priority is low will executes after. Due to context switching the highest priority thread may execute after lowest priority thread.
  • When you are assigning a priority to a thread you need to be careful, if you are not doing carefully you may get thread starvation.
  • The thread priority will always depend upon the process priority or parent container, so by setting the thread priority to highest will not correspond to real-time execution.

How to set and get the priority of thread?

Thread.Priority Property is used to get or set a value indicating the scheduling priority of a thread.

Syntax:

public ThreadPriority Priority{ get; set; }

Here, the ThreadPriority enum under System.Threading namespace is responsible for specifying the scheduling priority of a Thread and the priorities are:

  • Highest: The value of this priority is 4.
  • AboveNormal: The value of this priority is 3.
  • Normal: The value of this priority is 2.
  • BelowNormal: The value of this priority is 1.
  • Lowest: The value of this priority is 0.

Property Value: One of the ThreadPriority values. The default value is Normal.

Exceptions:

  • ThreadStateException: If the thread has reached a final state, such as Aborted.
  • ArgumentException: If the value specified for a set operation is not a valid ThreadPriority value.

Example 1:




// C# program to illustrate how to
// set and get the priority of threads
using System;
using System.Threading;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating and initializing threads
        Thread T1 = new Thread(work);
        Thread T2 = new Thread(work);
        Thread T3 = new Thread(work);
  
        // Set the priority of threads
        T2.Priority = ThreadPriority.Highest;
        T3.Priority = ThreadPriority.BelowNormal;
        T1.Start();
        T2.Start();
        T3.Start();
  
        // Display the priority of threads
        Console.WriteLine("The priority of T1 is: {0}",
                                          T1.Priority);
  
        Console.WriteLine("The priority of T2 is: {0}",
                                          T2.Priority);
  
        Console.WriteLine("The priority of T3 is: {0}",
                                          T3.Priority);
    }
  
    public static void work()
    {
  
        // Sleep for 1 second
        Thread.Sleep(1000);
    }
}


Output:

The priority of T1 is: Normal
The priority of T2 is: Highest
The priority of T3 is: BelowNormal

Example 2:




// C# program to illustrate the
// Priority property of Thread class
using System;
using System.Threading;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating and initializing threads
        Thread T1 = new Thread(work1);
        Thread T2 = new Thread(work2);
  
        // Set the priority of threads
        // Here T2 thread executes first 
        // because the Priority of T2 is
        // highest as compare to T1 thread
        T1.Priority = ThreadPriority.Lowest;
        T2.Priority = ThreadPriority.Highest;
        T1.Start();
        T2.Start();
    }
    public static void work1()
    {
  
        Console.WriteLine("T1 thread is working..");
    }
    public static void work2()
    {
  
        Console.WriteLine("T2 thread is working..");
    }
}


Output:

T2 thread is working..
T1 thread is working..

Reference:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads