Other than some deprecated methods that we’ll examine in the next section, the methods of the ThreadGroup class are mostly informative. We’ll examine all the methods of the ThreadGroup class in this section.
There are often times when you’d like to call one of the thread group methods but don’t necessarily have a thread group object. The Thread class has a method that returns a reference to the thread group of a thread object:
Returns the ThreadGroup reference of a thread, for example:
// Find the thread group of the current thread. ThreadGroup tg = Thread.currentThread().getThreadGroup();
You can also retrieve the parent thread group of an existing thread
group with the
getParent() method of the
Returns the ThreadGroup reference of the parent of a thread group.
Finally, you can test whether a particular thread group is an
ancestor of another thread group with the
parentOf() method of the ThreadGroup class:
Returns true if the group
g is an ancestor of a
Note that the
parentOf() method is badly named;
it returns true if the group
g is the same as
the calling thread group, or the parent of the thread group, or the
grandparent of the thread group, and so on up the thread group
The next set of methods we’ll explore allows you to retrieve a list of all threads in a thread group. Enumeration ...