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) HerongYang.com. All Rights Reserved.
 */
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
Running BThread 6
Running BThread 5
Running AThread 3
Ending BThread 5
Ending BThread 6
Running BThread 8
Running BThread 7
Ending AThread 2
Ending BThread 7
Ending BThread 8
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 = Attach Listener
   # of active sub thread groups = 2
   ThreadGroup name = main
      Has parent thread group = true
      # of active threads = 2
      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 = 0
         # 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]
        Thread[AThread 3,5,main]
        java.lang.ThreadGroup[name=Group X,maxpri=10]

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