views:

374

answers:

6

Hello
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

+1  A: 

I don't know about eclipse but what you are looking for is a profiler. Checkout JProfiler or have a look at this list for 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.

raoulsson
YourKit has the best representation of threads and sometimes it can even detect the deadlock automatically.
nanda
+1  A: 

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.

raoulsson
A: 

What about NetBeans? I've heard that it can debug and detect a Deadlock.

Omar Dolaimy
A: 

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.

Steve Emmerson
+4  A: 

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

Thorbjørn Ravn Andersen
A: 

I would recommend using a static analysis tool like

FindBugs

, which can often detect Deadlocks at compile-time

Helper Method