A thread group can be a daemon groupwhich is totally unrelated to the concept of daemon threads. A daemon
THReadGroup is automatically destroyed when it becomes empty. Setting a ThreadGroup to be a
daemon group does not affect whether any thread or group contained in that group is a daemon. It affects only
what happens when the group becomes empty.
Thread groups can also be used to set an upper limit on the priority of the threads they contain. After you
invoke setMaxPriority with a maximum priority, any attempt to set a thread priority higher than the
thread group's maximum is silently reduced to that maximum. Existing threads are not affected by this
invocation. To ensure that no other thread in the group will ever have a higher priority than that of a particular
thread, you can set that thread's priority and then set the group's maximum priority to be less than that. The
limit also applies to the thread group itself. Any attempt to set a new maximum priority for the group that is
higher than the current maximum will be silently reduced.
static synchronized void
maxThread(Thread thr, int priority)
{
ThreadGroup grp = thr.getThreadGroup();
thr.setPriority(priority);
grp.setMaxPriority(thr.getPriority() - 1);
}
This method works by setting the thread's priority to the desired value and then setting the group's maximum
allowable priority to less than the thread's priority. The new group maximum is set to one less than the
thread's actual prioritynot to priority- 1 because an existing group maximum might limit your ability to
set the thread to that priority. Of course, this method assumes that no thread in the group already has a higher
priority.
ThreadGroup supports the following constructors and methods:
publicThreadGroup(String name)
Creates a new ThreadGroup. Its parent will be the ThreadGroup of the
current thread. Like Thread names, the name of a group is not used by the
runtime systembut it can be null.
publicThreadGroup(ThreadGroup parent, String name)
Creates a new THReadGroup with a specified name in the
ThreadGroupparent. A NullPointerException is thrown if
parent is null.
public final StringgetName()
Returns the name of this ThreadGroup.
public final ThreadGroupgetParent()
Returns the parent ThreadGroup, or null if it has none (which can only
occur for the top-level thread group).
public final voidsetDaemon(boolean daemon)
Sets the daemon status of this thread group.