Lives in the UK. Likes blogging, cycling and eating lemon drizzle cake. Roger is a DZone MVB and is not an employee of DZone and has posted 143 posts at DZone. You can read more from them at their website. View Full User Profile

Investigating Deadlocks – Part 2

11.01.2012
| 2352 views |
  • submit to reddit
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.

kill SIGQUIT

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.

jstack

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

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.
Published at DZone with permission of Roger Hughes, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Ratnesh Singh replied on Tue, 2012/11/06 - 12:10am

This one is great...

Waiting for next......... 

Roger Hughes replied on Sun, 2012/11/11 - 5:05am in response to: Ratnesh Singh

Ratnesh

You can find a complete list of links to blogs in this series on my Captain Debug blog .

Roger

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.