In C#, each thread has a priority that determines how frequently it gets access to the CPU. The Thread.Priority property is used to get or set the scheduling priority of a thread. The priority of a thread can be set to one of the values defined in the ThreadPriority enumeration: Highest, AboveNormal, Normal, BelowNormal, and Lowest.
When a thread is created, it gets a default priority of ThreadPriority.Normal. You can change the priority of a thread by setting the Priority property. Here is an example:
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Thread thread1 = new Thread(SomeMethod) { Name = "Thread 1", Priority = ThreadPriority.Normal };
Thread thread2 = new Thread(SomeMethod) { Name = "Thread 2", Priority = ThreadPriority.Lowest };
Thread thread3 = new Thread(SomeMethod) { Name = "Thread 3", Priority = ThreadPriority.Highest };
Console.WriteLine($"Thread 1 Priority: {thread1.Priority}");
Console.WriteLine($"Thread 2 Priority: {thread2.Priority}");
Console.WriteLine($"Thread 3 Priority: {thread3.Priority}");
thread1.Start();
thread2.Start();
thread3.Start();
Console.ReadKey();
}
public static void SomeMethod()
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine($"Thread Name: {Thread.CurrentThread.Name} Printing {i}");
}
}
}
Impact of Thread Priority
Thread priority influences how threads are scheduled by the operating system. However, it does not guarantee that a high-priority thread will always execute before a low-priority thread. Factors such as context switching and the availability of shared resources can affect the actual execution order.
For example, if a high-priority thread is waiting for I/O operations, a lower-priority thread may get CPU time and complete its execution. This can lead to scenarios where a high-priority thread gets less CPU time than a low-priority thread over a specific period
Example Demonstrating Thread Priority
Here is an example that demonstrates how changing the priority of threads affects their
using System;
using System.Threading;
class PriorityTest
{
static volatile bool loopSwitch = true;
[ThreadStatic] static long threadCount = 0;
public static void Main()
{
PriorityTest priorityTest = new PriorityTest();
Thread thread1 = new Thread(priorityTest.ThreadMethod) { Name = "ThreadOne" };
Thread thread2 = new Thread(priorityTest.ThreadMethod) { Name = "ThreadTwo", Priority = ThreadPriority.BelowNormal };
Thread thread3 = new Thread(priorityTest.ThreadMethod) { Name = "ThreadThree", Priority = ThreadPriority.AboveNormal };
thread1.Start();
thread2.Start();
thread3.Start();
Thread.Sleep(10000);
loopSwitch = false;
}
public void ThreadMethod()
{
while (loopSwitch)
{
threadCount++;
}
Console.WriteLine($"{Thread.CurrentThread.Name,-11} with {Thread.CurrentThread.Priority,11} priority has a count = {threadCount:N0}");
}
}
In this example, three threads are created with different priorities. The threads increment a variable in a loop for 10 seconds. The output shows how the priority affects the count of each thread
Important Considerations
- ThreadStateException: Thrown if the thread has reached a final state, such as Aborted.
- ArgumentException: Thrown if the value specified for a set operation is not a valid ThreadPriority value.
- Operating System Scheduling: The operating system may not always honor the thread priority due to various factors.
By understanding and using thread priorities effectively, you can optimize the performance of your multithreaded applications in C#.
Refer:
- Thread.Priority Property
- Threads Priorities in C#
- C# Thread Priority in Multithreading.