Java 获取当前所有的线程

来源:互联网 发布:淘宝页头150px图片 编辑:程序博客网 时间:2024/05/18 18:04
  1. ThreadGroup group = Thread.currentThread().getThreadGroup();  
  2. ThreadGroup topGroup = group;  
  3. // 遍历线程组树,获取根线程组  
  4. while (group != null) {  
  5.     topGroup = group;  
  6.     group = group.getParent();  
  7. }  
  8. // 激活的线程数加倍  
  9. int estimatedSize = topGroup.activeCount() * 2;  
  10. Thread[] slackList = new Thread[estimatedSize];  
  11. // 获取根线程组的所有线程  
  12. int actualSize = topGroup.enumerate(slackList);  
  13. // copy into a list that is the exact size  
  14. Thread[] list = new Thread[actualSize];  
  15. System.arraycopy(slackList, 0, list, 0, actualSize);  
  16. System.out.println("Thread list size == " + list.length);  
  17. for (Thread thread : list) {  
  18.     System.out.println(thread.getName());  
  19. }  
0 0
原创粉丝点击