A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-thread-priority-in-multithreading/ below:

C# Thread Priority in Multithreading

C# Thread Priority in Multithreading

Last Updated : 11 Jul, 2025

In a multithreaded environment, each thread has a priority that determines how frequently the thread is allocated CPU resources by the operating system. The Thread.Priority property in C# is used to set or get the priority of a thread.

Thread Priority Levels

The ThreadPriority enum under the System.Threading namespace specifies the following priority levels:

Priority Level

Description

Value

Highest

Highest priority for a thread.

4

AboveNormal

Higher than Normal priority.

3

Normal (Default)

Default priority.

2

BelowNormal

Lower than Normal priority.

1

Lowest

Lowest priority for a thread.

0

Syntax:

public ThreadPriority Priority{ get; set; }

Exceptions:

Example 1: This example demonstrates how to set and get the priority of threads.

C#
// C# program to illustrate how to 
// set and get the priority of threads 
using System;
using System.Threading;

class Geeks
{
	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 100 milliseconds
		Thread.Sleep(100);
	}
}

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

Example 2: This example demonstrates how thread priority can influence thread execution.

C#
// C# program to illustrate the 
// Priority property of Thread class 
using System;
using System.Threading;

class Geeks
{
	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
T1 thread is working..
T2 thread is working..


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4