Depending upon the order and importance of the task assigned to threads in a multitasking environment , the priority levels of threads is determined. The thread having the higher priority is executed more frequently than one that has a lower priority.
priorities of a thread:
priorities of a thread:
Name
|
Description
|
Lowest
|
Refers to the last important priority level that can be assigned to a
thread. A thread with this priority can be scheduled after thread with any
other priority.
|
BelowNormal
|
Refers to the priority level
that is just below the normal level. The thread with this can be scheduled
after thread having the normal priority and before those with the lowest
priority.
|
Normal
|
Refers to the priority level that is neither lowest nor highest. The
thread with this priority level can be scheduled after thread with the
AboveNormal priority and before those with the BelowNormal priority. All the threads have the Normal priority by default.
|
AboveNoraml
|
Refer the priority level that is just below the highest level. The
thread with this priority can be scheduled after threads with the highest
priority and before those with the Normal priority.
|
Highest
|
Refers to the most important priority level that can be assigned to a
thread. The thread with this priority can be scheduled before thread with any
other priority.
|
Note : The thread priority is set to Normal , by default.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace thread_priprity.cs
{
class Program
{
static void Main(string[] args)
{
Thread firstthread = new Thread(firstfunction);
Thread secondthread = new Thread(secondfunction);
Thread thirdthread = new Thread(thirdfunction);
firstthread.Name = "first thread";
secondthread.Name = "second thread";
thirdthread.Name = "third thread";
// setting priorities for threads
firstthread.Priority = ThreadPriority.Lowest;
secondthread.Priority = ThreadPriority.Highest;
thirdthread.Priority = ThreadPriority.AboveNormal;
Console.WriteLine("thread starts..");
firstthread.Start();
secondthread.Start();
thirdthread.Start();
}
public static void firstfunction()
{
for (int number = 1; number <= 5; number++)
{
Console.WriteLine("first thread displays" + number);
Thread.Sleep(3000);
}
}
public static void secondfunction()
{
for (int number = 1; number <= 5; number++)
{
Console.WriteLine("second thread displays" + number);
Thread.Sleep(7000);
}
}
public static void thirdfunction()
{
for (int number = 1; number <= 5; number++)
{
Console.WriteLine("third thread displays" + number);
Thread.Sleep(10000);
} Console.ReadKey();
}
}
}
output :
No comments:
Post a Comment