Threading Simplified: Part 7 (Thread Priority)

I am here to continue the discussion around Threading. Today we will discuss Thread Priority and related concepts.

In case you haven't seen my previous posts, you can go through the following:

    Let’s start by posing some basic questions to understand the concepts.

    Why we need Thread Priority

    Well, this is not required in common cases; however, there are a few cases when you may want to elevate priority of some threads. One such example could be when you want certain talk to be completed first over others.

    There are basically five types of thread priority:

    • Highest
    • AboveNormal
    • Normal
    • BelowNormal
    • Lowest

    Let’s take a simple example to understand better.

    1. static void DoWork1()   
    2. {  
    3.     for (int i = 0; i < 5; i++)  
    4.     {  
    5.         Console.WriteLine("DoWork1: " + i);  
    6.     }  
    7. }  
    8.   
    9. static void DoWork2() {  
    10.     for (inti = 0; i < 5; i++)  
    11.     {  
    12.         Console.WriteLine("DoWork2: " + i);  
    13.     }  
    14. }  
    15.   
    16. static void Main(string[] args)  
    17. {  
    18.     Console.Title = "Threading Priority Demo";  
    19.   
    20.     Thread thread1 = new Thread(new ThreadStart(DoWork1));  
    21.     Thread thread2 = new Thread(new ThreadStart(DoWork2));  
    22.   
    23.     thread1.Priority = ThreadPriority.Highest;  
    24.     thread2.Priority = ThreadPriority.Lowest;  
    25.   
    26.     thread2.Start();  
    27.     thread1.Start();  
    28. }  
    Output:

    Output

    You can see in above output that DoWork1 is getting executed first despite that thread2 or DoWork2 is begun first.

    Why So?

    It is because in the code we have set the thread1 priority as highest and thread2 priority as lowest.
    Points to remember: 
    • Setting any thread priority as highest doesn’t guarantee that in all the cases the prioritized thread will be executed first and other threads will keep waiting. Due to context switching and other underlying factors, sometimes it may not work as expected.

      For example, sometimes you may see in the result that DoWork2 is executed before completion of every DoWork1, but in most cases DoWork1 will lead the execution.

    • We need to be extra careful when playing with thread priority as not doing that properly can lead to thread starvation.

    • Setting the thread priority to highest doesn’t equate to real-time execution as a thread’s priority will always depends on its parent container or process priority.

    • To set Process Priority, we need to set ProcessPriorityClassenum in the current process object as in the following.
      1. Process prc = Process.GetCurrentProcess();  
      2. prc.PriorityClass = ProcessPriorityClass.High;  
    • Please be careful when setting process priority as ‘RealTime;’ it means that OS will keep busy only in the process whose set priority is RealTime.

      Hope you liked the article. Looking forward to your comments/suggestions.

    Up Next
      Ebook Download
      View all
      Learn
      View all