?? threadgroupdemo.java
字號:
public class ThreadGroupDemo extends Thread{
public static void 創(chuàng)建線程組(){
ThreadGroup group = new ThreadGroup("函數(shù)創(chuàng)建的線程組");
System.out.println("函數(shù)中創(chuàng)建的線程組的父線程組:"+
group.getParent().getName());
}
public static void 創(chuàng)建線程(){
Runnable r = new Runnable(){
public void run(){
try{
創(chuàng)建線程組();
Thread.sleep(1000*10);
}catch(Exception e){}
System.out.println("線程"+ Thread.currentThread().getName() + "屬于 " +
Thread.currentThread().getThreadGroup().getName() + " 執(zhí)行完畢");
}
};
Thread t = new Thread(r);
t.start();
}
public static void main(String arg[]) throws Exception{
ThreadGroup group = Thread.currentThread().getThreadGroup();
System.out.println("當(dāng)前線程組:"+ group.getName());
ThreadGroup group1 = new ThreadGroup("我的線程組");
Runnable r = new Runnable(){
public void run(){
try{
創(chuàng)建線程();
Thread.sleep(1000*10);
}catch(Exception e){}
System.out.println("線程"+ Thread.currentThread().getName() + "屬于 " +
Thread.currentThread().getThreadGroup().getName() + " 執(zhí)行完畢");
}
};
Thread 線程1 = new Thread(r,"線程1");
Thread 線程2 = new Thread(group1,r,"線程2");
線程1.start();
線程2.start();
Thread.sleep(1000*1);
System.out.println("num:"+group.activeCount());
Thread 線程組[] = new Thread[group.activeCount()];
int threadNum = group.enumerate(線程組);
System.out.println("\n當(dāng)前線程組:"+ group.getName() + "下有以下線程\n");
for(int i=0; i< threadNum; i++)
System.out.println(線程組[i].getName());
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -