A Java thread dump is a way of finding out what every thread in the JVM is doing at a particular point in time. This is especially useful if your Java application sometimes seems to hang when running under load, as an analysis of the dump will show where the threads are stuck.

You can generate a thread dump under Unix/Linux by running kill -QUIT <pid>, and under Windows by hitting Ctl + Break.

A great example of where this would be useful is the well-known Dining Philosophers deadlocking problem. Taking example code from Concurrency: State Models & Java Programs, we can cause a deadlock situation and then create a thread dump.

Dining Philosophers applet screenshot

In the example below (shown using tda), we can see that the 5 Philosopher threads each have a lock on a Fork object and are each waiting to obtain a lock on a second Fork object before they can eat. Unfortunately this never happens and all the philosophers starve.

Thread dump of the Dining Philosphers from Thread Dump Analyzer

Download the thread dump from here (7 KB).

Note that not all hangs are going to be due to deadlocks, and there are many tools (including Eclipse) that will help you analyse thread dumps.

 

Published On: April 10, 2007Tags:

7 Comments

  1. Stuart Moncrieff April 11, 2007 at 8:45 am - Reply

    The Sun 1.5 JVM is able to detect some simple deadlocks.

    Using the example code from
    http://java.sun.com/docs/books/faq/src/thr/DeadlockExample.html the thread dump includes the following information:

    Found one Java-level deadlock:
    =============================
    “Thread-1”:
    waiting to lock monitor 0x00a891ec (object 0x06c216e0, a java.lang.String),
    which is held by “Thread-0”
    “Thread-0”:
    waiting to lock monitor 0x00a8920c (object 0x06c21708, a java.lang.String),
    which is held by “Thread-1”

  2. Stuart Moncrieff April 11, 2007 at 8:56 am - Reply

    Some additional points:

    · You may be able to generate a thread dump from the Admin Console of your application server. Under Unix, this is much simpler than doing a kill -3 from the shell, and it means that you do not have to be a member of the same group that the application server is a member of.
    · It is helpful to run multiple stack traces over a short period of time. If the same thread is still stuck at the same position of code, that is a good indication of a problem. This is a good thing to create a shell script for.
    · I quite liked this blog entry on stack traces and thread dumps: http://www.me.umn.edu/~shivane/blogs/cafefeed/2004/06/of-thread-dumps-and-stack-traces.html
    · Also, this one: http://java.sun.com/developer/technicalArticles/Programming/Stacktrace/

  3. Anthony Hallett April 23, 2007 at 10:33 am - Reply

    Good to see you back posting

    🙂

  4. Jonathan Camilleri June 30, 2011 at 11:23 pm - Reply

    Any idea where the thread dump is located, when taking one using the ctrl + \, because I could not find a thread dump when I simulated a deadlock on my WinXP computer?

  5. Deepak January 10, 2012 at 7:26 pm - Reply
  6. Pierre-Hugues Charbonneau August 6, 2012 at 1:55 am - Reply

    Hi Stuart and good job with your performance related Blog here,

    Glad to see other individuals interested in sharing knowledge about Java Thread Dump. This is very important skill to master, especially for individuals involved in Java EE production support.

    I’m creating right now a whole article series from my Blog on this subject, your readers may be interested in this. It includes also real case studies and analysis approaches.

    http://javaeesupportpatterns.blogspot.com/2011/11/how-to-analyze-thread-dump-part-1.html

    Thanks.
    P-H

  7. jack June 13, 2016 at 8:17 pm - Reply

    http://fastthread.io/ is a good analytical tool with all types of graphical representation on thread dumps. I will recommend this tool to all my java team members.

Leave A Comment