O'Reilly logo

Java Threads, Second Edition by Henry Wong, Scott Oaks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Thread Group Methods

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.

Finding Thread Groups

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:

ThreadGroup getThreadGroup()

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 ThreadGroup class:

ThreadGroup getParent()

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:

boolean parentOf(ThreadGroup g)

Returns true if the group g is an ancestor of a thread group.

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 hierarchy.

Enumerating Thread Groups

The next set of methods we’ll explore allows you to retrieve a list of all threads in a thread group. Enumeration ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required