Daemon Threads

Chapter 6 - Thread Prioritization

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Determining the Current Priority: getPriority()
The getPriority() method of Thread returns an int representing the current priority of the thread. Generally, a threads priority does not change over its lifetime, but it can. The GetPriority class shown in Listing 6.1 demonstrates the use of getPriority() .
Listing 6.1  GetPriority.javaUsing getPriority()
1: public class GetPriority 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:         System.out.println(
26:             in main() - Thread.currentThread().getPriority()= +
27:             Thread.currentThread(). getPriority ());
28:
29:         System.out.println(
30:             in main() - Thread.currentThread().getName()= +
31:             Thread.currentThread().getName());
32:
33:         Thread threadA = new Thread(makeRunnable(), threadA);
34:         threadA.start();
35:
36:         try { Thread.sleep(3000); }
37:         catch (InterruptedException x) { }
38:
39:         System.out.println(in main() - threadA.getPriority()= +
40:                 threadA. getPriority ());
41:     }
42: }
In main() , the name and priority of the thread running main() is printed (lines 2531). Next, makeRunnable() is used to create a new Runnable object to pass to the constructor of Thread (line 33). In addition, the name of this new thread is designated as threadA (line 33). Next, this thread is started (line 34) and is allowed to run for 3 seconds (line 36). After the sleep, getPriority() is used to determine the current priority of threadA (lines 3940).
The makeRunnable() method (lines 222) is used to create a new Runnable instance that prints the name and priority of the thread running it five times. Each time through the loop, the current thread is determined (line 6), and then the name and priority are extracted and printed (lines 710). Before looping again, the thread sleeps for two seconds (lines 1216).
Listing 6.2 shows the output produced when GetPriority is run. Your output should match.
Listing 6.2  Output from GetPriority
in main() - Thread.currentThread().getPriority()=5
in main() - Thread.currentThread().getName()=main
in run() - priority=5, name=threadA
in run() - priority=5, name=threadA
in main() - threadA.getPriority()=5
in run() - priority=5, name=threadA
in run() - priority=5, name=threadA
in run() - priority=5, name=threadA
Notice that the name of the thread running main() is main and that its priority is 5 . Also note that threadA inherited the priority of 5 from main .

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