Advertisement



< Prev
Next >



C# Threads Priorities




In C# language, each and every thread has a priority which, to an extent, determines how often it gets access to the CPU. In general, a low-priority thread will get less CPU time than a high-priority thread.

Now, we must also understand that how much CPU time a thread gets doesn't solely depend on its priority, but it also depends on the kind of operation it is executing.

For example, if a high-priority thread is waiting on some shared I/O resource to complete its task, it will be blocked and taken off the CPU, and at the same time, a lower-priority may get the CPU time and finish its execution, if its task doesn't require such resource. In such a scenario, a high-priority thread may get less CPU time than a low-priority thread over a specific period.

Another factor which determines how much CPU time is allocated to a thread is how task scheduling is implemented by the operating system.




How to set priority of a Thread.


When a thread is created, it gets a default priority setting. We can get or set a thread's priority by using the Priority property of the Thread. The Priority property can be assigned one of the fields of the ThreadPriority Enum, described in the table below:

Fields Values Description
Lowest 0
Threads with the Lowest prioroty can be scheduled after threads with any other superior priority.

BelowNormal 1
Threads with BelowNormal priority can be scheduled after thread with Normal priority and before threads with Lowest priority.

Normal 2
Threads with Normal priority can be scheduled after thread with AboveNormal priority and before threads with BelowNormal and Lowest priority.

AboveNormal 3
Threads with AboveNormal priority can be scheduled after thread with Highest priority and before threads with Normal, BelowNormal and Lowest priority.

Highest 4
Threads with Highest priority can be scheduled before thread with any other priority.



By default, each thread gets a default priority of 2 i.e. ThreadPriority.Normal




Setting and getting the priority of a Thread


Let us understand how to set and get the priorities of a Thread by an example, which uses the Priority property of the Thread class.

In the upcoming example, we are going to set the priority of our Thread within its entry-point method but, apparently we could set the priority of a thread anywhere it is accessible after it is created.

//C# - Program to get and set the priority of a thread

using System;
using System.Threading;

class ThreadEx
{
String threadName;

//Constructor of our class
public ThreadEx(String name)
{
	threadName = name;
}


//Entry Point of the new thread.
public void Run()	
{
	//Getting the reference to this thread
	//By calling the CurrentThread priority
	Thread t = Thread.CurrentThread;
	
	//Setting the name of our thread
	t.Name = threadName;

	//Getting the default priority of NewThread.
	Console.WriteLine("Default priority of "+ t.Name + " : " +  t.Priority);

	Console.WriteLine("Setting the priority of " + t.Name);

	//Setting the priority of NewThread to ThreadPriority.Highest
	t.Priority = ThreadPriority.Highest;

	//Getting the newly set priority of this thread
	Console.WriteLine("Newly set priority of " + t.Name + " : " + t.Priority);  
}


public static void Main(String[] ar)
{
	//Creating an object of our class
	ThreadEx th = new ThreadEx("My thread");
	
	//Creating an object of Thread class, and its constructor,
	//We are specifying the method that will be called to begin execution of our thread.
	Thread myThread = new Thread(th.Run);
	
	//Calling the Start() method, which automatically calls the entry-point method of thread
	myThread.Start(); 
}
}


Output is -:


Default priority of My thread : Normal
Setting the priority of My thread
Newly set priority of My thread : Highest





Program Analysis





Advertisement




Ideally, a high-priority thread gets more CPU time than a low-priority thread.


Let us see a simple example of how a high-priority thread gets more CPU time than a low-priority thread. Although you must also remember that this may not happen in all the cases, due to the factors which we have discussed at the beginning of this tutorial.

In the upcoming example, we are going to set the priority of our manually created Thread within the Main() method(right after it is created), which could usually be the case.

//C# - Program to get and set the priority of a thread

using System;
using System.Threading;

class ThreadEx
{
String threadName;

//Constructor of our class
public ThreadEx(String name)
{
	threadName = name;
}


//Entry Point of the new thread.
public void Run()	
{
	//Getting the reference to this thread
	//By calling the CurrentThread priority
	Thread t = Thread.CurrentThread;
	
	//Setting the name of our thread
	t.Name = threadName;

	int total=0;
	for(int i=0;i<100000000; i++)
	{	
		total = total +i;	
	} 
	Console.WriteLine(t.Name + " with priority " + t.Priority + " has finished its execution with total = "+ total); 
}


public static void Main(String[] ar)
{
	//Getting the reference to the Main thread.
	Thread t = Thread.CurrentThread;

	
	//Setting the name of the Main thread
	t.Name = "Main Thread";


	//Setting the priority of Main Thread to minimum priority i.e. ThreadPriority.Lowest
	t.Priority= ThreadPriority.Lowest;

	
	//Creating an object of our class
	ThreadEx th = new ThreadEx("My Thread");
	
	//Creating an object of Thread class,
	//And, specifying the method that will be called to begin execution of our thread.
	Thread myThread = new Thread(th.Run);
	

	//Setting the priority of our thead to Highest i.e. ThreadPriority.Highest
	myThread.Priority = ThreadPriority.Highest; 

	//Calling the Start() method, which automatically calls the entry-point method of our thread
	myThread.Start(); 


	int total=0;
	for(int i=0;i<100000000; i++)
	{	
		total = total +i;	
	}

	Console.WriteLine(t.Name + " with priority " + t.Priority + " has finished its execution with total = "+ total);
}
}


Output is -:


My Thread with priority Highest has finished its execution with total = 887459712
Main Thread with priority Lowest has finished its execution with total = 887459712


Program Analsis




However, you should never base an important outcome of your program solely on the basis of thread priorities because setting the thread with maximum priority is not a guarantee of maximum CPU time, for the reasons explained at the beginning of this tutorial.


Please share this article -




< Prev
Next >
< C# Creating Multiple Threads
C# Join() in Thread >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement