jstack dump日志文件中的线程状态
dump 文件里,值得关注的线程状态有:
- 死锁,Deadlock(重点关注)
- 执行中,Runnable
- 等待资源,Waiting on condition(重点关注)
- 等待获取监视器,Waiting on monitor entry(重点关注)
- 暂停,Suspended
- 对象等待中,Object.wait() 或 TIMED_WAITING
- 阻塞,Blocked(重点关注)
- 停止,Parked
1、线程状态为"waiting for monitor entry"
表示该线程等待进入一个临界区,故它在“Entry Set”队列中等待,
此时线程的状态一般是Blocked.
java.lang.Thread.State: BLOCKED(on object monitor)
线程阻塞,是指当前线程执行过程中,所需要的资源长时间等待却一直未能获取到,被容器的线程管理器标识为阻塞状态,可以理解为等待资源超时的线程。
示例:
下面这个线程在等待这个锁 0x00000000fe7e3b50,等待进入临界区:
"RMI TCP Connection(64896)-172.16.52.118" daemon prio=10 tid=0x00000000405a6000 nid=0x68fe
waiting for monitor entry [0x00007f2be65a3000]
java.lang.Thread.State:
BLOCKED (on object monitor)
at com.xyz.goods.service.impl.GoodsServiceImpl.
findChanellGoodsCountWithCache(GoodsServiceImpl.java:1734)
-
waiting to lock <0x00000000fe7e3b50> (a java.lang.String)
那么谁持有这个锁呢?
是另一个先调用了 findChanellGoodsCountWithCache 函数的线程:
"RMI TCP Connection(64878)-172.16.52.117" daemon prio=10 tid=0x0000000040822000 nid=0x6841 runnable [0x00007f2be76b3000]
java.lang.Thread.State:
RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
- locked <0x00000000af4ed638> (a java.io.BufferedInputStream)
at org.bson.io.Bits.readFully(Bits.java:35)
at org.bson.io.Bits.readFully(Bits.java:28)
at com.mongodb.Response.<init>(Response.java:35)
at com.mongodb.DBPort.go(DBPort.java:110)
- locked <0x00000000af442d48> (a com.mongodb.DBPort)
at com.mongodb.DBPort.go(DBPort.java:75)
- locked <0x00000000af442d48> (a com.mongodb.DBPort)
at com.mongodb.DBPort.call(DBPort.java:65)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:202)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:296)
at com.mongodb.DB.command(DB.java:152)
at com.mongodb.DBCollection.getCount(DBCollection.java:760)
at com.mongodb.DBCollection.getCount(DBCollection.java:731)
at com.mongodb.DBCollection.count(DBCollection.java:697)
at com.xyz.goods.manager.MongodbManager.count(MongodbManager.java:202)
at com.xyz.goods.service.impl.GoodsServiceImpl.findChanellGoodsCount(GoodsServiceImpl.java:1787)
at com.xyz.goods.service.impl.GoodsServiceImpl.
findChanellGoodsCountWithCache(GoodsServiceImpl.java:1739)
-
locked <0x00000000fe7e3b50> (a java.lang.String)
2、线程状态为"waiting on condition"
表明它是在等待另一个条件的发生来唤醒自己,或者是调用了Thread.sleep(xx)方法
此时线程的状态一般为:
"Thread-9" daemon prio=6 tid=0x0be0c800 nid=0x774 waiting on condition [0x0cf6f000..0x0cf6fce8]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
示例:
等待另一个条件发生来将自己唤醒:
"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指线程处于挂起中。
2)“
waiting on condition”需要与堆栈中的“
parking to wait for <0x00000000acd84de8>
(a java.util.concurrent.SynchronousQueue$TransferStack)” 结合来看。首先,本线程肯定是在等待某个条件的发生,来把自己唤醒。其次,SynchronousQueue 并不是一个队列,只是线程之间移交信息的机制,当我们把一个元素放入到 SynchronousQueue 中时必须有另一个线程正在等待接受移交的任务,因此这就是本线程在等待的条件。
示例:
"RMI RenewClean-[172.16.50.182:4888]" daemon prio=10 tid=0x0000000040d2c800 nid=0x97e
in Object.wait() [0x00007f9ccafd0000]
java.lang.Thread.State:
TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000799b032d8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
- locked <0x0000000799b032d8> (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)
3、线程状态为"in Object.wait()"
这种情况表示:该线程获取了monitor之后,又调用了Object.wait()方法。
每个monitor在某个时刻,只能被一个"Active Thread"拥有,而其他的线程都是"waiting Thread",这些等待线程被分类在两个队列"Entry set"和"Wait set"中等待。在"Entry set"中等待的Thread处于"Wait for monitor entry"而"Wait set "中等待的Thread处于"in Object.wait()"
当线程获得Monitor,如果发现继续运行的条件无法满足,它将调用对象(一般是被synchronized的对象)的wait()方法,放弃monitor,进入"Wait set"。
此时的Thread状态大致处于一下几种状态:例如
"http-8080-2" daemon prio=6 tid=0x0c618000 nid=0xdc0 in Object.wait() [0x0cf1f000..0x0cf1fd68]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x041c6df0> (a org.apache.tomcat.util.net.JIoEndpoint$Worker)
at java.lang.Object.wait(Object.java:485)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.await(JIoEndpoint.java:458)
- locked <0x041c6df0> (a org.apache.tomcat.util.net.JIoEndpoint$Worker)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:484)
at java.lang.Thread.run(Thread.java:619)
Locked ownable synchronizers:
- None
"TP-Monitor" daemon prio=6 tid=0x0c197800 nid=0xd2c in Object.wait() [0x0cecf000..0x0cecf9e8]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x041a6dd0> (a org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable)
at org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable.run(ThreadPool.java:565)
- locked <0x041a6dd0> (a org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable)
at java.lang.Thread.run(Thread.java:619)
Locked ownable synchronizers:
- None
一般都是RMI相关线程(RMI RenewClean、GC Daemon、RMI Reaper)、GC线程(Finalizer)、引用对象垃圾回收线程(Reference Handler)等系统线程处于这种状态。
如图Java Monitor
问题分析:
大量线程状态为"waiting for monitor entry"
导致这种现象的原因可能是全局锁阻塞了大量的线程
如果Thread dump中显示处于这种"waiting for monitor entry"状态的线程越来越多的话,则说明某个线程处于某个临界区的事件过长,导致其他的线程迟迟无法进入该临界区。
大量线程状态为"waiting on condition"
导致这种情况的原因可能是 应用获取第三方的资源时迟迟无法得到其Response,导致大量的线程处于 等待状态。从线程的堆栈观察,如果是在等待网络读写,则这可能是网络瓶颈的征兆,网络读写阻塞导致 线程迟迟无法执行。
综合示范一: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) {……} 申请进入了临界区,从而进入了上图中的“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]是线程栈起始地址。
综合示范二: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) 而进入等待状态。
2)“Wait Set”中等待的线程状态就是“
in Object.wait() ”。当线程获得了 Monitor,进入了临界区之后,如果发现线程继续运行的条件没有满足,它则调用对象(一般就是被 synchronized 的对象)的 wait() 方法,放弃了 Monitor,进入 “Wait Set”队列。只有当别的线程在该对象上调用了 notify() 或者 notifyAll() ,“ Wait Set”队列中线程才得到机会去竞争,但是只有一个线程获得对象的 Monitor,恢复到运行态。
3)RMI RenewClean 是 DGCClient 的一部分。
DGC 指的是 Distributed GC,即分布式垃圾回收。
4)请注意,是先
locked <0x00000000aa672478>,后
waiting on <0x00000000aa672478>,之所以先锁再等同一个对象,请看下面它的代码实现:
static private class Lock { };
private Lock lock = new Lock();
public Reference<? extends T>
remove(long timeout)
{
synchronized (lock) {
if (
r !=
null)
return
r;
for (;;) {
……
}
}
即,线程的执行中,先用 synchronized 获得了这个对象的 Monitor(对应于
locked <0x00000000aa672478> );当执行到 lock.wait(timeout);,线程就放弃了 Monitor 的所有权,进入“Wait Set”队列(对应于
waiting on <0x00000000aa672478> )。
5)从堆栈信息看,是正在清理 remote references to remote objects ,引用的租约到了,分布式垃圾回收在逐一清理呢。