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 java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
– locked <0x0000000780b7e688> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
– locked <0x0000000780b7e688> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
)

  • 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!

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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