Java代码 收藏代码
/**
* Copyright 2008, David Robert Nadeau, NadeauSoftware.com
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2 of
* the License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file. If not, see
* <a href="http://www.gnu.org/licenses">GNU Licenses</a>.
*/
import java.lang.management.*;
/**
* The ThreadUtilities class provides a collection of static methods
* for listing and finding Thread, ThreadGroup, and ThreadInfo objects.
* <p>
* Please see the accompanying article:
* <a href="http://nadeausoftware.com/articles/2008/04/java_tip_how_list_and_find_threads_and_thread_groups">Java tip: How to list and find threads and thread groups</a>
*
* @author <a href="http://NadeauSoftware.com/">David R. Nadeau</a>
*/
public final class ThreadUtilities
{
// Dummy constructor
/**
* The ThreadUtilities class cannot be instanced.
*/
private ThreadUtilities( )
{
}
// Thread groups
/**
* The root thread group saved on the first search for it.
* The root group doesn't change for the life of the JVM,
* so once found there is no need to find it again.
*/
private static ThreadGroup rootThreadGroup = null;
/**
* Get the root thread group in the thread group tree.
* Since there is always a root thread group, this
* method never returns null.
*
* @return the root thread group
*/
public static ThreadGroup getRootThreadGroup( )
{
if ( rootThreadGroup != null )
return rootThreadGroup;
/**
* Get a list of all thread groups. Since there is
* always at least one thread group (the root), this
* method never returns a null or empty array.
*
* @return an array of thread groups
*/
public static ThreadGroup[] getAllThreadGroups( )
{
final ThreadGroup root = getRootThreadGroup( );
int nAlloc = root.activeGroupCount( );
int n = 0;
ThreadGroup[] groups = null;
do
{
nAlloc *= 2;
groups = new ThreadGroup[ nAlloc ];
n = root.enumerate( groups, true );
} while ( n == nAlloc );
ThreadGroup[] allGroups = new ThreadGroup[n+1];
allGroups[0] = root;
System.arraycopy( groups, 0, allGroups, 1, n );
return allGroups;
}
/**
* Get the thread group with the given name. A null is
* returned if no such group is found. If more than one
* group has the same name, the first one found is returned.
*
* @param name the thread group name to search for
* @return the thread group, or null if not found
* @throws NullPointerException
* if the name is null
*/
public static ThreadGroup getThreadGroup( final String name )
{
if ( name == null )
throw new NullPointerException( "Null name" );
final ThreadGroup[] groups = getAllThreadGroups( );
for ( ThreadGroup group : groups )
if ( group.getName( ).equals( name ) )
return group;
return null;
}
// Threads
/**
* Get a list of all threads. Since there is always at
* least one thread, this method never returns null or
* an empty array.
*
* @return an array of threads
*/
public static Thread[] getAllThreads( )
{
final ThreadGroup root = getRootThreadGroup( );
final ThreadMXBean thbean =
ManagementFactory.getThreadMXBean( );
int nAlloc = thbean.getThreadCount( );
int n = 0;
Thread[] threads = null;
do
{
nAlloc *= 2;
threads = new Thread[ nAlloc ];
n = root.enumerate( threads, true );
} while ( n == nAlloc );
return java.util.Arrays.copyOf( threads, n );
}
/**
* Get a list of all threads in a thread group. An empty
* array is returned if there are no threads in the group.
*
* @param group the thread group to list
* @return an array of threads
* @throws NullPointerException
* if the group is null
*/
public static Thread[] getGroupThreads( final ThreadGroup group )
{
if ( group == null )
throw new NullPointerException( "Null group" );
int nAlloc = group.activeCount( );
int n = 0;
Thread[] threads = null;
do
{
nAlloc *= 2;
threads = new Thread[ nAlloc ];
n = group.enumerate( threads, false );
} while ( n == nAlloc );
return java.util.Arrays.copyOf( threads, n );
}
/**
* Get a list of all threads in a named thread group.
* A null is returned if the group cannot be found.
* An empty array is returned if there are no threads in
* the group.
*
* @param name the name of the thread group
* @return an array of threads, or null if the
* group is not found
* @throws NullPointerException
* if the name is null
*/
public static Thread[] getGroupThreads( final String name )
{
final ThreadGroup group = getThreadGroup( name );
if ( group == null )
return null;
return getGroupThreads( group );
}
/**
* Get a list of all threads, sorted from highest to
* lowest priority. Since there is always at least one
* thread, this method never returns null or an empty
* array. Since threads may change their priority during
* this method's sort, the returned thread list may not be
* correct by the time it is returned.
*
* @return an array of threads
*/
public static Thread[] getAllThreadsPrioritized( )
{
final Thread[] allThreads = getAllThreads( );
java.util.Arrays.sort( allThreads,
new java.util.Comparator<Thread>( )
{
public int compare( final Thread t1, final Thread t2 )
{ return t2.getPriority( ) - t1.getPriority( ); }
} );
return allThreads;
}