/**
* 在这里给出对类 ThreadList 的描述。
*
* @作者(你的名字)
* @版本(一个版本号或者一个日期)
*/
import java.lang.*;
import java.util.*;
public class ThreadList
{
private static ThreadGroup getRootThreadGroups(){
//获得当前线程组
ThreadGroup rootGroup=Thread.currentThread().getThreadGroup();
while(true){
if(rootGroup.getParent()!=null){
rootGroup=rootGroup.getParent(); //循环获取最终父线程
}else{
break;
}
}
return rootGroup;
}
public static List<String> getThreads(ThreadGroup group){
List<String> threadList=new ArrayList<String>();
Thread[]threads=new Thread[group.activeCount()];//根据活动线程数创建数组
int count=group.enumerate(threads,false);
for(int i=0;i<count;i++){
threadList.add(group.getName()+"线程组:"+threads[i].getName());
//循环获取线程名到数组
}
return threadList;
}
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){
for(String string:getThreadGroups(getRootThreadGroups())){
System.out.println(string);
}
}
}