Java並發-ThreadGroup獲取所有線程


一:獲取當前項目所有線程

 

 1 public Thread[] findAllThread(){
 2   ThreadGroup currentGroup =Thread.currentThread().getThreadGroup();
 3 
 4   while (currentGroup.getParent()!=null){
 5       // 返回此線程組的父線程組
 6       currentGroup=currentGroup.getParent();
 7   }
 8   //此線程組中活動線程的估計數
 9   int noThreads = currentGroup.activeCount();
10 
11   Thread[] lstThreads = new Thread[noThreads];
12   //把對此線程組中的所有活動子組的引用復制到指定數組中。
13   currentGroup.enumerate(lstThreads);
14 
15   for (Thread thread : lstThreads) {
16       System.out.println("線程數量:"+noThreads+" 線程id:" + thread.getId() + " 線程名稱:" + thread.getName() + " 線程狀態:" + thread.getState());
17   }
18   return lstThreads;
19 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM