Investigating Deadlocks – Part 2

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.

Reference: Investigating Deadlocks – Part 2: Obtaining the Thread Dump from our JCG partner Roger Hughes at the Captain Debug’s Blog blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply

three × 2 =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books