java获取当前所有线程

public class Main extends Thread {
   public static void main(String[] args) {
      Main t1 = new Main();
      t1.setName("thread1");
      t1.start();
      ThreadGroup currentGroup = 
      Thread.currentThread().getThreadGroup();
      int noThreads = currentGroup.activeCount();
      Thread[] lstThreads = new Thread[noThreads];
      currentGroup.enumerate(lstThreads);
      for (int i = 0; i < noThreads; i++)
      System.out.println("线程号:" + i + " = " + lstThreads[i].getName());
   }
}

以上代码运行输出结果为:

线程号:0 = main 线程号:1 = thread1

你可能感兴趣的:(java,线程)