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

Displaying the "system" ThreadGroup Tree

This section provides a tutorial example on how to display the 'system' ThreadGroup tree by using the enumerate() and list() methods.

As I mentioned in the previous section, there is a single tree structure managed by the JVM to organize all objects of ThreadGroup class and Thread class. Here are the rules of how this tree is maintained:

  • The root node of the tree is a ThreadGroup object named "system".
  • The "system" thread group contains a number of threads performing JVM system tasks.
  • The "system" thread group contains just one ThreadGroup object named "main".
  • The "main" thread group contains one thread named "main", which is the starting thread for the application program.
  • The "main" thread group contains no other ThreadGroup objects.
  • If a new ThreadGroup object is created without a parent ThreadGroup object specified, it will be added into the "main" thread group.
  • If a new Thread object is created without a ThreadGroup object specified, it will be added into the "main" thread group.

Here is a sample program showing how to use some of the methods in ThreadGroup class to get information out of the ThreatGroup tree:

/**
 * ThreadGroupTree.java
 * Copyright (c) 2003 by Dr. Herong Yang
 */
class ThreadGroupTree {
   public static void main(String[] args) {
      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);
      }  
   }   
}

Output:

ThreadGroup name = system
   Has parent thread group = false
   # of active threads = 6
   Thread name = Reference Handler
   Thread name = Finalizer
   Thread name = Signal Dispatcher
   Thread name = CompileThread0
   # of active sub thread groups = 1
   ThreadGroup name = main
      Has parent thread group = true
      # of active threads = 2
      Thread name = main
      # 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]

Note that:

  • There are 4 Thread objects in the "system" thread group. I have no idea what they are for.
  • The "main" thread group contains 2 Thread objects. But the enumerate() method copies only one thread object - the "main" thread.
  • However the list() method is correctly reporting 2 Thread objects, named "main" and "Thread-0".
  • What is the purpose of "Thread-0" thread? How to get a reference of this thread?

Table of Contents

 About This Book

 Installing JDK 1.4 on Windows 2000

 Installing JDK 1.5 on Windows XP

 Installing JDK 1.6 on Windows XP

 Execution Process, Entry Point, Input and Output

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Execution Threads and Multi-Threading Java Programs

ThreadGroup Class and "system" ThreadGroup Tree

 "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

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Displaying the "system" ThreadGroup Tree