Saturday, December 25, 2010

How to Set the Thread priority in C#.

You can set the thread priority after it is created using the priority property of the Thread class.
The following syntax shows how to set the thread priority:


NewThread.priority = ThreadPriority.Highest;


The syntax, the ThreadPriority.Highest property specifies the new priority setting for a thread. Threads are scheduled for execution based on their priority. Even through threads are executing within run time, all thread are assigned processor time slices by the operating system. The details of the scheduling algorithm used to determine the order in which threads are executed varies with each operating system.
If multiple threads with the same priority are available, the scheduler cycles through the threads in that priority, giving each thread a fixed time slice in which to execute. As long as a thread with a higher priority is available to run, lower priority threads do not get to execute. When there are no more runnable threads at a given priority, the scheduler moves to the next lower priority and schedules the threads at that priority for execution.

Example:
set the priority of the child threads:
using System;
using System.Threading;

class ThreadSchedule
{
    public static void ChildThread1()
    {
        Console.WriteLine("Child thread 1 started");

        Console.WriteLine("Child thread - counting from 1 to 10");
        for (int T = 1; T < 11; T++)
        {
            for (int Cnt = 0; Cnt < 100; Cnt++)
            {
                Console.Write(".");
                //code to imitate work begin done.
            }
            Console.Write("{0}", T);
        }
        Console.WriteLine("Child thread 1 finished ");
    }

    public static void ChildThread2()
    {
        Console.WriteLine("child thread 2 started");

        Console.WriteLine("Child thread - counting from 11 to 20");
        for (int T = 11; T < 20; T++)
        {
            for (int Cnt = 0; Cnt < 100; Cnt++)
            {
                Console.WriteLine(".");
                //Code to imitate work being done.
            }
            Console.WriteLine("{0}", T);
        }
        Console.WriteLine("Child thread 2 finished");
    }

    public static void Main()
    {
        ThreadStart Child1 = new ThreadStart(ChildThread1);
        ThreadStart Child2 = new ThreadStart(ChildThread2);

        Console.WriteLine("Main - Creating child threads");

        Thread Thread1 = new Thread(Child1);
        Thread Thread2 = new Thread(Child2);

        Thread1.Priority = ThreadPriority.Highest;
        Thread2.Priority = ThreadPriority.Lowest;

        Thread1.Start();
        Thread2.Start();
        Console.ReadLine();
    }
}

Output is display below:


Note: - When a processor is informed about the priority of a thread, the operating system eventually uses the information as a part of its scheduling algorithm. The processor then relays that information. In .Net, this algorithm is based on the priority level used with the Thread. Priority property.



What Other's are reading.
Identifying the Thread priority in console application.
Creating multiple threads in console Application.
Understanding Advantages and limitations of multithreading in Console Application.
How to Manage threads in c# Console Application.

1 comment:

Anonymous said...

i tried this for my code, lowest priority and highest priority gave the same results.

Recent Posts