以编程方式进行线程转储 /JDI(Java 调试器接口)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/239544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread dump programmatically /JDI (Java Debugger Interface)
提问by jiriki
I like to generate a thread dump programmatically. I've learned that there a basically two ways to do it:
我喜欢以编程方式生成线程转储。我了解到,基本上有两种方法可以做到:
- Use the "Java Virtual Machine Tool Interface" JVM-TI
- Use the higher abstracted "Java Debugger Interface" JDI
- 使用“Java 虚拟机工具接口”JVM-TI
- 使用更高抽象的“Java 调试器接口”JDI
For the JVM-TI I was able to find some useful information, but I would have to write a JNI-DLL which, at least for the moment, I would like to avoid. With the JDI I can use Java and it seems I'm able to use it from within the application. But I wasn't able to find some kind of tutorial or HOWTO for it. The only documentation I could find, were the Java-Docs http://java.sun.com/j2se/1.5.0/docs/guide/jpda/jdi/which isn't very helpful, because it doesn't show me how to use this classes.
对于 JVM-TI,我能够找到一些有用的信息,但是我必须编写一个 JNI-DLL,至少目前我想避免这种情况。通过 JDI,我可以使用 Java,而且我似乎可以在应用程序中使用它。但我无法找到某种教程或 HOWTO。我能找到的唯一文档是 Java-Docs http://java.sun.com/j2se/1.5.0/docs/guide/jpda/jdi/这不是很有帮助,因为它没有显示给我如何使用这个类。
So, does anybody know of a good tutorial/book I could read?
那么,有人知道我可以阅读的好的教程/书籍吗?
Thx for any help!
感谢您的帮助!
采纳答案by VonC
Did you consider the remote alternative ? I.e. VisualVM
你考虑过远程选择吗?即VisualVM


jps and jstackare also useful tools included in JDK 5, providing a quick command line method for obtaining stack traces of all current threads.
jps 和 jstack也是 JDK 5 中包含的有用工具,提供了一种快速的命令行方法来获取所有当前线程的堆栈跟踪。
This article suggest JDI is also used as a remote tool.
本文建议 JDI 也用作远程工具。
So I am not sure you can triggers a thread dump within your own program, instead you find a way to send to yourself a SIGQUIT signal (kill -3) on Unix platforms, or press the Ctrl-\ key on Unix or Ctrl-Break on Windows platforms.
因此,我不确定您是否可以在自己的程序中触发线程转储,而是找到一种在 Unix 平台上向自己发送 SIGQUIT 信号(kill -3)的方法,或者在 Unix 上按 Ctrl-\ 键或 Ctrl-Break在 Windows 平台上。
Plus, JDI wasn't intended to be used to debug the same process in which the JDI client is running. Still this thread I just linked to is the closest I have found to actually use JDI within the same program.
另外,JDI 不打算用于调试运行 JDI 客户端的同一进程。我刚刚链接到的这个线程仍然是我发现在同一程序中实际使用 JDI 最接近的线程。
回答by jiriki
There is a third way: Thread.getAllStackTraces()
还有第三种方式:Thread.getAllStackTraces()
http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#getAllStackTraces()
http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#getAllStackTraces()
This is much easier than the debugger interface...
这比调试器界面容易得多......
回答by jiriki
You can get just about all the Thread info you need including deadlocks from http://java.sun.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html
您可以从http://java.sun.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html获取您需要的几乎所有线程信息,包括死锁
回答by jiriki
Thread.getAllStackTraces() dumps only the execution trace of all the threads, but doesn't give the information of object locks that have been obtained by a particular thread or the lock on which a particular thread has been waiting. Basically, we'll not be able to nail down deadlocks with this.
Thread.getAllStackTraces() 只转储所有线程的执行轨迹,但不提供特定线程已获得的对象锁或特定线程一直在等待的锁的信息。基本上,我们将无法解决这个问题的僵局。

