在JVM中除了使用者建立的執行續,還有服務於使用者執行續的其他執行續,這些根據其不同的用途分配道不同組進行管理,以下範例將檢視JVM中的執行續名稱,以及其所歸屬的組名:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class ThreadList {

	private static ThreadGroup getRootThreadGroups() {
		ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
		
		while(rootGroup.getParent() != null)
			rootGroup = rootGroup.getParent();
		
		return rootGroup;
	}
	
	public static List<String> getThreads(ThreadGroup group){
		List<String> threaList = new ArrayList<String>();
		Thread[] threads = new Thread[group.activeCount()];
		int count = group.enumerate(threads,false);
		for(int i=0;i<count;i++) 
			threaList.add(group.getName() + " 執行續組 : " + threads[i].getName()); 
		
		return threaList;
	}

	public static List<String> getThreadGroups(ThreadGroup group){
		List<String> threadList = getThreads(group);
		ThreadGroup[] groups = new ThreadGroup[group.activeGroupCount()];
		int count = group.enumerate(groups,false);
		for(int i=0;i<count;i++)
			threadList.addAll(getThreads(groups[i]));
		return threadList;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(String string : getThreadGroups(getRootThreadGroups()))
			System.out.println(string);
	}

}

 

 

文章標籤
全站熱搜
創作者介紹
創作者 Lung-Yu,Tsai 的頭像
Lung-Yu,Tsai

Lung-Yu,Tsai 的部落格

Lung-Yu,Tsai 發表在 痞客邦 留言(0) 人氣(5)