在Java应用运维和问题排查过程中,线程转储(Thread Dump)是一个非常重要的工具,它能够帮助我们了解JVM内部线程的运行状态,快速定位死锁、线程阻塞、资源竞争等问题。本文将通过一个实际的线程转储日志案例,详细分析其内容,并结合代码示例,讲解如何从中发现问题并优化应用性能。
线程转储是JVM在某一时刻所有线程的快照,包含每个线程的调用栈、状态和锁信息。通过分析线程转储,我们可以:
使用 jstack 命令(适用于运行中的Java进程):
jstack -l <pid> > thread_dump.log通过 kill -3 发送信号(适用于Linux环境):
kill -3 <pid>使用JMX工具(如VisualVM、JConsole)。
我们分析的日志片段如下:
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]
java.lang.Thread.State: RUNNABLE
at java.lang.Thread.run(Thread.java:749)
"Druid-ConnectionPool-Destroy-816944408" #137 daemon prio=5 os_prio=0 tid=0x00007f0364222000 nid=0xa9aff waiting on condition [0x00007f04385fc000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.alibaba.druid.pool.DruidDataSource$DestroyConnectionThread.run(DruidDataSource.java:2786)
...SIGTERM handler 线程RUNNABLEkill -15),表明应用正在关闭。Druid-ConnectionPool-Destroy-*
TIMED_WAITING (sleeping)timeBetweenEvictionRunsMillis 参数,避免频繁销毁。Druid-ConnectionPool-Create-*
WAITING (parking)maxActive 配置。nacos-grpc-client-executor-* TIMED_WAITING (parking)示例代码:
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 { Thread.sleep(100); } catch (InterruptedException e) {}
synchronized (lock2) {
System.out.println("Thread 1");
}
}
}).start();
new Thread(() -> {
synchronized (lock2) {
synchronized (lock1) {
System.out.println("Thread 2");
}
}
}).start();
}
}线程转储中的死锁表现:
Found one Java-level deadlock:
=============================
Thread 1:
waiting to lock monitor 0x00007f03fc005000 (object 0x0000000749b623a0, a java.lang.Object),
which is held by Thread 2
Thread 2:
waiting to lock monitor 0x00007f03fc005100 (object 0x0000000749b623b0, a java.lang.Object),
which is held by Thread 1解决方案:
jstack 检测死锁。示例代码:
ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; i++) {
executor.submit(() -> {
try { Thread.sleep(1000); } catch (InterruptedException e) {}
});
}线程转储表现:
"pool-1-thread-1" #12 prio=5 os_prio=0 tid=0x00007f03fc005000 nid=0xa9b06 waiting on condition [0x00007f0438dfc000]
java.lang.Thread.State: WAITING (parking)解决方案:
ThreadPoolExecutor 动态调整。Druid 配置优化:
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监控SQL泄漏:
// 在代码中显式关闭连接
try (Connection conn = dataSource.getConnection()) {
// SQL操作
} // 自动关闭通过分析线程转储,我们可以:
最佳实践:
通过本文的讲解,希望读者能够掌握线程转储的分析方法,并在实际运维中快速定位问题,提升Java应用的稳定性! 🚀