获得当前所有的线程

来源:互联网 发布:cnabs数据库全称 编辑:程序博客网 时间:2024/06/05 04:23
public static Thread[] findAllThreads() {
  ThreadGroup group = Thread.currentThread().getThreadGroup();
  ThreadGroup topGroup = group;
  // 遍历线程组树,获取根线程组
  while (group != null) {
   topGroup = group;
   group = group.getParent();
  }
  // 激活的线程数加倍
  int estimatedSize = topGroup.activeCount() * 2;
  Thread[] slackList = new Thread[estimatedSize];
  // 获取根线程组的所有线程
  int actualSize = topGroup.enumerate(slackList);
  // copy into a list that is the exact size
  Thread[] list = new Thread[actualSize];
  System.arraycopy(slackList, 0, list, 0, actualSize);
  return list;
 }

0 0