三个实例演示 Java Thread Dump 日志分析
时间: 2023-07-31 09:04:32 浏览: 117
好的,下面给出三个简单的 Java Thread Dump 日志分析实例:
1. 查看占用 CPU 最高的线程
通过查看 Thread Dump 日志中的 CPU 占用时间,可以快速定位占用 CPU 最高的线程。例如:
```
"pool-1-thread-3" #9 prio=5 os_prio=0 tid=0x00007fbb481ac800 nid=0x2a4f runnable [0x00007fbb4c76c000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x00000000f3c0b3e8> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x00000000f3c0b3e8> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.example.MyClass.myMethod(MyClass.java:123)
```
上述例子中的线程名为 "pool-1-thread-3" ,其 CPU 占用时间状态为 RUNNABLE,同时该线程正在等待从 Socket 中读取数据,且当前线程被阻塞(locked)在一个 InputStreamReader 对象上。因此可以认为该线程正在占用大量 CPU 资源,需要进行优化。
2. 查看线程死锁
通过查看 Thread Dump 日志中线程的锁定情况,可以快速定位线程死锁。例如:
```
"Thread-1" #9 prio=5 os_prio=0 tid=0x000000001fdd9800 nid=0x3368 waiting for monitor entry [0x000000001f3ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.example.MyClass.myMethod(MyClass.java:123)
- waiting to lock <0x00000000f3c0b3e8> (a java.lang.Object)
at com.example.MyClass.myOtherMethod(MyClass.java:456)
- locked <0x00000000f3c0b3e8> (a java.lang.Object)
"Thread-2" #10 prio=5 os_prio=0 tid=0x000000001fdd9800 nid=0x3369 waiting for monitor entry [0x000000001f4ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.example.MyClass.myOtherMethod(MyClass.java:456)
- waiting to lock <0x00000000f3c0b3e8> (a java.lang.Object)
at com.example.MyClass.myMethod(MyClass.java:123)
- locked <0x00000000f3c0b3e8> (a java.lang.Object)
```
上述例子中的两个线程分别为 "Thread-1" 和 "Thread-2",它们都在等待获取同一个对象的锁(<0x00000000f3c0b3e8>),造成了死锁。需要根据具体情况解除死锁。
3. 查看等待线程数目
通过查看 Thread Dump 日志中的等待线程数目,可以快速定位是否存在线程等待过多的情况。例如:
```
"pool-1-thread-1" #7 prio=5 os_prio=0 tid=0x0000000028ac4000 nid=0x2f2c waiting on condition [0x0000000029b8f000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f3c0b3e8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lockInterruptibly(ReentrantReadWriteLock.java:727)
at com.example.MyClass.myMethod(MyClass.java:123)
"pool-1-thread-2" #8 prio=5 os_prio=0 tid=0x0000000028ac5000 nid=0x2f2d waiting on condition [0x0000000029c8f000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f3c0b3e8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lockInterruptibly(ReentrantReadWriteLock.java:727)
at com.example.MyClass.myOtherMethod(MyClass.java:456)
```
上述例子中的两个线程分别为 "pool-1-thread-1" 和 "pool-1-thread-2",它们都在等待获取同一个 ReentrantReadWriteLock 锁,但是由于其他线程占用了该锁,因此它们都进入了 WAITING 状态。如果等待线程数目过多,可能会导致应用程序性能下降,需要进行优化。
阅读全文