Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 8
Active thread groups in main thread group: 2
java.lang.ThreadGroup[name=main,maxpri=10]
Thread[main,5,main]
Thread[Thread-0,5,main]
java.lang.ThreadGroup[name=subgroup 1,maxpri=10]
Thread[thread 1,5,subgroup 1]
Thread[thread 2,5,subgroup 1]
Thread[thread 3,5,subgroup 1]
java.lang.ThreadGroup[name=subgroup 2,maxpri=10]
Thread[my thread,5,subgroup 2]
Output that begins with Thread results from list()'s internal calls to Thread's toString() method, an output format I described in Part 1. Along with that output, you see output beginning with java.lang.ThreadGroup. That output identifies the thread group's name followed by its maximum priority.
A thread group's maximum priority is the highest priority any of its threads can attain. Consider the aforementioned network server program. Within that program, a thread waits for and accepts requests from client programs. Before doing that, the wait-for/accept-request thread might first create a thread group with a maximum priority just below that thread's priority. Later, when a request arrives, the wait-for/accept-request thread creates a new thread to respond to the client request and adds the new thread to the previously created thread group. The new thread's priority automatically lowers to the thread group's maximum. That way, the wait-for/accept-request thread responds more often to requests because it runs more often.
Java assigns a maximum priority to each thread group. When you create a group, Java obtains that priority from its parent
group. Use ThreadGroup's void setMaxPriority(int priority) method to subsequently set the maximum priority. Any threads that you add to the group after setting its maximum priority
cannot have a priority that exceeds the maximum. Any thread with a higher priority automatically lowers when it joins the
thread group. However, if you use setMaxPriority(int priority) to lower a group's maximum priority, all threads added to the group prior to that method call keep their original priorities.
For example, if you add a priority 8 thread to a maximum priority 9 group, and then lower that group's maximum priority to
7, the priority 8 thread remains at priority 8. At any time, you can determine a thread group's maximum priority by calling
ThreadGroup's int getMaxPriority() method. To demonstrate priority and thread groups, I wrote MaxPriorityDemo:
// MaxPriorityDemo.java
class MaxPriorityDemo
{
public static void main (String [] args)
{
ThreadGroup tg = new ThreadGroup ("A");
System.out.println ("tg maximum priority = " + tg.getMaxPriority ());
Thread t1 = new Thread (tg, "X");
System.out.println ("t1 priority = " + t1.getPriority ());
t1.setPriority (Thread.NORM_PRIORITY + 1);
System.out.println ("t1 priority after setPriority() = " +
t1.getPriority ());
tg.setMaxPriority (Thread.NORM_PRIORITY - 1);
System.out.println ("tg maximum priority after setMaxPriority() = " +
tg.getMaxPriority ());
System.out.println ("t1 priority after setMaxPriority() = " +
t1.getPriority ());
Thread t2 = new Thread (tg, "Y");
System.out.println ("t2 priority = " + t2.getPriority ());
t2.setPriority (Thread.NORM_PRIORITY);
System.out.println ("t2 priority after setPriority() = " +
t2.getPriority ());
}
}
When run, MaxPriorityDemo produces the following output: