Java Reference
In-Depth Information
same thread group as that of the thread that created it, unless a secur-
ity manager specifies differently. For example, if some event-handling
code in an applet creates a new thread, the security manager can make
the new thread part of the applet thread group rather than the system
thread group of the event-processing thread. When a thread terminates,
the Thread object is removed from its group and so may be garbage col-
lected if no other references to it remain.
There are four Thread constructors that allow you to specify the
ThreadGroup of the thread. You saw three of them on page 343 when
looking at Runnable . The fourth is:
public Thread(ThreadGroup group, String name)
Constructs a new thread in the specified thread group with
the given name.
To prevent threads from being created in arbitrary thread groups (which
would defeat the security mechanism), these constructors can them-
selves throw SecurityException if the current thread is not permitted to
place a thread in the specified thread group.
The ThreadGroup object associated with a thread cannot be changed after
the thread is created. You can get a thread's group by invoking its
getThreadGroup method. You can also check whether you are allowed to
modify a THRead by invoking its checkAccess method, which throws Secur-
ityException if you cannot modify the thread and simply returns if you
can (it is a void method).
A thread group can be a daemon group which is totally unrelated to the
concept of daemon threads. A daemon THReadGroup is automatically des-
troyed 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 be-
comes 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 maxim-
 
Search WWH ::




Custom Search