Thread priority determines the relative importance of a thread during execution. It helps the operating system decide which thread should get CPU time first when multiple threads are running.
In this chapter, you will learn what thread priority is, how to set the priority of a thread, and practical examples of using different thread priorities in C#.
Thread priority is a property of the Thread class that indicates the priority level of a thread. A thread with a higher priority has a greater chance of being executed before a thread with a lower priority. However, the execution order is controlled by the operating system, so the actual order is not guaranteed.
The priority of a thread is set by using the Priority property of the Thread class.
The following syntax is used to set the priority of a thread in C#:
Thread thread = new Thread(MethodName); thread.Priority = ThreadPriority.Highest; thread.Start();
In this syntax:
The following table demonstrate several thread priority levels.
| Priority | Description |
|---|---|
| Lowest | It assigns the lowest priority to the thread. |
| BelowNormal | It assigns a priority below the normal level. |
| Normal | It assigns the default priority to the thread. |
| AboveNormal | It assigns a priority above the normal level. |
| Highest | It assigns the highest priority to the thread. |
The following examples demonstrate how to assign different priority levels to threads.
This example shows how to assign different priorities to three threads. The thread with the highest priority has a greater chance of executing before the others.
using System;
using System.Threading;
public class MyThread
{
public void Thread1()
{
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name+" is running");
}
}
public class ThreadExample
{
public static void Main()
{
MyThread mt = new MyThread();
Thread t1 = new Thread(new ThreadStart(mt.Thread1));
Thread t2 = new Thread(new ThreadStart(mt.Thread1));
Thread t3 = new Thread(new ThreadStart(mt.Thread1));
t1.Name = "Player1";
t2.Name = "Player2";
t3.Name = "Player3";
t3.Priority = ThreadPriority.Highest;
t2.Priority = ThreadPriority.Normal;
t1.Priority = ThreadPriority.Lowest;
t1.Start();
t2.Start();
t3.Start();
}
}
Output:
Player1 is running Player3 is running Player2 is running
Explanation
In this example, three threads are created and assigned different priority levels. Player3 is assigned the Highest priority, Player2 is assigned Normal, and Player1 is assigned Lowest. Although the higher-priority thread has a better chance of running first, the execution order is not guaranteed.
This example demonstrates how to assign the Highest priority to a single thread and display its priority.
using System;
using System.Threading;
class Program
{
static void ShowMessage()
{
Thread t = Thread.CurrentThread;
Console.WriteLine("Thread Name: " + t.Name);
Console.WriteLine("Priority: " + t.Priority);
}
static void Main()
{
Thread thread = new Thread(ShowMessage);
thread.Name = "MainWorker";
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
}
Output:
Thread Name: MainWorker Priority: Highest
Explanation
In this example, we create a thread named MainWorker and assign it the Highest priority. Inside the thread, the current thread's name and priority are displayed by using the CurrentThread property.
This example shows how to create multiple threads with different priority levels and display their assigned priorities.
using System;
using System.Threading;
class Program
{
static void DisplayPriority()
{
Thread t = Thread.CurrentThread;
Console.WriteLine($"{t.Name} : {t.Priority}");
}
static void Main()
{
Thread t1 = new Thread(DisplayPriority);
Thread t2 = new Thread(DisplayPriority);
Thread t3 = new Thread(DisplayPriority);
t1.Name = "Thread A";
t2.Name = "Thread B";
t3.Name = "Thread C";
t1.Priority = ThreadPriority.Lowest;
t2.Priority = ThreadPriority.Normal;
t3.Priority = ThreadPriority.Highest;
t1.Start();
t2.Start();
t3.Start();
}
}
Output:
Thread A : Lowest Thread B : Normal Thread C : Highest
Explanation
In this example, three threads are created with different priority levels. Each thread prints its name and assigned priority. This demonstrates how the Priority property can be used to assign different execution priorities to threads.
We request you to subscribe our newsletter for upcoming updates.