Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# Multithreading
Next article icon

C# Thread Priority in Multithreading

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  • A programmer can explicitly assign a priority to a thread using the Priority property.
  • By default, a thread’s priority is set to Normal.
  • The operating system handles thread scheduling, but priorities influence how threads are executed.
  • Setting priorities incorrectly can lead to thread starvation, where lower-priority threads may not get enough CPU time.
  • A high-priority thread does not guarantee that it will execute before a low-priority thread because of context switching and other OS-level scheduling mechanisms.
  • The thread priority always depends on the process priority (the parent container).

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:

  • 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: 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.. 


Next Article
C# Multithreading
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp Multithreading

Similar Reads

  • Thread.CurrentThread Property in C#
    A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as CurrentThread to check the current running thread. Or in other words, the value of this property indicates the current running thread. Syntax: public static Thread CurrentThr
    2 min read
  • C# Multithreading
    C# is a multi-paradigm programming language that supports several programming styles, including procedural, object-oriented, and functional programming. One of the essential features of C# is its support for multithreading, which enables developers to write applications that can perform multiple tas
    10 min read
  • C# | Thread(ParameterizedThreadStart) Constructor
    Thread(ParameterizedThreadStart) Constructor is used to initialize a new instance of the Thread class. It defined a delegate which allows an object to pass to the thread when the thread starts. This constructor gives ArgumentNullException if the parameter of this constructor is null. Syntax: public
    2 min read
  • C# Main Thread
    In C#, threads are the smallest units of execution that allow parallel execution of code, enabling multiple tasks to run concurrently within a single process. The Thread class in the System.Threading namespace is used to create and manage threads. In C#, the Main method is the entry point of any con
    5 min read
  • C# Joining Threads
    In C#, multiple threads can be created and run concurrently to perform parallel tasks. Sometimes it is necessary to wait for one thread to finish its execution before proceeding or for all threads to complete before continuing further. The Thread.Join() method allows one thread to wait for another t
    5 min read
  • C# | Thread(ThreadStart) Constructor
    Thread(ThreadStart) Constructor is used to initialize a new instance of a Thread class. This constructor will give ArgumentNullException if the value of the parameter is null. Syntax: public Thread(ThreadStart start); Here, ThreadStart is a delegate which represents a method to be invoked when this
    2 min read
  • How to Thread Lock Work in C#?
    C# makes the concurrent execution of a code possible with the help of threads. The namespace System. Threading which is pre-built in C# supports the use of threads. Typically we create threads for the concurrent execution of a program. But in certain cases we may not want our program to be run concu
    4 min read
  • C# Types of Threads
    Multithreading is one of the core features in C# that allows multiple tasks to be performed concurrently. Threads allow your application to run multiple tasks simultaneously, improving performance, and resource utilization. In C#, there are two types of threads. Foreground ThreadsBackground ThreadsN
    4 min read
  • How to Create Threads in C#?
    Multithreading enables concurrent task execution in C#. It improves performance and responsiveness. We can create threads using the System.Threading namespace. With the help of Threads, we can achieve multitasking and can define their behavior using different methods provided by the Thread Class. Ex
    6 min read
  • Task Parallel Library in C#
    In C#, Task Parallel Library (TPL) is a collection of APIs that provides more control over parallel and asynchronous programming. It is present in the System.Threading.Tasks namespace. TPL simplifies multithreading by managing thread scheduling and execution efficiently. It includes features like da
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences