Here are the methods of the ThreadGroup class that we introduced in this chapter:
Creates a thread group with the given name.
Creates a thread group that descends from the given parent and has the given name.
Suspends all threads that descend from this thread group.
Resumes all threads that descend from this thread group.
Stops all threads that descend from this thread group.
Cleans up the thread group and removes it from the thread group hierarchy.
Interrupts all threads that descend from this thread group.
Returns the ThreadGroup reference of the parent of a thread group.
Returns true if the group g
is an ancestor of a
thread group.
Fills in the list array with a reference to all threads in this thread group and all threads that are in groups that descend from this thread group.
Fills in the list
array with a reference to all
threads in this thread group and, if recurse
is
true, all threads that are in groups that descend from this thread
group.
Returns the number of active threads in this and all descending thread groups.
Retrieves all thread group references that are descendants of the given thread group. This method operates recursively on the thread group hierarchy.
Retrieves all thread group references that are immediate descendants
of the given thread group and, if recurse
is true,
all descendants of the current thread group.
Retrieves all thread group references that are descendants of the given thread group. This method operates recursively on the thread group hierarchy.
Retrieves all thread group references that are immediate descendants of the given thread group and, if recurse is true, all descendants of the current thread group.
Returns the number of thread group descendants (at any level) of the given thread group.
Sets the maximum priority for the thread group.
Retrieves the maximum priority for the thread group.
Changes the daemon status of the thread group.
Returns
true
if the thread group is a daemon group.
Returns a flag indicating whether the thread group has been destroyed.
Returns the name of the thread group.
Sends a list of all the threads in the current thread group to standard out.
Sets the vmAllowSuspension
flag of the thread
group, returning the old value. When the virtual machine runs low on
memory, some implementations of the virtual machine will seek to
obtain memory by suspending threads in thread groups for which the
vmAllowSuspension
flag is set to
true
.
This method is called when a thread exits due to an uncaught
exception; its default behavior is to print the stack trace of the
thread to System.err
.
In addition, we introduced these new methods of the Thread class:
Constructs a new thread that belongs to the given thread group and has the given name.
Constructs a new thread that belongs to the given thread group and runs the given target object.
Constructs a new thread that belongs to the given thread group, runs the given target object, and has the given name.
Returns the ThreadGroup reference of a thread.
Finally, we introduced these methods of the SecurityManager class that operate on threads:
Checks if the current thread is allowed to modify the state of the
thread t
.
Checks if the current thread group is allowed to modify the state of
the thread group tg
.
In this chapter, we filled in the final piece of Java’s thread mechanism: a way to group threads together and operate on all threads within the group. Additionally, the ThreadGroup class forms a thread hierarchy on which security policies for Java’s thread mechanism are based.
Like the other topics in the last few chapters, the ThreadGroup class is not one that is needed by the majority of programs; it’s a special-use class for cases in which you need additional control over groups of threads. The ThreadGroup class is the last of the special-use mechanisms you need in order to complete your understanding of using threads in Java. Although we present some informative miscellaneous topics in the appendixes, the information we’ve presented in the body of this book should allow you to write productive and, if need be, very complex threaded programs in Java.