Summary

Chapter 6 - Thread Prioritization

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Changing the Priority of a Thread: setPriority()
The setPriority() method of Thread takes an int as a parameter indicating the new priority for the thread. The setPriority() method can be invoked either before the thread is started or once it is running. Some VMs might allow you to change the priority of the system threads, but this is not recommendedeven if it is permitted.
The class SetPriority , shown in Listing 6.3, demonstrates how setPriority() can be used to specify a priority of a thread before it is started. It also shows how the priority of a running thread can be changed on-the-fly .
Listing 6.3  SetPriority.javaChanging the Priority of a Thread with setPriority()
1: public class SetPriority extends Object {
2:     private static Runnable makeRunnable() {
3:         Runnable r =  new Runnable() {
4:                 public void run() {
5:                     for (int i = 0; i < 5; i++) {
6:                         Thread t = Thread.currentThread();
7:                         System.out.println(
8:                             in run() - priority= +
9:                             t.getPriority() +
10:                             , name = + t.getName());
11:
12:                         try {
13:                             Thread.sleep(2000);
14:                         } catch (InterruptedException x) {
15:                             // ignore
16:                         }
17:                     }
18:                 }
19:             };
20:
21:         return r;
22:     }
23:
24:     public static void main(String[] args) {
25:         Thread threadA = new Thread(makeRunnable(), threadA);
26:         threadA.setPriority(8);
27:         threadA.start();
28:
29:         Thread threadB = new Thread(makeRunnable(), threadB);
30:         threadB.setPriority(2);
31:         threadB.start();
32:
33:         Runnable r = new Runnable() {
34:                 public void run() {
35:                     Thread threadC =
36:                         new Thread(makeRunnable(), threadC);
37:                     threadC.start();
38:                 }
39:             };
40:         Thread threadD = new Thread(r, threadD);
41:         threadD.setPriority(7);
42:         threadD.start();
43:
44:         try { Thread.sleep(3000); }
45:         catch (InterruptedException x) { }
46:
47:         threadA.setPriority(3);
48:         System.out.println(in main() - threadA.getPriority()= +
49:                 threadA.getPriority());
50:     }
51: }
The makeRunnable() method of SetPriority works the same as it did in GetPriority . This time several new threads are spawned inside main() . First, threadA is created and has its priority set to 8 before it is started (lines 2527). Next, threadB is created and has its priority set to 2 before it is started (lines 2931).
To demonstrate how a thread priority is inherited, threadD creates threadC (lines 3342). The initial priority for threadD is set to 7 before it is started (line 41). When threadD runs, it executes the run() method inside r (lines 3438). A new Thread is constructed by threadD and is called threadC . The priority of threadC is not changed, but is simply inherited from threadC s creator.
After launching the threads, the main thread sleeps for three seconds. When it wakes up, it invokes setPriority() on the already running thread threadA to change its priority to 3 .
Listing 6.4 shows the output produced when SetPriority is run. Your output should be very similar, but might differ slightly.
Listing 6.4  Possible Output from SetPriority
1: in run() - priority=8, name=threadA
2: in run() - priority=2, name=threadB
3: in run() - priority=7, name=threadC
4: in run() - priority=8, name=threadA
5: in run() - priority=2, name=threadB
6: in run() - priority=7, name=threadC
7: in main() - threadA.getPriority()=3
8: in run() - priority=3, name=threadA
9: in run() - priority=2, name=threadB
10: in run() - priority=7, name=threadC
11: in run() - priority=3, name=threadA
12: in run() - priority=2, name=threadB
13: in run() - priority=7, name=threadC
14: in run() - priority=3, name=threadA
15: in run() - priority=2, name=threadB
16: in run() - priority=7, name=threadC
While examining the output from SetPriority , you can see that threadC is running with a priority of 7 (lines 3, 6, 10, 13, and 16). It was never directly set to 7 , but was defaulted to that level when it was created by threadD . Also note that when the priority of threadA was reduced to 3 from 8 , threadA printed the new priority the last three times through its for loop (lines 8, 11, and 14).
When setPriority() Might Not Work
Calls to setPriority can throw a SecurityException if the calling thread is not permitted to make the priority change. This will happen only if there is a SecurityManager installed and it rejects the change request. By default, applications do not have a SecurityManager .
An IllegalArgumentException will be thrown if the specified priority is greater than Thread.MAX_PRIORITY or less than Thread.MIN_PRIORITY. allowed for the thread group . For example, in this code fragment
Thread t = //...
ThreadGroup tg = t.getThreadGroup();
int groupMax = tg.getMaxPriority();
the maximum value for the thread priority of thread t is groupMax . Usually, this is the same as Thread.MAX_PRIORITY . When groupMax is less than Thread.MAX_PRIORITY , calls to setPriority() still work, but will silently reduce the value passed to groupMax . If groupMax is 6 and setPriority(9) is called, the code will run as if setPriority(6) was called instead. See Chapter 10, Thread Groups, for more on ThreadGroup .
  Caution The Java 1.1 VM inside Netscapes Navigator/Communicator (versions 4.0 through 4.5) silently ignores all calls to setPriority() . Even calls that would lower the priority of a thread are ignored! No exception is thrown, but the thread priority remains unchanged. Netscape implements an elaborate security scheme that requires a bunch of code to tell the VM to allow thread priorities to be changed. Future versions of Netscape might change this policy, but beware of the potential problems that applets might have running in this version of the browser. If thread prioritization is important in your applet, consider requiring the Java plug-in that Sun Microsystems supplies for free.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

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