Thread dump 文件分析

1.相关代码

public class TestAwaitMain {

private static ReentrantLock  lock;

public static void main(String[] args) {

// TODO Auto-generated method stub

//1.线程A先持有锁

//  然后await

//在B中查询是否能获取锁

//如果能则说明 await()方法可以释放锁

lock=new ReentrantLock();

Condition condition = lock.newCondition();

//开辟两个线程

new Thread(new RunnableA(lock,condition),"t_A").start();

try {

  Thread.sleep(1000L);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

new Thread(new RunnableB(lock,condition),"t_B").start();

lock.lock();

System.out.println("线程"+Thread.currentThread().getName()+"Condition队列长度是  :"+lock.getWaitQueueLength(condition));

System.out.println("等待获取lock锁的"+"AQS队列长度是 :"+lock.getQueueLength());

}

}





RunnableA


public class RunnableA implements Runnable {

private ReentrantLock lock;

private Condition condition;

public RunnableA(ReentrantLock lock, Condition condition) {

super();

this.lock = lock;

this.condition = condition;

}

@Override

public void run() {


System.out.println("线程A运行");

    //先阻塞

lock.lock();

System.out.println("线程"+Thread.currentThread().getName()+"的lock方法已经执行");

try {

System.out.println("线程"+Thread.currentThread().getName()+"持有锁吗 ?:"+lock.isHeldByCurrentThread());

System.out.println("持有锁的线程数:"+ lock.getHoldCount());

try {

System.out.println("线程"+Thread.currentThread().getName()+"准备执行await()方法");

System.out.println("线程"+Thread.currentThread().getName()+"等待获取锁的队列长度"+lock.getQueueLength());

System.out.println("线程"+Thread.currentThread().getName()+"Condition队列长度"+lock.getWaitQueueLength(condition));

lock.hasWaiters(condition);

//condition.await();

//此时,如果不执行await操作

//看看AQS队列的长度

Thread.sleep(10000L);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} finally {

// TODO: handle finally clause

//lock.unlock();

}

}

}





RunnableB

public class RunnableB implements Runnable {

    private ReentrantLock lock;

private Condition condition;

public RunnableB(ReentrantLock lock, Condition condition) {

super();

this.lock = lock;

this.condition = condition;

}

@Override

public void run() {


System.out.println("线程B运行");

    //先阻塞

lock.lock();

System.out.println("线程"+Thread.currentThread().getName()+"的lock方法已经执行");

try {

System.out.println("线程"+Thread.currentThread().getName()+"持有锁吗 ?:"+lock.isHeldByCurrentThread());

System.out.println("持有锁的线程数:"+ lock.getHoldCount());

try {

System.out.println("线程"+Thread.currentThread().getName()+"准备执行await()方法");

System.out.println("线程"+Thread.currentThread().getName()+"等待获取锁的AQS队列长度是 :"+lock.getQueueLength());

System.out.println("线程"+Thread.currentThread().getName()+"Condition队列长度是  "+lock.getWaitQueueLength(condition));

//lock.hasWaiters(condition);

condition.await();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} finally {

// TODO: handle finally clause

lock.unlock();

}

}

}






2.打开jvisualVM生成thread dump



线程dump



全部文件内容

2018-09-18 14:18:43

Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode):

"JMX server connection timeout 21" #21 daemon prio=5 os_prio=0 tid=0x000000001b5dd000 nid=0x13b0 in Object.wait() [0x000000001e77f000]

