三个实例演示 Java Thread Dump 日志分析
转自:http://www.cnblogs.com/zhengyun_ustc/archive/2013/01/06/dumpanalysis.html?jstack Dump 日志文件中的线程状态dump 文件里,值得关注的线程状态有:
下面我们先从第一个例子开始分析,然后再列出不同线程状态的含义以及注意事项,最后再补充两个实例。
综合示范一:Waiting to lock 和 Blocked实例如下:"RMI TCP Connection(267865)-172.16.5.25" daemon prio=10 tid=0x00007fd508371000 nid=0x55ae?waiting for monitor entry?[0x00007fd4f8684000]? ?java.lang.Thread.State:?BLOCKED (on object monitor)at org.apache.log4j.Category.callAppenders(Category.java:201)-?waiting to lock <0x00000000acf4d0c0>?(a org.apache.log4j.Logger)at org.apache.log4j.Category.forcedLog(Category.java:388)at org.apache.log4j.Category.log(Category.java:853)at org.apache.commons.logging.impl.Log4JLogger.warn(Log4JLogger.java:234)at com.tuan.core.common.lang.cache.remote.SpyMemcachedClient.get(SpyMemcachedClient.java:110)……1)线程状态是?Blocked,阻塞状态。说明线程等待资源超时!2)“?waiting to lock <0x00000000acf4d0c0>”指,线程在等待给这个?0x00000000acf4d0c0 地址上锁(英文可描述为:trying to obtain??0x00000000acf4d0c0?lock)。3)在 dump 日志里查找字符串?0x00000000acf4d0c0,发现有大量线程都在等待给这个地址上锁。如果能在日志里找到谁获得了这个锁(如locked <?0x00000000acf4d0c0?>),就可以顺藤摸瓜了。4)“waiting for monitor entry”说明此线程通过 synchronized(obj) {……} 申请进入了临界区,从而进入了下图1中的“Entry Set”队列,但该 obj 对应的 monitor 被其他线程拥有,所以本线程在 Entry Set 队列中等待。5)第一行里,"RMI TCP Connection(267865)-172.16.5.25"是?Thread Name?。tid指Java Thread id。nid指native线程的id。prio是线程优先级。[0x00007fd4f8684000]是线程栈起始地址。?Dump文件中的线程状态含义及注意事项含义如下所示:
图1 A Java Monitor
?
综合示范二:Waiting on condition?和?TIMED_WAITING实例如下:"RMI TCP Connection(idle)" daemon prio=10 tid=0x00007fd50834e800 nid=0x56b2?waiting on condition?[0x00007fd4f1a59000]? ?java.lang.Thread.State:?TIMED_WAITING (parking)at sun.misc.Unsafe.park(Native Method)-?parking to wait for ?<0x00000000acd84de8>?(a java.util.concurrent.SynchronousQueue$TransferStack)at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:198)at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:424)at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:323)at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:874)at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:945)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)at java.lang.Thread.run(Thread.java:662)1)“TIMED_WAITING (parking)”中的 timed_waiting 指等待状态,但这里指定了时间,到达指定的时间后自动退出等待状态;parking指线程处于挂起中。综合示范三:in Obejct.wait()?和?TIMED_WAITING
实例如下:"RMI RenewClean-[172.16.5.19:28475]" daemon prio=10 tid=0x0000000041428800 nid=0xb09?in Object.wait()?[0x00007f34f4bd0000]? ?java.lang.Thread.State:?TIMED_WAITING (on object monitor)at java.lang.Object.wait(Native Method)-?waiting on <0x00000000aa672478> (a java.lang.ref.ReferenceQueue$Lock)at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)-?locked <0x00000000aa672478> (a java.lang.ref.ReferenceQueue$Lock)at sun.rmi.transport.DGCClient$EndpointEntry$RenewCleanThread.run(DGCClient.java:516)at java.lang.Thread.run(Thread.java:662)1)“TIMED_WAITING (on object monitor)”,对于本例而言,是因为本线程调用了?java.lang.Object.wait(long timeout) 而进入等待状态。?
参考资源:1)CUBRID,2012,How to Analyze Java Thread Dumps;2)iteye,2012,虚拟机stack全分析;3)iteye,2008,如何分析Java虚拟机死锁;4)csdn,2012,java stack dump中JVM运行过程中产生的一些常见线程介绍和解释;5)2009,Java线程dump的分析;6)jiacheo,2012,tomcat thread dump 分析;