FileDocCategorySizeDatePackage
ThreadLister.javaAPI DocExample1694Sat Jun 02 02:44:12 BST 2001None

ThreadLister.java

// This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
// Copyright (c) 1997 by David Flanagan
// This example is provided WITHOUT ANY WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for non-commercial purposes.
// For any commercial use, see http://www.davidflanagan.com/javaexamples

import java.io.*;

/**
 * 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.
 **/
public class ThreadLister {
  /** Display information about a thread. */
  private static void printThreadInfo(PrintWriter out, Thread t, 
                                        String indent) {
    if (t == null) return;
    out.println(indent + "Thread: " + t.getName() +
                "  Priority: " + t.getPriority() +
                (t.isDaemon()?" Daemon":"") +
                (t.isAlive()?"":" Not Alive"));
  }
  
  /** Display info about a thread group and its threads and groups */
  private static void printGroupInfo(PrintWriter out, ThreadGroup g, 
                                 String indent) {
    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