001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: // This code is repackaged after the code from David Flanagan, Java Examples in a Nutshell
034: // Site http://www.oreilly.com and http://www.davidflanagan.com/javaexamples
035: // Email
036: package com.db.utils;
037:
038: import java.io.*;
039:
040: /**
041: * This class contains a useful static method for listing all threads
042: * and threadgroups in the VM. It also has a simple main() method so it
043: * can be run as a standalone program.
044: **/
045: public class ThreadLister {
046:
047: /** Display information about a thread. */
048: private static void printThreadInfo(PrintWriter out, Thread t,
049: String indent) {
050:
051: if (t == null)
052: return;
053: out.println(indent + "Thread: " + t.getName() + " Priority: "
054: + t.getPriority() + (t.isDaemon() ? " Daemon" : "")
055: + (t.isAlive() ? "" : " Not Alive"));
056:
057: }
058:
059: /** Display info about a thread group and its threads and groups */
060: private static void printGroupInfo(PrintWriter out, ThreadGroup g,
061: String indent) {
062:
063: if (g == null)
064: return;
065: int num_threads = g.activeCount();
066: int num_groups = g.activeGroupCount();
067: Thread[] threads = new Thread[num_threads];
068: ThreadGroup[] groups = new ThreadGroup[num_groups];
069:
070: g.enumerate(threads, false);
071: g.enumerate(groups, false);
072:
073: out.println(indent + "Thread Group: " + g.getName()
074: + " Max Priority: " + g.getMaxPriority()
075: + (g.isDaemon() ? " Daemon" : ""));
076:
077: for (int i = 0; i < num_threads; i++)
078: printThreadInfo(out, threads[i], indent + " ");
079: for (int i = 0; i < num_groups; i++)
080: printGroupInfo(out, groups[i], indent + " ");
081:
082: }
083:
084: /** Find the root thread group and list it recursively */
085: public static void listAllThreads(PrintWriter out) {
086:
087: ThreadGroup current_thread_group;
088: ThreadGroup root_thread_group;
089: ThreadGroup parent;
090:
091: // Get the current thread group
092: current_thread_group = Thread.currentThread().getThreadGroup();
093:
094: // Now go find the root thread group
095: root_thread_group = current_thread_group;
096: parent = root_thread_group.getParent();
097: while (parent != null) {
098: root_thread_group = parent;
099: parent = parent.getParent();
100: }
101:
102: // And list it, recursively
103: printGroupInfo(out, root_thread_group, "");
104:
105: }
106:
107: /**
108: * The main() method: just print the list of threads to the console
109: **/
110: public static void main(String[] args) {
111:
112: PrintWriter out = new PrintWriter(new OutputStreamWriter(
113: System.out));
114: ThreadLister.listAllThreads(out);
115: out.flush();
116:
117: }
118:
119: /**
120: * This nested class is a simple applet that displays the output of
121: * ThreadLister.listAllThreads() when run from an the start() method of
122: * an applet. The output from an applet is typically different than the
123: * output when run from a standalone program.
124: **/
125: public static class Applet extends java.applet.Applet {
126:
127: java.awt.TextArea textarea; // Where to display the output.
128:
129: /** Create a text area to put our listing in */
130: public void init() {
131:
132: textarea = new java.awt.TextArea();
133: this .setLayout(new java.awt.BorderLayout());
134: this .add("Center", textarea);
135:
136: }
137:
138: /** When the applet starts, list all the threads */
139: public void start() {
140:
141: StringWriter sout = new StringWriter(); // Capture listing in a string
142: PrintWriter out = new PrintWriter(sout);
143: ThreadLister.listAllThreads(out);
144: out.flush();
145: textarea.setText(sout.toString()); // Display the listing
146:
147: }
148:
149: }
150:
151: }
|