FileDocCategorySizeDatePackage
ThreadLister.javaAPI DocExample3716Mon Sep 22 13:30:32 BST 1997None

ThreadLister

public class ThreadLister extends Object
This class contains a useful static method for listing all threads and threadgroups in the VM. It also has a simple main() method so it can be run as a standalone program.

Fields Summary
Constructors Summary
Methods Summary
public static voidlistAllThreads(java.io.PrintWriter out)
Find the root thread group and list it recursively

    ThreadGroup current_thread_group;
    ThreadGroup root_thread_group;
    ThreadGroup parent;
    
    // Get the current thread group
    current_thread_group = Thread.currentThread().getThreadGroup();
    
    // Now go find the root thread group
    root_thread_group = current_thread_group;
    parent = root_thread_group.getParent();
    while(parent != null) {
      root_thread_group = parent;
      parent = parent.getParent();
    }
    
    // And list it, recursively
    printGroupInfo(out, root_thread_group, "");
  
public static voidmain(java.lang.String[] args)
The main() method: just print the list of threads to the console

    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    ThreadLister.listAllThreads(out);
    out.flush();
  
private static voidprintGroupInfo(java.io.PrintWriter out, java.lang.ThreadGroup g, java.lang.String indent)
Display info about a thread group and its threads and groups

    if (g == null) return;
    int num_threads = g.activeCount();
    int num_groups = g.activeGroupCount();
    Thread[] threads = new Thread[num_threads];
    ThreadGroup[] groups = new ThreadGroup[num_groups];
    
    g.enumerate(threads, false);
    g.enumerate(groups, false);
    
    out.println(indent + "Thread Group: " + g.getName() + 
                "  Max Priority: " + g.getMaxPriority() +
                (g.isDaemon()?" Daemon":""));
    
    for(int i = 0; i < num_threads; i++)
      printThreadInfo(out, threads[i], indent + "    ");
    for(int i = 0; i < num_groups; i++)
      printGroupInfo(out, groups[i], indent + "    ");
  
private static voidprintThreadInfo(java.io.PrintWriter out, java.lang.Thread t, java.lang.String indent)
Display information about a thread.

    if (t == null) return;
    out.println(indent + "Thread: " + t.getName() +
                "  Priority: " + t.getPriority() +
                (t.isDaemon()?" Daemon":"") +
                (t.isAlive()?"":" Not Alive"));