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:

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) HerongYang.com. All Rights Reserved.
 */
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 = 5
   Thread name = Reference Handler
   Thread name = Finalizer
   Thread name = Signal Dispatcher
   Thread name = Attach Listener
   # of active sub thread groups = 1
   ThreadGroup name = main
      Has parent thread group = true
      # of active threads = 1
      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,9,system]
    Thread[Attach Listener,5,system]
    java.lang.ThreadGroup[name=main,maxpri=10]
        Thread[main,5,main]

Note that:

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 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

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB