Java Thread Dump

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 Philosopers 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.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

3 Responses to “Java Thread Dump”

  1. Stuart Moncrieff Says:

    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 0×00a891ec (object 0×06c216e0, a java.lang.String),
    which is held by “Thread-0″
    “Thread-0″:
    waiting to lock monitor 0×00a8920c (object 0×06c21708, a java.lang.String),
    which is held by “Thread-1″

  2. Stuart Moncrieff Says:

    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 Says:

    Good to see you back posting

    :-)

Leave a Reply