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]