Thread Priorities


Each thread has a priority setting associated with it. A thread’s priority determines, in part, how much CPU time a thread receives relative to other currently executing threads. In general, low-priority threads receive little CPU time. High-priority threads receive a lot. As you might expect, a thread’s priority setting profoundly affects its execution characteristics and its interaction with other threads currently executing in the system.

It is important to understand that factors other than a thread’s priority can also affect when a thread has access to the CPU. For example, if a high-priority thread is waiting on some resource, perhaps for keyboard input, it will be blocked, and a lower-priority thread will run. Thus, in this situation a low-priority thread may gain greater access to the CPU than the high-priority thread over a specific period.

When a child thread is started, it receives a default priority setting. You can change a thread’s priority through the Priority property, which is a member of Thread. This is its general form:

 public ThreadPriority Priority{ get; set; }

ThreadPriority is an enumeration that defines the following five priority settings:

 ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal ThreadPriority.BelowNormal ThreadPriority.Lowest

The default priority setting for a thread is ThreadPriority.Normal.

To understand how priorities affect thread execution, we will use an example that executes two threads, one having a higher priority than the other. The threads are created as instances of the MyThread class. The run( ) method contains a loop that counts the number of iterations. The loop stops when either the count reaches 1,000,000,000 or the static variable stop is true. Initially, stop is set to false. The first thread to count to 1,000,000,000 sets stop to true. This causes the second thread to terminate with its next timeslice. Each time through the loop, the string in currentName is checked against the name of the executing thread. If they don’t match, it means that a task-switch occurred. Each time a task-switch happens, the name of the new thread is displayed and currentName is given the name of the new thread. This allows you to watch how often each thread has access to the CPU. After both threads stop, the number of iterations for each loop is displayed.

 // Demonstrate thread priorities. using System; using System.Threading; class MyThread {   public int count;   public Thread thrd;   static bool stop = false;   static string currentName;   /* Construct a new thread. Notice that this      constructor does not actually start the      threads running. */   public MyThread(string name) {     count = 0;     thrd = new Thread(this.run);     thrd.Name = name;     currentName = name;   }   // Begin execution of new thread.   void run() {     Console.WriteLine(thrd.Name + " starting.");     do {       count++;       if(currentName != thrd.Name) {         currentName = thrd.Name;         Console.WriteLine("In " + currentName);       }     } while(stop == false && count < 1000000000);     stop = true;     Console.WriteLine(thrd.Name + " terminating.");   } } class PriorityDemo {   public static void Main() {     MyThread mt1 = new MyThread("High Priority");     MyThread mt2 = new MyThread("Low Priority");     // Set the priorities.     mt1.thrd.Priority = ThreadPriority.AboveNormal;     mt2.thrd.Priority = ThreadPriority.BelowNormal;     // Start the threads.     mt1.thrd.Start();     mt2.thrd.Start();     mt1.thrd.Join();     mt2.thrd.Join();     Console.WriteLine();     Console.WriteLine(mt1.thrd.Name + " thread counted to " +                       mt1.count);     Console.WriteLine(mt2.thrd.Name + " thread counted to " +                       mt2.count);   } }

Here is the output produced when the program was run on a 1-GHz Pentium-based computer under Windows XP:

 High Priority starting. In High Priority Low Priority starting. In Low Priority In High Priority In Low Priority In High Priority In Low Priority In High Priority In Low Priority In High Priority In Low Priority In High Priority High Priority terminating. Low Priority terminating. High Priority thread counted to 1000000000 Low Priority thread counted to 23996334

In this run, the high-priority thread got approximately 98 percent of the CPU time. Of course, the precise output you see may vary, depending upon the speed of your CPU and the number of other tasks running in the system. Which version of Windows you are running can even have an effect.

Because multithreaded code can behave differently in different environments, you should never base your code on the execution characteristics of a single environment. For example, in the preceding example, it would be a mistake to assume that the low-priority thread will always execute at least a small amount of time before the high-priority thread finishes. In a different environment, the high-priority thread might complete before the low-priority thread has executed even once, for example.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net