Java并发-ThreadGroup获取所有线程

一:获取当前项目所有线程

 1 ThreadGroup currentGroup =Thread.currentThread().getThreadGroup();
 2 while (currentGroup.getParent()!=null){
 3     // 返回此线程组的父线程组
 4     currentGroup=currentGroup.getParent();
 5 }
 6 //此线程组中活动线程的估计数
 7 int noThreads = currentGroup.activeCount();
 8 
 9 Thread[] lstThreads = new Thread[noThreads];
10 //把对此线程组中的所有活动子组的引用复制到指定数组中。
11 currentGroup.enumerate(lstThreads);
12 
13 for (Thread thread : lstThreads) {
14     System.out.println("线程数量:"+noThreads+" 线程id:" + thread.getId() + " 线程名称:" + thread.getName() + " 线程状态:" + thread.getState());
15 }

猜你喜欢

转载自www.cnblogs.com/java-zzl/p/9567548.html