Java Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 6.00

Adding Threads and Thread Groups

This section provides a tutorial example on how to add threads and thread groups to the 'system' ThreadGroup tree.

The next program shows how to add more threads and thread groups to the ThreadGroup tree:

/**
 * GroupingThreads.java
 * Copyright (c) 2003 by Dr. Herong Yang
 */
class GroupingThreads {
   public static void main(String[] args) {
      Thread t = new AThread();
      t.setName("AThread 1");
      t = new AThread();
      t.setName("AThread 2");
      t.start();
      t.interrupt();
      t = new AThread();
      t.setName("AThread 3");
      t.start();
      ThreadGroup g = new ThreadGroup("Group X");
      t = new BThread(g, "BThread 4");
      t = new BThread(g, "BThread 5");
      t.start();
      t = new BThread(g, "BThread 6");
      t.start();
      g.interrupt();
      g = new ThreadGroup("Group Y");
      t = new BThread(g, "BThread 7");
      t.start();
      t = new BThread(g, "BThread 8");
      t.start();
      g.interrupt();
      try {Thread.sleep(1000);}
      catch (InterruptedException e) {}
      g.destroy();
      printInfo();
   }
   public static void printInfo() {
      Thread t = Thread.currentThread();
      ThreadGroup g = t.getThreadGroup();
      g = g.getParent();
      printInfo(g,"");
      g.list();
   }
   public static void printInfo(ThreadGroup g, String p) {
      System.out.println(p+"ThreadGroup name = "+g.getName());
      System.out.println(p+"   Has parent thread group = "
         +(g.getParent()!=null));
      int n = g.activeCount();
      System.out.println(p+"   # of active threads = "+n);
      Thread[] l = new Thread[n];
      n = g.enumerate(l, false);
      for (int i=0; i<n; i++) {
         System.out.println(p+"   Thread name = "+l[i].getName());
      }  
      n = g.activeGroupCount();
      System.out.println(p+"   # of active sub thread groups = "+n);
      ThreadGroup[] s = new ThreadGroup[n];
      n = g.enumerate(s, false);
      for (int i=0; i<n; i++) {
         printInfo(s[i],"   "+p);
      }  
   }   
   static class AThread extends Thread {
      public void run() {
         System.out.println("Running "+getName());
         try {
            sleep(1000*60*60);
         } catch (InterruptedException e) {
         }
         System.out.println("Ending "+getName());
      }
   }
   static class BThread extends Thread {
      public BThread(ThreadGroup g, String n) {
         super(g,n);
      }
      public void run() {
         System.out.println("Running "+getName());
         try {
            sleep(1000*60*60);
         } catch (InterruptedException e) {
         }
         System.out.println("Ending "+getName());
      }
   }
}

Output:

Running AThread 2
Ending AThread 2
Running AThread 3
Running BThread 5
Ending BThread 5
Running BThread 6
Ending BThread 6
Running BThread 7
Ending BThread 7
Running BThread 8
Ending BThread 8
ThreadGroup name = system
   Has parent thread group = false
   # of active threads = 9
   Thread name = Reference Handler
   Thread name = Finalizer
   Thread name = Signal Dispatcher
   Thread name = CompileThread0
   # of active sub thread groups = 2
   ThreadGroup name = main
      Has parent thread group = true
      # of active threads = 5
      Thread name = main
      Thread name = AThread 3
      # of active sub thread groups = 1
      ThreadGroup name = Group X
         Has parent thread group = true
         # of active threads = 1
         # of active sub thread groups = 0
java.lang.ThreadGroup[name=system,maxpri=10]
    Thread[Reference Handler,10,system]
    Thread[Finalizer,8,system]
    Thread[Signal Dispatcher,10,system]
    Thread[CompileThread0,10,system]
    java.lang.ThreadGroup[name=main,maxpri=10]
        Thread[main,5,main]
        Thread[Thread-0,5,main]
        Thread[AThread 1,5,main]
        Thread[AThread 3,5,main]
        java.lang.ThreadGroup[name=Group X,maxpri=10]
            Thread[BThread 4,5,Group X]

Note that:

  • As expected, new thread groups were added to the "main" thread group by default.
  • As expected, new threads are were added to the "main" thread group by default, if they were created without a thread group.
  • Thread "AThread 1" was never started. It was reported by the list() method, but not included in the enumerate() method.
  • Based on the previous note, we can calculate the number of unstarted threads in thread group by activeCount()-enumerate(Thread[] l, true).
  • Thread group "Group Y" was destroyed by the destroy() method, when it became empty.

Sections in This Chapter

"ThreadGroup" Class - Container of Threads and Thread Groups

Displaying the "system" ThreadGroup Tree

Adding Threads and Thread Groups

JDK 1.3 Bug - Memory Leak With Unstarted Threads

Dr. Herong Yang, updated in 2008
Adding Threads and Thread Groups