Chapter 10. Details
Daemon Thread Groups
Calling Native Code
A Few Assorted Methods
The Effect of Using a JIT
APIs Used in this Chapter
The Class java.lang.Thread
The Class java.lang.ThreadGroup
In which a number of minor details are covered.
A thread group is a group of threads, or more precisely, a group of threads (possibly empty) and
other thread groups (also possibly empty). The raison d'Ítre for thread groups is security. Java
needs some method of allowing you to download untrusted foreign code and run it without it
being able to affect the rest of your program.
The idea was that you run the foreign code in its own thread group and tell that thread group that it
is not allowed to start, stop, suspend, etc., any threads outside that group. This way, your threads
are safe from the foreign code, but the foreign code is still able to create new threads of its own.
Unfortunately, they changed the security model for Java, and thread groups no longer provide this
kind of protection; rather, security is handled in a different fashion. So of what use are thread
groups now? You could use thread groups as a general container to keep track of your own threads,
but you'll probably find it easier simply to keep a list or an array of thread objects and manipulate
your threads with those. Because of the way MT programs are written, about the only thing you'll
ever do with a list of threads is to wait for them to exit, or to interrupt them. Thread groups don't
help you with this.
When you create a thread, it is placed in a thread group. You can specify a particular thread group
if you wish. That thread group can now restrict the new thread to creating threads only in that
group or subgroups. You can place a small number of restrictions on threads in a particular thread
group (set a maximum priority level), and you can call stop(), suspend(), resume(), or
interrupt() on all threads in a group, but none of these are particularly useful. You can also
do the usual set operations to find the parent of a group, the children, etc.
You will probably never use thread groups.
Search WWH :