C# | Thread Priority in Multithreading

24 Jan, 2019
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.
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 threadsusing 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 classusing 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:
https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.priority?view=netframework-4.7.2
CSharp Multithreading
CSharp Thread Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Delegates
C# | Abstract Classes
Introduction to .NET Framework
C# | Arrays
C# | Method Overriding
C# Dictionary with examples
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | .NET Framework (Basic Architecture and Component Stack)
C# | Constructors