获取java vm中当前运行的所有线程

获取java vm中当前运行的所有线程 

转载源:http://woai1huan.blog.163.com/blog/static/51337182200672451959865/

下面的静态方法可以用数组返回Java VM中当前运行的所有线程
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;
}
==============

如果只是想监控查看线程的话,那么在myEcllipse的debug栏里面有显示

你可能感兴趣的:(java,thread)