stop all threads related to printing. Thread groups offer this convenience. The following
program, which creates two thread groups of two threads each, illustrates this usage:
// Demonstrate thread groups.
class NewThread extends Thread {
boolean suspendFlag;
NewThread(String threadname, ThreadGroup tgOb) {
super(tgOb, threadname);
System.out.println("New thread: " + this);
suspendFlag = false;
start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(getName() + ": " + i);
Thread.sleep(1000);
synchronized(this) {
while(suspendFlag) {
wait();
}
}
}
} catch (Exception e) {
System.out.println("Exception in " + getName());
}
426 Part II: The Java Library
Method Description
final boolean isDaemon( ) Returnstrueif the group is a daemon group.
Other wise, it returnsfalse.
boolean isDestroyed( ) Returnstrueif the group has been destroyed.
Other wise, it returnsfalse.
void list( ) Displays information about the group.
final boolean parentOf(ThreadGroupgroup) Returnstrueif the invoking thread is the parent of
group(orgroup,itself). Other wise, it returnsfalse.
final void setDaemon(booleanisDaemon) IfisDaemonistrue, then the invoking group is
flagged as a daemon group.
final void setMaxPriority(intpriority) Sets the maximum priority of the invoking group
topriority.
String toString( ) Returns the string equivalent of the group.
void uncaughtException(Threadthread,
Throwablee)
This method is called when an exception goes
uncaught.
TABLE 16-18 The Methods Defined byThreadGroup(continued)