  java.lang.Thread.State: TIMED_WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(Unknown Source)

- locked <0x00000000d8180db0> (a [I)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"RMI TCP Connection(idle)" #20 daemon prio=5 os_prio=0 tid=0x000000001c731000 nid=0x2348 waiting on condition [0x000000001e27e000]

  java.lang.Thread.State: TIMED_WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for  <0x00000000d8191040> (a java.util.concurrent.SynchronousQueue$TransferStack)

at java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)

at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(Unknown Source)

at java.util.concurrent.SynchronousQueue$TransferStack.transfer(Unknown Source)

at java.util.concurrent.SynchronousQueue.poll(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"RMI TCP Connection(7)-192.168.1.235" #19 daemon prio=5 os_prio=0 tid=0x000000001c4ed000 nid=0x153c runnable [0x000000001d1ae000]

  java.lang.Thread.State: RUNNABLE

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.socketRead(Unknown Source)

at java.net.SocketInputStream.read(Unknown Source)

at java.net.SocketInputStream.read(Unknown Source)

at java.io.BufferedInputStream.fill(Unknown Source)

at java.io.BufferedInputStream.read(Unknown Source)

- locked <0x00000000d8190e50> (a java.io.BufferedInputStream)

at java.io.FilterInputStream.read(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$3/1038727162.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- <0x00000000d8190fe0> (a java.util.concurrent.ThreadPoolExecutor$Worker)

"RMI TCP Connection(6)-192.168.1.235" #17 daemon prio=5 os_prio=0 tid=0x000000001c4d2800 nid=0x1618 runnable [0x000000001cfad000]

  java.lang.Thread.State: RUNNABLE

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.socketRead(Unknown Source)

at java.net.SocketInputStream.read(Unknown Source)

at java.net.SocketInputStream.read(Unknown Source)

at java.io.BufferedInputStream.fill(Unknown Source)

at java.io.BufferedInputStream.read(Unknown Source)

- locked <0x00000000d81b0278> (a java.io.BufferedInputStream)

at java.io.FilterInputStream.read(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$3/1038727162.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- <0x00000000d8191170> (a java.util.concurrent.ThreadPoolExecutor$Worker)

"JMX server connection timeout 16" #16 daemon prio=5 os_prio=0 tid=0x000000001c4bf000 nid=0x2b8c in Object.wait() [0x000000001ceaf000]

  java.lang.Thread.State: TIMED_WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(Unknown Source)

- locked <0x00000000d81be188> (a [I)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"RMI Scheduler(0)" #15 daemon prio=5 os_prio=0 tid=0x000000001c4be800 nid=0x1354 waiting on condition [0x000000001cdaf000]

  java.lang.Thread.State: TIMED_WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for  <0x00000000d81be320> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)

at java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)

at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"RMI TCP Accept-0" #13 daemon prio=5 os_prio=0 tid=0x000000001c422000 nid=0xdf4 runnable [0x000000001c98f000]

  java.lang.Thread.State: RUNNABLE

at java.net.DualStackPlainSocketImpl.accept0(Native Method)

at java.net.DualStackPlainSocketImpl.socketAccept(Unknown Source)

at java.net.AbstractPlainSocketImpl.accept(Unknown Source)

at java.net.PlainSocketImpl.accept(Unknown Source)

- locked <0x00000000d81958a0> (a java.net.SocksSocketImpl)

at java.net.ServerSocket.implAccept(Unknown Source)

at java.net.ServerSocket.accept(Unknown Source)

at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"t_B" #11 prio=5 os_prio=0 tid=0x000000001b4ad000 nid=0x1a3c waiting on condition [0x000000001c08e000]

  java.lang.Thread.State: WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for  <0x00000000d8198268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)

at java.util.concurrent.locks.LockSupport.park(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(Unknown Source)

at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(Unknown Source)

at java.util.concurrent.locks.ReentrantLock.lock(Unknown Source)

at com.concurrent.testawait.RunnableB.run(RunnableB.java:29)

at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x000000001b458800 nid=0x126c runnable [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

  Locked ownable synchronizers:

- None

"C1 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000001b3dd800 nid=0xdb0 waiting on condition [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

  Locked ownable synchronizers:

- None

"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x000000001b3dd000 nid=0x1028 waiting on condition [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

  Locked ownable synchronizers:

- None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000001b3da800 nid=0x1f4 waiting on condition [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

  Locked ownable synchronizers:

- None

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001b3d7000 nid=0xaac waiting on condition [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

  Locked ownable synchronizers:

- None

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001b3d3800 nid=0x2d84 runnable [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

  Locked ownable synchronizers:

- None

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000199b1800 nid=0x750 in Object.wait() [0x000000001b32f000]

  java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x00000000d81bee58> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(Unknown Source)

- locked <0x00000000d81bee58> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(Unknown Source)

at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

  Locked ownable synchronizers:

- None

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000004cb6000 nid=0x2dd4 in Object.wait() [0x000000001b22e000]

  java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x00000000d81b06e0> (a java.lang.ref.Reference$Lock)

at java.lang.Object.wait(Unknown Source)

at java.lang.ref.Reference.tryHandlePending(Unknown Source)

- locked <0x00000000d81b06e0> (a java.lang.ref.Reference$Lock)

at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)

  Locked ownable synchronizers:

- None

"main" #1 prio=5 os_prio=0 tid=0x0000000004bc4000 nid=0x2c94 waiting on condition [0x000000000480f000]

  java.lang.Thread.State: WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for  <0x00000000d8198268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)

at java.util.concurrent.locks.LockSupport.park(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(Unknown Source)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(Unknown Source)

at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(Unknown Source)

at java.util.concurrent.locks.ReentrantLock.lock(Unknown Source)

at com.concurrent.testawait.TestAwaitMain.main(TestAwaitMain.java:27)

  Locked ownable synchronizers:

- None

"VM Thread" os_prio=2 tid=0x0000000019987800 nid=0x1c8c runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000004bda800 nid=0x18c8 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000004bdc800 nid=0x170c runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000004bde000 nid=0x484 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000004be0800 nid=0x1e84 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000004be2800 nid=0x1348 runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000004be3800 nid=0x10b0 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001b49c800 nid=0x2f80 waiting on condition

JNI global references: 228






3.核心内容


"t_B" #11 prio=5 os_prio=0 tid=0x000000001b4ad000 nid=0x1a3c waiting on condition [0x000000001c08e000]

  java.lang.Thread.State: WAITING (parking)

        at sun.misc.Unsafe.park(Native Method)

        - parking to wait for  <0x00000000d8198268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)

        at java.util.concurrent.locks.LockSupport.park(Unknown Source)

        at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)

        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(Unknown Source)

        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(Unknown Source)

        at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(Unknown Source)

        at java.util.concurrent.locks.ReentrantLock.lock(Unknown Source)

        at com.concurrent.testawait.RunnableB.run(RunnableB.java:29)

        at java.lang.Thread.run(Unknown Source)

  Locked ownable synchronizers:

        - None




"main" #1 prio=5 os_prio=0 tid=0x0000000004bc4000 nid=0x2c94 waiting on condition [0x000000000480f000]

  java.lang.Thread.State: WAITING (parking)

        at sun.misc.Unsafe.park(Native Method)

        - parking to wait for  <0x00000000d8198268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)

        at java.util.concurrent.locks.LockSupport.park(Unknown Source)

        at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)

        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(Unknown Source)

        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(Unknown Source)

        at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(Unknown Source)

        at java.util.concurrent.locks.ReentrantLock.lock(Unknown Source)

        at com.concurrent.testawait.TestAwaitMain.main(TestAwaitMain.java:27)

  Locked ownable synchronizers:

        - None




3.1 进行分析



t_B :这是线程名称

prio=5 : 代表该线程的优先级

tid    : thread id  即:线程id

nid    :  native  thread id  即: 本地线程id

java.lang.Thread.State: WAITING (parking)  : 表明线程目前的状态

    - parking to wait for  <0x00000000d8198268>  :表明该线程处于Waiting状态,其正在等待获取0x00000000d8198268锁

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容