When invokeAndWait() and invokeLater() Are Not Needed

Chapter 10 - Thread Groups

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

What Are Thread Groups?
In Java, all threads belong to an instance of the ThreadGroup class. A thread group has a name and some properties associated with it and can be used to facilitate the management of threads within it as a group. Thread groups allow the threads of the VM to be organized and can provide some inter-group security. A ThreadGroup can contain other ThreadGroup s. Figure 10.1 shows a sample containment tree for Thread and ThreadGroup objects. There is only one root thread group, and it contains all the other threads and groups. Each subgroup can contain other groups and threads. All threads belong to exactly one thread group. All thread groups (except for the root thread group) have exactly one parent thread group.
Figure 10.1: An example containment hierarchy for threads and thread groups.
When a new ThreadGroup is constructed , it is added as a member of an existing ThreadGroup . If threadX is in the groupA thread group and it executes the code
ThreadGroup groupB = new ThreadGroup(groupB);
the newly formed thread group groupB has groupA as its parent group. If you want a parent thread group other than the default, you can specify the parent thread group at the time of construction. If threadX is in groupA and it executes the code
ThreadGroup groupD = new ThreadGroup(groupC, groupD);
the newly formed groupD has groupC as its parent thread group, not groupA .
Like Thread , instances of ThreadGroup have names associated with them. Unlike Thread , a name must be specifiedthere is no option for names to be automatically generated. If threadX executes the code
ThreadGroup group = new ThreadGroup(comm subsystem);
a new ThreadGroup is created and named comm subsystem . To retrieve the name of a particular group, the getName() method is used. If you run the code
String groupName = group.getName();
groupName is the name passed to the constructor when group is instantiated : that is, comm ubsystem .
New Thread objects default to be in the same thread group as the thread that does the construction. If threadX is in groupA and it executes the code
Thread threadY = new Thread(target);
threadY is in groupA , just like the thread ( threadX ) that constructed it. If a new thread should be in another group, the desired thread group can be passed to the constructor for Thread . If threadX is in groupA and it executes the code
Thread threadZ = new Thread(groupC, target);
threadZ is a member of groupC , not groupA .

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net