Blog

Introduction to Thread Dump

1. What is a Thread Dump

A Thread Dump is a snapshot of all threads in a Java application at a specific moment. It captures the current state information of all threads in the Java Virtual Machine (JVM). This is an important diagnostic tool for analyzing the operational status of Java applications, especially when encountering performance issues or deadlock situations.

A Thread Dump typically includes the following information:

2. How to Export Thread Dumps on Linux Systems

There are several methods to generate Thread Dumps for Java applications on Linux systems:

Using the jstack Tool

jstack is a command-line tool included with the JDK, specifically designed for generating thread dumps:

# First find the Java process PID
jps

# Use jstack to generate a thread dump
jstack <pid> > thread_dump.txt

If you need to capture multiple thread dumps to analyze an issue:

# Generate a thread dump every 5 seconds, 3 times
for i in 1 2 3; do jstack <pid> > thread_dump_$i.txt; sleep 5; done

Using the kill Command to Send a Signal

You can also generate a thread dump by sending a SIGQUIT signal to the Java process:

kill -3 <pid>

This will output the thread dump to the application's standard output (usually the console or log file).

Using the jcmd Tool

jcmd is another JDK tool with more comprehensive functionality:

jcmd <pid> Thread.print > thread_dump.txt

3. How to Use Thread Dumps to Solve Problems

Solution Approaches

Scenario 1: High CPU, High Load, Slow Response

Approach:

Scenario 2: Finding Threads Consuming the Most CPU

Approach:

Scenario 3: Low CPU Usage but Long Response Times

Approach:

Scenario 4: Requests Not Responding

Approach:

Case Studies

Case Study 1: Deadlock Analysis

Thread Dump characteristics of a deadlock issue:

Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x000000001d840878 (object 0x00000000eb4231d0, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x000000001d843208 (object 0x00000000eb4231e0, a java.lang.Object),
  which is held by "Thread-1"

Case Study 2: Connection Pool Exhaustion Analysis

When database or HTTP connection pools are exhausted, multiple threads will be in a WAITING state, waiting for connection resources:

"http-nio-8080-exec-10" #49 daemon prio=5 os_prio=0 tid=0x00007f3b14337000 nid=0x7f8c WAITING [0x00007f3aa9bfe000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000000c46ff608> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:478)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:349)
    at org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:134)

Case Study 3: Infinite Loop Causing High CPU Usage

Characteristics of high CPU usage caused by infinite loops or long calculations:

"high-cpu-thread" #23 prio=5 os_prio=0 tid=0x00007fa3c4173800 nid=0x1234 runnable [0x00007fa3bc5fc000]
   java.lang.Thread.State: RUNNABLE
    at com.example.HighCpuUsage.computeEndlessly(HighCpuUsage.java:42)
    at com.example.HighCpuUsage.process(HighCpuUsage.java:29)
    at com.example.HighCpuUsage.run(HighCpuUsage.java:18)

4. Thread Dump Analysis Best Practices

  1. Regular Sampling: Capture multiple Thread Dumps at intervals of a few seconds when problems occur
  2. Comparative Analysis: Compare multiple Thread Dumps to observe changes in thread states
  3. Focus on Hotspots: Pay special attention to threads in the RUNNABLE state, especially those that remain in the same method for a long time
  4. State Distribution: Analyze the distribution ratio of threads in different states to assess the overall health of the system
  5. Complementary Tools: Use system tools such as top, vmstat, etc., to gain a comprehensive understanding of system status
  6. Automated Analysis: Utilize professional tools like theadanalyzer.online, FastThread, IBM Thread Analyzer. to assist with analysis

By regularly capturing Thread Dumps and performing comparative analysis, you can more effectively diagnose intermittent issues and performance bottlenecks, thereby improving the stability and performance of Java applications.

← Back to Blog