日期:2014-05-20 浏览次数:20546 次
public class MachineThreadGroup extends Thread { public MachineThreadGroup(ThreadGroup group,String name) { super(group,name); } public void run() { for(int a=0;a<1000;a++){ System.out.println(Thread.currentThread().getName()+":"+a); yield(); } } public static void main(String[] args) throws Exception { ThreadGroup group=new ThreadGroup("machines"); for(int i=1;i<=5;i++){ MachineThreadGroup machine=new MachineThreadGroup(group,"machine"+i); machine.start(); } int activeCount = group.activeCount(); Thread[] machines=new Thread[activeCount]; group.enumerate(machines); for(int i=0;i<activeCount;i++){ System.out.println(machines[i].getName()+"is alive!"); } } }