如何使用 Eclipse 在 Java 中调试死锁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2081766/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:19:35  来源:igfitidea点击:

How to Debug a deadlock in Java using Eclipse

javamultithreadingdebugging

提问by Thorbj?rn Ravn Andersen

What techniques can one use to debug what appears to be a deadlock in a Java program. My IDE is Eclipse and I think I've identifid the two deadlocked threads. In the debugger, right-clicking any of the threads in question and selecting suspend suspends the thread and displays the code currently being executed. Attempting step-into or step-over the line question appears to have no effect - the thread state changes to "Stepping" with control never returning to the debugger unless suspend is clicked again.
Thanks

可以使用哪些技术来调试 Java 程序中看似死锁的问题。我的 IDE 是 Eclipse,我想我已经确定了两个死锁线程。在调试器中,右键单击任何有问题的线程并选择挂起会挂起线程并显示当前正在执行的代码。尝试单步执行或单步跳过该行问题似乎没有任何效果 - 线程状态更改为“单步执行”,除非再次单击挂起,否则控制权永远不会返回到调试器。
谢谢

回答by Thorbj?rn Ravn Andersen

If you are using a Sun JVM then attach with JConsole and go to the Threads pane. There is a "Detect Deadlock" button.

如果您使用的是 Sun JVM,则附加 JConsole 并转到“线程”窗格。有一个“检测死锁”按钮。

回答by Mischa

A few minutes ago I stumbled upon this:

几分钟前,我偶然发现了这个:

http://runnerwhocodes.blogspot.com/2007/10/deadlock-detection-with-eclipse.html

http://runnerwhocodes.blogspot.com/2007/10/deadlock-detection-with-eclipse.html

For me it looks like the best approach detect deadlocks (even it would not help in each case, e.g. detecting deadlocks in combination with EMT-Locks).

对我来说,它看起来是检测死锁的最佳方法(即使在每种情况下都无济于事,例如检测与 EMT 锁相结合的死锁)。

回答by raoulsson

When running the program in "console mode", you can hit ctrl+break, which gives you a thread dump. Once you run into the deadlock, this could be helpful. If the deadlock doesn't appear all too often, it could be difficult to catch the deadlock like this however.

在“控制台模式”下运行程序时,您可以点击ctrl+break,这会为您提供线程转储。一旦陷入僵局,这可能会有所帮助。如果死锁不是经常出现,那么像这样捕捉死锁可能很困难。

回答by Steve Emmerson

The debugging window that shows the stacks of the various threads will indicate when a thread stops. When two threads are stopped, you can examine what each is waiting on. Finding something in common will tell you the source of the deadlock.

显示各种线程堆栈的调试窗口将指示线程何时停止。当两个线程停止时,您可以检查每个线程在等待什么。找到一些共同点会告诉你僵局的根源。

回答by raoulsson

I don't know about eclipse but what you are looking for is a profiler. Checkout JProfileror have a look at this listfor example. The profiler connects directly to the JVM and visualizes what's going on inside your program in real time. When deadlocks occur you get visual/textual clues of which threads are in conflict.

我不知道日食,但你正在寻找的是一个分析器。例如,检查JProfiler或查看此列表。分析器直接连接到 JVM 并实时可视化程序内部发生的情况。当发生死锁时,您会获得哪些线程发生冲突的视觉/文本线索。

回答by helpermethod

I would recommend using a static analysis tool like

我建议使用静态分析工具,如

FindBugs

查找错误

, which can often detect Deadlocks at compile-time

,这通常可以在编译时检测到死锁