|
|
< Day Day Up > |
|
9.2 Scheduling with Thread PrioritiesThe Thread class contains a number of methods and variables related to thread priorities: package java.lang;
public class Thread implements Runnable {
public static final int Thread.MAX_PRIORITY;
public static final int Thread.MIN_PRIORITY;
public static final int Thread.NORM_PRIORITY;
public void setPriority(int priority);
public int getPriority( );
}The setPriority() method changes the priority of a particular thread. This method can be called at any time (subject to security restrictions, which we discuss in Chapter 13). As we'll see later in this chapter, using priorities to give preference to certain threads may or may not give you the effect you expect. In general, attempting to influence scheduling behavior using priorities offers limited benefit. In the Java Thread class, three static final variables define the allowable range of thread priorities:
The symbolic definition of priority constants is not necessarily useful. Typically, we like to think of constant values like these in terms of symbolic names, which allows us to believe that the actual values are irrelevant. Using symbolic names also allows us to change the variables and have that change reflected throughout our code. Unfortunately, that logic doesn't apply in the case of thread priorities: if we have to manipulate the individual priorities of threads, we sometimes have to know what the range of those values actually is. Because of the way in which these values map to thread priorities of operating systems, threads with different Java priorities may end up with the same operating system priority. When you write an applet, the thread that the applet runs in is given a priority of NORM_PRIORITY + 1. It's interesting to wonder how far you can take this: NORM_PRIORITY + 2, + 3, and so on until you get to MAX_PRIORITY. If you really want to work with Java's full range of priorities, the symbolic values don't help you: you have to know that the minimum priority available to developers is 1, the maximum is 10, and the default is 5. This yields 10 distinct priorities that you can assign to a a thread; the 11th priority (priority 0) is reserved for the virtual machine. On the other hand, not all operating systems support 10 distinct levels of thread priorities, so NORM_PRIORITY - 2 and NORM_PRIORITY - 3 may be the same thing on your particular machine. Working with numeric values doesn't really provide a full range either. The best we can do for portable applications is to use the three symbolic priorities and realize that they're really just a hint to the virtual machine anyway. Let's see what happens when we use these calls. We'll modify our Fibonacci calculator so that each of the task threads is started with a different priority: for (int i = 0; i < t.length; i++) {
t[i] = new Thread(new Task(n, "Task " + i));
t[i].setPriority((i % 10) + 1);
t[i].start( );
}What happens when we run this program is very dependent on the operating system hosting the program. We'll discuss that effect for several popular platforms in the next section. 9.2.1 Other Thread-Scheduling MethodsOther methods in the Thread class also affect scheduling. For the most part, we do not recommend that you use these methods. The suspend() and resume() methods directly affect scheduling; a thread that is suspended is in the blocked state. However, as we discussed in Chapter 2, these methods are deprecated. The Thread class also includes a yield() method, which asks the host operating system to select another thread to run. Its effect is very dependent on the operating system hosting the virtual machine; much of the time, the yield() method turns out to be a no-op. On the green thread model (see the next section), the yield( ) method can be very useful, but as the Java platform has evolved to support native threads of an operating system, the yield() method has lost its value. |
|
|
< Day Day Up > |
|