Tuesday, 23 October 2012

Investigating Deadlocks – Part 2: Obtaining the Thread Dump

One of the most important requirements when investigating deadlocks is actually having a deadlock to investigate. In my last blog I wrote some code called DeadlockDemo that used a bunch of threads to transfer random amounts between a list of bank accounts before grinding to a halt in a deadlock.

This blog runs that code to demonstrates a few ways of obtaining a thread dump. A thread dump is simply a report showing the status of all your application’s threads at a given point in time. The good thing about it is that it contains various bits of information that will allow you to figure out why you have a deadlock and hopefully allowing you to fix it, but more on that later.


If your Java application is running on a UNIX machine the first, and possibly easiest, way to grab hold of a thread dump is to use the UNIX kill command via a terminal.

To do this, first get hold of your application’s process identifier or PID using the ps and grep commands. For example if you type:

    ps –e | grep java

...then you’ll produce a list that looks something like this:

74941 ttys000    0:00.01 grep java
70201 ttys004    1:00.89 /usr/bin/java threads.deadlock.DeadlockDemo

The PID for DeadlockDemo is, in this case, 70201 and is taken from the output above. Note that different flavours of UNIX or different ps command line args can produce slightly different results, so check your man pages.

Having got hold of your PID, use it to issue a kill SIGQUIT command:

    kill -3 70201

The kill command is the UNIX command that disposes of unwanted processes

Although the -3 above is the SIGQUIT (equivalent to a keyboard ctrl-D) argument, if Java receives this signal it will not quit, it will display a thread dump on its associated terminal. You can then grab hold of this and copy it into a text file for further analysis.


If you’re working in Windows then the UNIX command line isn’t available. To counter this problem Java comes with a utility that performs the equivalent of kill. This is called jstack and is available on both UNIX and Windows. It is used in the same way as the kill command demonstrated above:

    jstack <PID>

Getting hold of a PID in Windows is a matter of opening the Windows Task Manager. Task Manager doesn’t display the PIDs by default and so you need to update its setup by using the view menu option and checking the PID (Process Identifier) option in the Select Columns dialogue box.

Next, it’s just a matter of examining the process list and finding the appropriate instance of java.exe.

Read java.exe’s PID and use it as a jstack argument as shown below:

    jstack 3492

Once the command has completed you can grab hold of the output and copy it into a text file for further analysis.


jVisualVM is the 'Rolls Royce' way of obtaining a thread dump. It’s provided by Oracle as tool that allows you to get hold of lots of different info about a Java VM. This includes heap dumps, CPU usage, memory profiling and much more.

jVisualVM’s actual program name is jvisualvm or jvisualvm.exe on Windows. Once running you’ll see something like this:

To obtain a thread dump, find your application in the left hand applications panel, then right click and select: “Thread Dump”.

A thread dump is then displayed in jvisualvm’s right-hand pane as shown below:

Note that I have seen jvisualvm hang on several occasions when connecting to a local VM. When this happens ensure that its proxy settings are set to No Proxy

Having obtained a thread dump, my next blog will now use it to investigate the what’s going wrong with the example DeadlockDemo code.

For more information see the other blogs in this series.

All source code for this an other blogs in the series are available on Github at git://github.com/roghughe/captaindebug.git

No comments: