package thread.group;
public class SayThread extends Thread {
public SayThread(ThreadGroup group, String name) {
super(group, name);
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(getName() + " = " + i);
try {
sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println(getName() + " done");
}
}
package thread.group;
public class Test {
/**
* @author jzh add 2011-12-20
* @param args
*/
public static void main(String args[]) {
ThreadGroup group = new ThreadGroup("groupThread");
SayThread howskyThread = new SayThread(group, "jiang");
SayThread jzhThread = new SayThread(group, "jzh");
SayThread sayThread = new SayThread(group, "say");
howskyThread.start();
jzhThread.start();
sayThread.start();
System.out.println("the number of active threads = " + group.activeCount());
// Copies into the specified array every active thread in this thread group and its subgroups.
Thread[] list = new Thread[group.activeCount()];
group.enumerate(list);
for (Thread thread : list) {
System.out.println(thread.getName());
}
}
}