Advertisement
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. |
//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();
}
}
Default priority of My thread : Normal
Setting the priority of My thread
Newly set priority of My thread : Highest
Advertisement
//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);
}
}
My Thread with priority Highest has finished its execution with total = 887459712
Main Thread with priority Lowest has finished its execution with total = 887459712
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement