Java Thread Dump

Hi All, From this post I’m going to talk about one of the most important tools when programming in java. Hope you have a basic idea about threads. A web server uses tens to hundreds of threads to process a large number of concurrent users. If two or more threads utilize the same resources, a contention between the threads is inevitable, and sometimes deadlock occurs. If a deadlock occurs then neither of the threads will be able to finish its task successfully.

So for any one who is working with java it could be useful to know about threads and thread dump. Before going in to the tool, let’s check some thread statuses first.

  • NEW: The thread is created but has not been processed yet.
  • RUNNABLE: The thread is occupying the CPU and processing a task. (It may be in WAITING status due to the OS’s resource distribution.)
  • BLOCKED: The thread is waiting for a different thread to release its lock in order to get the monitor lock.
  • WAITING: The thread is waiting by using a wait, join or park method.
  • TIMED_WAITING: The thread is waiting by using a sleep, wait, join or park method.

And also there are two thread types as well,

  • Daemon Threads : stop working when there are no other non-daemon threads.
  • Non Daemon Threads : A thread running the ‘static void main(String[] args)’ method is created as a non-daemon thread, and when this thread stops working, all other daemon threads will stop as well.

JDK 1.6 and higher versions provide ‘jstack‘ which can be used to get a thread dump of a running process. Let’s see how to take a thread dump using jstack. Here I’m using an Ubuntu OS.

  • Find a PID of a running process. You can do this with the ‘jps‘ command.

Screenshot from 2016-08-24 15:27:09

  • Then use the following command to get a thread dump. We can send the result to standerd output as well, to a file.

jstack -l PID > file_name.out

Screenshot from 2016-08-24 15:28:59.png

After taking the thread dump, we can analyze it using another  tool. For thread dump analyzing we can use ‘jvisualvm‘ provided by java. You can find it from the ‘$JAVA_HOME/bin‘ directory.

Screenshot from 2016-08-24 15:32:32.png

Let’s see how to analyze a thread dump. We can get some useful information like below,

pool-1-thread-13prio=6 tid=0x000000000729a000 nid=0x2fb4 runnable [0x0000000007f0f000] java.lang.Thread.State: RUNNABLE
at Method)
at sun.nio.cs.StreamDecoder.readBytes(
at sun.nio.cs.StreamDecoder.implRead(
– locked <0x0000000780b7e688> (a
– locked <0x0000000780b7e688> (a

  • Thread name: When using Java.lang.Thread class to generate a thread, the thread will be named Thread-(Number), whereas when using java.util.concurrent.ThreadFactory class, it will be named pool-(number)-thread-(number).
  • Priority: Represents the priority of the threads.
  • Thread ID: Represents the unique ID for the threads. (Some useful information, including the CPU usage or memory usage of the thread, can be obtained by using thread ID.)
  • Thread status: Represents the status of the threads.
  • Thread callstack: Represents the call stack information of the threads.

So by analyzing a thread dump we can recognize the different thread statuses what we talked previously. Using this way we can identify deadlock situations and other useful information about the threads of your running process. Hope you got a clear idea about thread dump and the tools and commands using for taking a thread dump and analyze it. See you soon with another important topic. Thank You!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s