`
hj270187161
  • 浏览: 21490 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多
获取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栏里面有显示
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics