Thread Groups

Threads are organized into thread groups. A thread group is simply a related collection of threads. For instance, for security reasons all the threads an applet spawns are members of a particular thread group. The applet is allowed to manipulate threads in its own group but not in others. Thus an applet can't turn off the system's garbage collection thread, for example.

Thread groups are organized into a hierarchy of parents and children.

The following program prints all active threads by using the getThreadGroup() method of java.lang.Thread and getParent() method of java.lang.ThreadGroup to walk up to the top level thread group; then using enumerate to list all the threads in the main thread group and its children (which covers all thread groups).

public class AllThreads {

  public static void main(String[] args) {

    ThreadGroup top = Thread.currentThread().getThreadGroup();
    while(true) {
      if (top.getParent() != null) top = top.getParent();
      else break;
    }
    Thread[] theThreads = new Thread[top.activeCount()];
    top.enumerate(theThreads);
    for (int i = 0; i < theThreads.length; i++) {
      System.out.println(theThreads[i]);
    }

  }

}
The exact list of threads varies from system to system, but it should look something like this:

Thread[clock handler,11,system]
Thread[idle thread,0,system]
Thread[Async Garbage Collector,1,system]
Thread[Finalizer thread,1,system]
Thread[main,1,main]
Thread[Thread-0,5,main]

Previous | Next | Top
Last Modified April 14, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu