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
全部文件内容
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锁