在正在运行的JVM中查找正在运行的实例 [英] Finding running instances in a running JVM

查看:163
本文介绍了在正在运行的JVM中查找正在运行的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以获得运行给定类的实例的句柄。触发此问题的特定问题是由于许多正在运行的线程而无法正常退出的应用程序。

I'm wondering if it's possible to get a handle on running instances of a given class. The particular issue that triggered this was an application that doesn't exit nicely because of a number of running threads.

是的,我知道你可以守护theads,然后他们不会阻止应用程序退出。但它确实让我想知道这是否可行。我最接近的是类加载器(受保护!) findLoadedClass ,虽然您必须通过自己的类加载器来执行此操作。

Yes, I know you can daemonize the theads, and they won't then hold up the application exit. But it did get me to wondering if this was possible. The closest thing I can is the classloaders (protected!) findLoadedClass, although you'd have to run through your own classloader to do this.

在相关的说明中,这是分析工具如何管理跟踪对象句柄的?通过运行自己的自定义类加载器?还是有一些我看不到的好看的狡猾方式?

On a related note, is this how profiling tools manage to track object handles? by running through their own custom classloaders? or is there some nice tricky way that I'm not seeing?

推荐答案

您确实可以通过使用 kill获取转储到stdout的所有正在运行的线程的堆栈跟踪 - 在类似操作系统的* NIX上退出< pid> ,或者在Windows控制台中运行应用程序并按 Ctrl-Pause (另一张海报)注意。)

You can indeed get a stack trace of all running Threads dumped to the stdout by using kill -QUIT <pid> on a *NIX like OS, or by running the application in a Windows console and pressing Ctrl-Pause (as another poster notes.)

然而,似乎你要求采用编程方式来做到这一点。因此,假设您真正想要的是当前调用堆栈的所有线程的集合包括来自给定类的一个或多个方法...

However, it seems like you're asking for programmatic ways to do it. So, assuming what you really want is the set of all Threads who's current call stacks include one or more methods from a given class...

我能找到的最好的东西不涉及调用JVMTI的是检查所有正在运行的线程的堆栈。我没有试过这个,但它应该在Java 1.5及更高版本中运行。请记住,根据定义,这不是所有线程安全的(正在运行的线程列表 - 以及它们当前的堆栈跟踪 - 将在您下面不断变化...实际使用时需要大量偏执的东西这个清单。)

The best thing I can find that doesn't involve calls into the JVMTI is to inspect the stacks of all running threads. I haven't tried this, but it should work in Java 1.5 and later. Keep in mind that this is, by definition, not AT ALL thread-safe (the list of running threads - and their current stack traces - are going to constantly change underneath you...lots of paranoid stuff would be necessary to actually make use of this list.)

public Set<Thread> findThreadsRunningClass(Class classToFindRunning) {

  Set<Thread> runningThreads = new HashSet<Thread>();
  String className = classToFindRunning.getName();

  Map<Thread,StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
  for(Thread t : stackTraces.keySey()) {
    StackTraceElement[] steArray = stackTraces.get(t);
    for(int i = 0;i<steArray.size();i++) {
      StackTraceElement ste = steArray[i];
      if(ste.getClassName().equals(className)) {
        runningThreads.add(t);
        continue;
      }
    }
  }

  return runningThreads;
}

请告诉我这种方法是否适用于您!

Let me know if this approach works out for you!

这篇关于在正在运行的JVM中查找正在运行的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