Creating Thread Groups

There are two constructors that create new thread groups:

ThreadGroup(String name)

Creates a thread group with the given name.

ThreadGroup(ThreadGroup parent, String name)

Creates a thread group that descends from the given parent and has the given name.

In the case of the first constructor, the new thread group is a child of the current thread’s thread group; in the second case, the new thread group is inserted into the thread group hierarchy with the given thread group as its parent. (Though it’s probably bad design to do so, by default a thread group can be inserted anywhere in a Java application’s thread group hierarchy.) In Java 1.0, only Java applications were allowed to create thread groups; this restriction no longer applies.

Each of these constructors creates an empty thread group—a thread group with no threads. There is no method to move a thread into a particular group; a thread is placed into a group only when the thread object is created. As this restriction implies, there are some additional constructors for the Thread class that specify the thread group to which the thread should belong:

Thread(ThreadGroup group, String name)

Constructs a new thread that belongs to the given thread group and has the given name.

Thread(ThreadGroup group, Runnable target)

Constructs a new thread that belongs to the given thread group and runs the given target object.

Thread(ThreadGroup group, Runnable target, String name)

Constructs a new thread that belongs to ...

Get Java Threads, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.