查看当前jvm运行的线程

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/younglibin/article/details/88348467

参考: http://blog.csdn.net/defonds/article/details/4848853

 

 

 

查看当前 jvm有那写线程在执行, 代码如下,  另外附件给了 个jar包,可以直接运行

 

java -jar  findThrea-client.jar

运行结果如下: 写道
current threadName [Reference Handler] priority[10] group[java.lang.ThreadGroup[name=system,maxpri=10]] parentGroup[null]
current threadName [Finalizer] priority[8] group[java.lang.ThreadGroup[name=system,maxpri=10]] parentGroup[null]
current threadName [Signal Dispatcher] priority[9] group[java.lang.ThreadGroup[name=system,maxpri=10]] parentGroup[null]
current threadName [main] priority[5] group[java.lang.ThreadGroup[name=main,maxpri=10]] parentGroup[java.lang.ThreadGroup[name=system,maxpri=10]]

 

 

原码:

 

package com.younglibin;

public class FindThread {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread[] slackList = findAllThreads();
		if (null == slackList) {
			System.out.println("current jvm no thread.");
		} else {
			for (Thread t : slackList) {
				System.out
						.println(String
								.format("current threadName [%s] priority[%s] group[%s] parentGroup[%s] ",
										t.getName(), t.getPriority(), t
												.getThreadGroup(), t
												.getThreadGroup().getParent()));
			}
		}
	}

	public static Thread[] findAllThreads() {
		ThreadGroup group = Thread.currentThread().getThreadGroup();
		ThreadGroup topGroup = group;

		// 遍历线程组树,获取根线程组
		while (group != null) {
			topGroup = group;
			group = group.getParent();
		}
		// 激活的线程数加倍
		int estimatedSize = topGroup.activeCount() * 2;
		Thread[] slackList = new Thread[estimatedSize];
		// 获取根线程组的所有线程
		int actualSize = topGroup.enumerate(slackList);
		// copy into a list that is the exact size
		Thread[] list = new Thread[actualSize];
		System.arraycopy(slackList, 0, list, 0, actualSize);
		return list;
	}

}

 

展开阅读全文
博主设置当前文章不允许评论。

没有更多推荐了,返回首页