List all Java threads that are running

public class Main
   public static void main(String []args) {
      // start up a couple thread belonging to some threadgroups
      ThreadGroup myGroup1 = new ThreadGroup("My ThreadGroup #1");
      new MyThread(myGroup1, "My Thread #1").start();
      new MyThread(myGroup1, "My Thread #2").start();
      ThreadGroup myGroup2 = new ThreadGroup(myGroup1, "My ThreadGroup #2");
      new MyThread(myGroup2, "My Thread #3").start();
      ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
      while (root.getParent() != null) {
         root = root.getParent();
      printThreads_aux(root, 0);
   public static void printThreads_aux(ThreadGroup group, int indent) {
      indentPrint(indent, group);
      // An application should use the activeCount method to 
      // get an estimate of how big the array should be
      Thread[] threads = new Thread[group.activeCount()*2];
      int nThreads = group.enumerate(threads, false);
      for (int i=0; i<nThreads; i++) {
         indentPrint(indent+3, threads[i]);
      ThreadGroup[] groups = new ThreadGroup[group.activeGroupCount()*2];
      int nGroups = group.enumerate(groups, false);
      for (int i=0; i<nGroups; i++) {
         printThreads_aux(groups[i], indent+3);
   public static void indentPrint(int cols, Object value) {
      for (int i=0; i<cols; i++) {
         System.out.print(' ');
class MyThread extends Thread {
   public MyThread(ThreadGroup group, String name) {
      super(group, name);
      // keep this thread running as long as nondaemon main runs
   public void run() {
      while (true);


   Thread[Reference Handler,10,system]
   Thread[Signal Dispatcher,10,system]
      java.lang.ThreadGroup[name=My ThreadGroup #1,maxpri=10]
         Thread[My Thread #1,5,My ThreadGroup #1]
         Thread[My Thread #2,5,My ThreadGroup #1]
         java.lang.ThreadGroup[name=My ThreadGroup #2,maxpri=10]
            Thread[My Thread #3,5,My ThreadGroup #2]