/** * 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; ThreadGroup tg = Thread.currentThread( ).getThreadGroup( ); ThreadGroup ptg; while ( (ptg = tg.getParent( )) != null ) tg = ptg; rootThreadGroup = tg; return tg; } /** * 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; } /** * Get a list of all daemon threads. An empty array is * returned if there are no daemon threads. * * @return an array of daemon threads */ public static Thread[] getAllDaemonThreads( ) { final Thread[] allThreads = getAllThreads( ); final Thread[] daemons = new Thread[allThreads.length]; int nDaemon = 0; for ( Thread thread : allThreads ) if ( thread.isDaemon( ) ) daemons[nDaemon++] = thread; return java.util.Arrays.copyOf( daemons, nDaemon ); } /** * Get a list of all threads with a given thread state. * Thread states are defined in the Thread.State enum for * the Thread class. Principal thread states include * RUNNABLE, WAITING, TIMED_WAITING, and BLOCKED. An * empty array is returned if there are no threads in * the chosen state. * * @param state the state to look for * @return an array of threads in that state */ public static Thread[] getAllThreads( final Thread.State state ) { final Thread[] allThreads = getAllThreads( ); final Thread[] found = new Thread[allThreads.length]; int nFound = 0; for ( Thread thread : allThreads ) if ( thread.getState( ) == state ) found[nFound++] = thread; return java.util.Arrays.copyOf( found, nFound ); } /** * Get the thread with the given name. A null is returned * if no such thread is found. If more than one thread has * the same name, the first one found is returned. * * @param name the thread name to search for * @return the thread, or null if not found * @throws NullPointerException * if the name is null */ public static Thread getThread( final String name ) { if ( name == null ) throw new NullPointerException( "Null name" ); final Thread[] threads = getAllThreads( ); for ( Thread thread : threads ) if ( thread.getName( ).equals( name ) ) return thread; return null; } /** * Get the thread with the given ID. A null is returned * if no such thread is found. * * @param id the thread ID to search for * @return the thread, or null if not found */ public static Thread getThread( final long id ) { final Thread[] threads = getAllThreads( ); for ( Thread thread : threads ) if ( thread.getId( ) == id ) return thread; return null; } /** * Get the thread for the given thread info. A null * is returned if the thread cannot be found. * * @param info the thread info to search for * @return the thread, or null if not found * @throws NullPointerException * if info is null */ public static Thread getThread( final ThreadInfo info ) { if ( info == null ) throw new NullPointerException( "Null info" ); return getThread( info.getThreadId( ) ); } // ThreadInfo /** * Get a list of all thread info objects. Since there is * always at least one thread running, there is always at * least one thread info object. This method never returns * a null or empty array. * * @return an array of thread infos */ public static ThreadInfo[] getAllThreadInfos( ) { final ThreadMXBean thbean = ManagementFactory.getThreadMXBean( ); final long[] ids = thbean.getAllThreadIds( ); // Get thread info with lock info, when available. ThreadInfo[] infos; if ( !thbean.isObjectMonitorUsageSupported( ) || !thbean.isSynchronizerUsageSupported( ) ) infos = thbean.getThreadInfo( ids ); else infos = thbean.getThreadInfo( ids, true, true ); // Clean nulls from array if threads have died. final ThreadInfo[] notNulls = new ThreadInfo[infos.length]; int nNotNulls = 0; for ( ThreadInfo info : infos ) if ( info != null ) notNulls[nNotNulls++] = info; if ( nNotNulls == infos.length ) return infos; // Original had no nulls return java.util.Arrays.copyOf( notNulls, nNotNulls ); } /** * Get the thread info for the thread with the given name. * A null is returned if no such thread info is found. * If more than one thread has the same name, the thread * info for the first one found is returned. * * @param name the thread name to search for * @return the thread info, or null if not found * @throws NullPointerException * if the name is null */ public static ThreadInfo getThreadInfo( final String name ) { if ( name == null ) throw new NullPointerException( "Null name" ); final Thread[] threads = getAllThreads( ); for ( Thread thread : threads ) if ( thread.getName( ).equals( name ) ) return getThreadInfo( thread.getId( ) ); return null; } /** * Get the thread info for the thread with the given ID. * A null is returned if no such thread info is found. * * @param id the thread ID to search for * @return the thread info, or null if not found * @throws IllegalArgumentException * if id <= 0 */ public static ThreadInfo getThreadInfo( final long id ) { final ThreadMXBean thbean = ManagementFactory.getThreadMXBean( ); // Get thread info with lock info, when available. if ( !thbean.isObjectMonitorUsageSupported( ) || !thbean.isSynchronizerUsageSupported( ) ) return thbean.getThreadInfo( id ); final ThreadInfo[] infos = thbean.getThreadInfo( new long[] { id }, true, true ); if ( infos.length == 0 ) return null; return infos[0]; } /** * Get the thread info for the given thread. A null is * returned if the thread info cannot be found. * * @param thread the thread to search for * @return the thread info, or null if not found * @throws NullPointerException * if thread is null */ public static ThreadInfo getThreadInfo( final Thread thread ) { if ( thread == null ) throw new NullPointerException( "Null thread" ); return getThreadInfo( thread.getId( ) ); } // MonitorInfo and LockInfo /** * Get the thread holding a lock on the given object. * A null is returned if there is no such thread. * * @param object the object to look for a lock on * @return the thread holding a lock, or * null if there is none * @throws NullPointerException * if the object is null */ public static Thread getLockingThread( final Object object ) { if ( object == null ) throw new NullPointerException( "Null object" ); final long identity = System.identityHashCode( object ); final Thread[] allThreads = getAllThreads( ); ThreadInfo info = null; MonitorInfo[] monitors = null; for ( Thread thread : allThreads ) { info = getThreadInfo( thread.getId( ) ); if ( info == null ) continue; monitors = info.getLockedMonitors( ); for ( MonitorInfo monitor : monitors ) if ( identity == monitor.getIdentityHashCode( ) ) return thread; } return null; } /** * Get the thread who's lock is blocking the given thread. * A null is returned if there is no such thread. * * @param blockedThread the blocked thread * @return the blocking thread, or null if * there is none * @throws NullPointerException * if the blocked thread is null */ public static Thread getBlockingThread( final Thread blockedThread ) { final ThreadInfo info = getThreadInfo( blockedThread ); if ( info == null ) return null; final long id = info.getLockOwnerId( ); if ( id == -1 ) return null; return getThread( id ); } }
http://nadeausoftware.com/articles/2008/04/java_tip_how_list_and_find_threads_and_thread_groups
相关推荐
基于Php语言设计并实现了微信小程序的社区门诊管理系统。该小程序基于B/S即所谓浏览器/服务器模式,选择MySQL作为后台数据库去开发并实现一个以微信小程序的社区门诊为核心的系统以及对系统的简易介绍。 用户注册,在用户注册页面通过填写账号、密码、确认密码、姓名、性别、手机、等信息进行注册操作; 用户登录,用户通过登录页面输入账号和密码,并点击登录进行小程序登录操作。 用户登陆微信端后,可以对首页、门诊信息、我的等功能进行详细操作 门诊信息,在门诊信息页面可以查看科室名称、科室类型、医生编号、医生姓名、 职称、坐诊时间、科室图片、点击次数、科室介绍等信息进行预约挂号操作 检查信息,在检查信息页面可以查看检查项目、检查地点、检查时间、检查费用、账号、姓名、医生编号、医生姓名、是否支付、审核回复、审核状态等信息进行支付操作
白色大气风格的设计师作品模板下载.zip
工程经济学自考必备软件下载
UML课程设计报告.doc
白色大气风格响应式彩绘精品水果网站模板.zip
白色简洁风格的别墅整站网站模板.zip
白色简洁风格的APP展示动态源码下载.zip
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于计算机科学与技术等相关专业,更为适合;
白色大气风格的雪山旅游景区CSS3网站模板.zip
介绍 基于python开发的大模型调用基础框架(源码) 使用说明 修改配置文件 cd config vim __init__.py # 在配置文件中添加大模型调用地址,模型名称,API_KEY等配置 启动应用 应用启动分为两种模式,命令行模式和web模式 命令行模式 python main.py cli web模式 python main.py api
基于JavaWeb的小区物业管理系统源代码+数据库 负责数据库的设计和界面的设计和实现; 界面使用 BootStrap 框架,页面自适应效果,修改页面后实现各个功能模块的布局; 负责实现用户登录注册,查看小区活动公告、水电费查询、车费查询信息; 采用的技术:采用 MVC 架构,数据库用 MySql;
白色简单风格的商务企业网站模板下载.zip
1. 平台在家电和电子产品方面的营运情况如何? 2. 哪些品牌和类别销量最高? 3. 用户消费规律 4. 哪些是我们的重点用户? 5. 平台有哪些优势和不足,需要如何改进?
全平台数据库管理工具, 支持 ClickHouse, Presto, Trino, MySQL, PostgreSQL, Apache Druid, ElasticSearch...
白色大气风格的旅游整站网站模板.zip
1、嵌入式物联网单片机项目开发例程,简单、方便、好用,节省开发时间。 2、代码使用KEIL 标准库开发,当前在STM32F030C8T6运行,如果是STM32F030其他型号芯片,依然适用,请自行更改KEIL芯片型号以及FLASH容量即可。 3、软件下载时,请注意keil选择项是jlink还是stlink。 4、有偿指导v:wulianjishu666; 5、如果接入其他传感器,请查看账号发布的其他资料。 6、单片机与模块的接线,在代码当中均有定义,请自行对照。 7、若硬件有差异,请根据自身情况调整代码,程序仅供参考学习。 8、代码有注释说明,请耐心阅读。 9、编译时请注意提示,请选择合适的编译器版本。
Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
白色大气风格的红唇少女女性类网站模板.zip
白色简洁风格的UIDesign设计公司企业网站模板.rar
Linux期末考试试题.doc