THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
Equivalent to enumerate(threadsInGroup,true).

public intactiveGroupCount()

Like activeCount, but counts groups, instead of threads, in all subgroups.
"Active" means "existing." There is no concept of an inactive group; the term
"active" is used for consistency with activeCount.

public intenumerate(ThreadGroup[] groupsInGroup, boolean
recurse)

Like the similar enumerate method for threads, but fills an array of
ThreadGroup references instead of THRead references.

public intenumerate(ThreadGroup[] groupsInGroup)

Equivalent to enumerate(groupsInGroup,true).

You can also use a ThreadGroup to manage threads in the group. Invoking interrupt on a group
invokes the interrupt method on each thread in the group, including threads in all subgroups. This method
is the only way to use a ThreadGroup to directly affect threadsthere used to be others but they have been
deprecated.


There are also two static methods in the Thread class to act on the current thread's group. They are
shorthands for invoking currentThread, invoking getThreadGroup on that thread, and then invoking
the method on that group.


public static intactiveCount()

Returns the number of active threads in the current thread's ThreadGroup.

public static intenumerate(Thread[] threadsInGroup)

Equivalent to invoking the method enumerate(threadsInGroup) on
the THReadGroup of the current thread.

The ThreadGroup class also supports a method that is invoked when a thread dies because of an uncaught
exception:


public void uncaughtException(Thread thr, Throwable exc)


We'll look at this in more detail in the next section.


Exercise 14.9: Write a method that takes a thread group and starts a thread that periodically prints the
hierarchy of threads and thread groups within that group, using the methods just described. Test it with a
program that creates several short-lived threads in various groups.


14.12. Threads and Exceptions


Exceptions always occur in a specific thread, due to the actions of that threadfor example, trying to perform
division by zero, or explicitly throwing an exception. Such exceptions are synchronous exceptions and always

Free download pdf