|
|
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 5 of 8
A about to wait.
B about to wait.
A interrupted.
A terminating.
B interrupted.
B terminating.
A is interrupted and terminates before B is interrupted and terminates.
You'll occasionally want to enumerate all threads or subgroups that comprise a thread group. The next section explores that activity.
Part 1 introduced you to Thread's activeCount() and enumerate(Thread [] thdarray) methods. activeCount() calls ThreadGroup's int activeCount() method via the current thread's group reference to return an estimate of the active threads in the current thread's group
and subgroups. enumerate(Thread [] thdarray) calls ThreadGroup's int enumerate(Thread [] thdarray) method via the current thread's group reference. enumerate(Thread [] thdarray) is just one of four enumeration methods in ThreadGroup:
int enumerate(Thread [] thdarray) copies into thdarray references to every active thread in the current thread group and all subgroups.
int enumerate(Thread [] thdarray, boolean recurse) copies into thdarray references to every active thread in the current thread group only if recurse is false. Otherwise, this method includes active threads from subgroups.
int enumerate(ThreadGroup [] tgarray) copies into tgarray references to every active subgroup in the current thread group.
int enumerate(ThreadGroup [] tgarray, boolean recurse) copies into tgarray references to every active subgroup in the current thread group only if recurse is false. Otherwise, this method includes all active subgroups of active subgroups, active subgroups of active subgroups
of active subgroups, and so on.
You can use ThreadGroup's activeCount and enumerate(Thread [] thdarray) methods to enumerate all program threads. First, you find the system thread group. Then you call ThreadGroup's activeCount() method to retrieve an active thread count for array-sizing purposes. Next, you call ThreadGroup's enumerate(Thread [] thdarray) method to populate that array with Thread references, as Listing 4 demonstrates:
// EnumThreads.java
class EnumThreads
{
public static void main (String [] args)
{
// Find system thread group
ThreadGroup system = null;
ThreadGroup tg = Thread.currentThread ().getThreadGroup ();
while (tg != null)
{
system = tg;
tg = tg.getParent ();
}
// Display a list of all system and application threads, and their
// daemon status
if (system != null)
{
Thread [] thds = new Thread [system.activeCount ()];
int nthds = system.enumerate (thds);
for (int i = 0; i < nthds; i++)
System.out.println (thds [i] + " " + thds [i].isDaemon ());
}
}
}
When run, EnumThreads produces the following output (on my platform):
Thread[Reference Handler,10,system] true
Thread[Finalizer,8,system] true
Thread[Signal Dispatcher,10,system] true
Thread[CompileThread0,10,system] true
Thread[main,5,main] false
Apart from a main thread, all other threads belong to the system thread group.
Tip: You can easily determine a thread group's parent group by calling ThreadGroup's ThreadGroup getParent() method. For all thread groups, save system, this method returns a nonnull reference. For system, this method returns null. You can also find out if a thread group is the parent, grandparent, and so forth of another thread
group by calling ThreadGroup's boolean parentOf(ThreadGroup tg) method. That method returns true if a thread, whose reference you use to call parentOf(ThreadGroup tg), is a parent (or other ancestor) of the group that tg references—or is the same group as the tg-referenced group. Otherwise, the method returns false.