SoFunction
Updated on 2025-04-23

One article will take you to analyze Java application thread dump in-depth

introduction

During the Java application operation and maintenance and problem investigation, Thread Dump is a very important tool. It can help us understand the running status of JVM threads and quickly locate deadlocks, thread blocking, resource competition and other problems. This article will use an actual thread dump log case to analyze its content in detail, and combine code examples to explain how to discover problems and optimize application performance.

1. What is thread dump

A thread dump is a snapshot of all threads of the JVM at a certain moment, including the call stack, status and lock information of each thread. By analyzing thread dumps, we can:

  • Check whether the thread is blocked or deadlocked.
  • Discover resource competition issues.
  • Optimize thread pool configuration.
  • Diagnostics the cause of slow response or crash of the application.

How to get thread dump?

Use the jstack command (for running Java processes):

jstack -l <pid> > thread_dump.log

Send signals via kill -3 (for Linux environment):

kill -3 <pid>

Use JMX tools (such as VisualVM, JConsole).

2. Case log analysis

The log fragments we analyzed are as follows:

2025-04-22 18:16:40
Full thread dump OpenJDK 64-Bit Server VM (25.362-b08 mixed mode):

"SIGTERM handler" #138 daemon prio=9 os_prio=0 tid=0x00007f03fc005000 nid=0xa9b06 runnable [0x00007f0438dfc000]
   : RUNNABLE
    at (:749)

"Druid-ConnectionPool-Destroy-816944408" #137 daemon prio=5 os_prio=0 tid=0x00007f0364222000 nid=0xa9aff waiting on condition [0x00007f04385fc000]
   : TIMED_WAITING (sleeping)
    at (Native Method)
    at $(:2786)
...

Key thread analysis

(1) SIGTERM handler thread

Status: RUNNABLE

Function: Process the JVM termination signal (such as kill -15), indicating that the application is closing.

Possible problems: If it is an unexpected shutdown, check whether there is an abnormal termination or OOM.

(2) Druid connection pool thread

Druid-ConnectionPool-Destroy-*

  • Status: TIMED_WAITING (sleeping)
  • Function: Destroy idle database connections.
  • Optimization suggestions: Adjust the timeBetweenEvictionRunsMillis parameter to avoid frequent destruction.

Druid-ConnectionPool-Create-*

  • Status: WAITING (parking)
  • Function: Create a new database connection.
  • Potential problem: If you block for a long time, it may be that the connection pool is exhausted, you need to check the maxActive configuration.

(3) Nacos client thread

nacos-grpc-client-executor-*

Status: TIMED_WAITING (parking)

Function: The Nacos client communicates with the server through gRPC.

Troubleshooting points: If a large number of threads are blocked, it may be that the Nacos server is unreachable or network problems.

3. Frequently Asked Questions and Solutions

3.1 Thread deadlock

Sample code:

public class DeadlockExample {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock1) {
                try { (100); } catch (InterruptedException e) {}
                synchronized (lock2) {
                    ("Thread 1");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (lock2) {
                synchronized (lock1) {
                    ("Thread 2");
                }
            }
        }).start();
    }
}

Deadlock performance in thread dump:

Found one Java-level deadlock:
=============================
Thread 1:
  waiting to lock monitor 0x00007f03fc005000 (object 0x0000000749b623a0, a ),
  which is held by Thread 2

Thread 2:
  waiting to lock monitor 0x00007f03fc005100 (object 0x0000000749b623b0, a ),
  which is held by Thread 1

Solution:

  • Use jstack to detect deadlocks.
  • Adjust the lock sequence to avoid circular dependencies.

3.2 Thread pool exhausted

Sample code:

ExecutorService executor = (2);
for (int i = 0; i < 10; i++) {
    (() -> {
        try { (1000); } catch (InterruptedException e) {}
    });
}

Thread dump performance:

"pool-1-thread-1" #12 prio=5 os_prio=0 tid=0x00007f03fc005000 nid=0xa9b06 waiting on condition [0x00007f0438dfc000]
   : WAITING (parking)

Solution:

  • Increase the thread pool size or dynamically adjust it using ThreadPoolExecutor.
  • Use bounded queues to avoid OOM.

3.3 Database connection leak

Druid configuration optimization:

spring:
  datasource:
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1
      test-while-idle: true

Monitor SQL leaks:

// Close the connection explicitly in the codetry (Connection conn = ()) {
    // SQL Operation} // Automatically close

4. Thread dump analysis tool

VisualVM (visual analysis of thread state)

FastThread (online analysis tool)

Eclipse MAT (analyzing thread reference relationships)

5. Summary

By analyzing thread dumps, we can:

  • Deadlock, thread blocking and other problems were found.
  • Optimize thread pool and database connection pool configuration.
  • Diagnostics the cause of application crashes or performance degradation.

Best Practices:

  • Collect thread dumps regularly (especially when application is stuck).
  • Combined with logs and monitoring (such as Prometheus + Grafana) comprehensive analysis.
  • Use automation tools such as Arthas for dynamic diagnosis.

This is the article about this article that will introduce you to the in-depth analysis of Java application thread dumps. For more related Java thread dump content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!