// StatusThreads - displays the Threads status of the Java virtual machine // // Partly based on the ThreadLister example fro "Java in a Nutshell". // See http://www.ora.com/www/catalog/books/javanut/examples/ // // Copyright (C) 1996 by Jef Poskanzer . All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF // SUCH DAMAGE. import java.applet.*; import java.awt.*; import java.io.*; public class StatusThreads extends Applet implements Runnable { // Applet info. public String getAppletInfo() { return getClass().getName() + " - Copyright (C) 1996 by Jef Poskanzer . All rights reserved."; } TextArea textArea; // Called when the applet is first created. public void init() { Acme.GuiUtils.handleBgcolor( this ); // // Figure out how big the TextArea should be, by trial&error. // // A binary search would be faster though. // int rows = 1; // int cols = 1; // Dimension pd; // do // { // textArea = new TextArea( rows + 1, cols ); // pd = textArea.preferredSize(); // } // while ( pd.width <= d.width ); // do // { // textArea = new TextArea( rows, cols + 1 ); // pd = textArea.preferredSize(); // } // while ( pd.height <= d.height ); // textArea = new TextArea( rows, cols ); setLayout( new BorderLayout() ); textArea = new TextArea(); textArea.setEditable( false ); add( "Center", textArea ); validate(); } Thread thread = null; Runtime runtime = Runtime.getRuntime(); // Called when the applet should start itself. public void start() { if ( thread == null ) { // Start the thread. thread = new Thread(this); thread.start(); } } // Called when the applet should stop itself. public void stop() { if ( thread != null ) { // Stop the thread. thread.stop(); thread = null; } } // This is the part of Runnable that we implement - the routine that // gets called when the thread is started. public void run() { Thread me = Thread.currentThread(); me.setPriority( Thread.MIN_PRIORITY ); while ( thread == me ) { Acme.TextcompOutputStream t = new Acme.TextcompOutputStream( textArea ); Acme.APrintStream p = new Acme.APrintStream( t ); listAll( p ); repaint(); try { Thread.sleep( 10000 ); // ten seconds } catch ( InterruptedException e ) {} } } static void listThread( PrintStream out, Thread t, String indent ) { if ( t == null ) return; out.println( indent + "Thread: " + t.getName() + " Priority: " + t.getPriority() + ( t.isDaemon() ? " Daemon" : "" ) + ( t.isAlive() ? "" : " Inactive" ) ); } static void listGroup( PrintStream out, ThreadGroup g, String indent ) { if ( g == null ) return; out.println( indent + "Thread Group: " + g.getName() + " Max Priority: " + g.getMaxPriority() + ( g.isDaemon() ? " Daemon" : "" ) ); String extraIndent = " "; int numThreadsEst = g.activeCount(); Thread[] threads = new Thread[numThreadsEst * 2]; int numThreads = g.enumerate( threads, false ); for ( int i = 0; i < numThreads; ++i ) listThread( out, threads[i], extraIndent + indent ); int numGroupsEst = g.activeGroupCount(); ThreadGroup[] groups = new ThreadGroup[numGroupsEst * 2]; int numGroups = g.enumerate( groups, false ); for ( int i = 0; i < numGroups; ++i ) listGroup( out, groups[i], extraIndent + indent ); } static void listAll( PrintStream out ) { ThreadGroup currentThreadGroup; ThreadGroup rootThreadGroup; ThreadGroup parent; // Start with the current thread group. currentThreadGroup = Thread.currentThread().getThreadGroup(); // Now follow the tree to the root thread group. rootThreadGroup = currentThreadGroup; parent = rootThreadGroup.getParent(); while ( parent != null ) { rootThreadGroup = parent; parent = parent.getParent(); } // And list it, recursively. listGroup( out, rootThreadGroup, "" ); } // Main program, so we can run as an application too. public static void main( String[] args ) { new Acme.MainFrame( new StatusThreads(), args, 400, 500 ); } }