C# thread priority – detail whatever you know

Every C# thread has a priority. Thread priorities in C# are available in ThreadPriority class from namespace System.Threading; Below C# thread priority can be assigned to a thread.

  • Highest
  • AboveNormal
  • Normal
  • BelowNormal
  • Lowest

By default “Normal” thread priority is assigned to a thread in C#.

Thread scheduler of operating system schedules all threads according to their thread priority for execution, but with no guarantee. Operating system may or may not respect the priority of thread. Means, it is not necessary that highest priority thread will always execute first and lower priority thread will execute last. Lowest priority thread will run slower, but not completely stop.

Note that operating system do not ignore priority completely and always try to complete high priority task first.

If we execute the below software program – C# thread priority example, you should find completion of highest priority thread first most of the time.

Program for thread Priority in C#

To check the desired result i.e. high priority thread complete its task first and low priority threads complete at later on the basis of above concepts, we will consider three threads performing the same amount of task.

Also, to notice the desired result, we need to give long task to threads as for smaller task we will not be able to see execution of thread in priory order.

Here we have given highest priority to third thread and lowest priority to first one. In the result most of the time you would see the completion for the third thread first and first one to last.

using System;
using System.Threading;


namespace threadPriority
{
    class Program
    { 

        static void Main(string[] args)
        {
            //Create threads
            Thread t1 = new Thread(function1st);
            Thread t2 = new Thread(function2nd);
            Thread t3 = new Thread(function3rd);
           

            //Set priorities to all threads
            t1.Priority = ThreadPriority.Lowest;
            t2.Priority = ThreadPriority.Normal;
            t3.Priority = ThreadPriority.Highest;
                       
            t1.Start();
            t2.Start();
            t3.Start();
           
        }

        static void function1st()
        {

            task();
            Console.WriteLine("Thread 1st completed");
            
        }
        static void function2nd()
        {

            task();
            Console.WriteLine("Thread 2nd completed");
        }
        static void function3rd()
        {
            task();
            Console.WriteLine("Thread 3rd completed");
        }
        
        //All thread will perform the same task.
        static void task()
        {
            //perform some task
            for (int i = 0; i < 1000; i++)
            {
                Console.Write("Value:\t" + i);
            }
        }
    }
}


Conclusion:

  • Thread scheduler of operating system schedule threads with no guarantee but tries to consider them.
  • For long running task threads get the benefit of priority setting.

Related Posts