Synchronized锁
来看下0x00000007d616a0c8锁的执行过程:
一、线程WaitSet--locked--waiting on--in Object.wait()1 先运行:
"WaitSet--locked--waiting on--in Object.wait()1" prio=6 tid=0x000000000d9fb800 nid=0x2e3c in Object.wait() [0x000000000e76f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d616a0c8> (a java.lang.Boolean)
at java.lang.Object.wait(Object.java:503)
at com.zqz.test.threadstate.ThreadStateTest$Monitor_WaitSet.Monitor_WaitSet_run(ThreadStateTest.java:1525)
- locked <0x00000007d616a0c8> (a java.lang.Boolean)
at com.zqz.test.threadstate.ThreadStateTest$Monitor_WaitSet.run(ThreadStateTest.java:1517)
Locked ownable synchronizers:
- None
这个线程:
1. 执行- locked <0x00000007d616a0c8> (a java.lang.Boolean) 持有了锁;
2. 执行- waiting on <0x00000007d616a0c8> (a java.lang.Boolean) 执行了Object.wait()释放了锁,并进入到wait 队列;
二、线程”locked--sleeping--wait on Condition1“开始执行:
"locked--sleeping--wait on Condition1" prio=6 tid=0x000000000d9fc000 nid=0x142c waiting on condition [0x000000000e8bf000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.zqz.test.threadstate.ThreadStateTest$LockedSleeping.LockedSleeping_run(ThreadStateTest.java:1565)
- locked <0x00000007d616a0d8> (a java.lang.Object)
- locked <0x00000007d616a0c8> (a java.lang.Boolean)
at com.zqz.test.threadstate.ThreadStateTest$LockedSleeping.run(ThreadStateTest.java:1547)
Locked ownable synchronizers:
- None
三、线程“EntrySet--waiting to lock---Waiting for monitor entry2”开始执行
"EntrySet--waiting to lock---Waiting for monitor entry2" prio=6 tid=0x000000000da03000 nid=0x383c waiting for monitor entry [0x000000000ebcf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.zqz.test.threadstate.ThreadStateTest$Monitor_EntrySet.Monitor_EntrySet_run(ThreadStateTest.java:1505)
- waiting to lock <0x00000007d616a0c8> (a java.lang.Boolean)
at com.zqz.test.threadstate.ThreadStateTest$Monitor_EntrySet.run(ThreadStateTest.java:1492)
Locked ownable synchronizers:
- None
这个线程执行后发现锁已经被别的线程持有了,所以执行- waiting to lock <0x00000007d616a0c8> (a java.lang.Boolean) 进入到monitor entry队列,等待竞争锁;
ReentrantLock
"RTlock has Lock1" prio=6 tid=0x000000000da2c800 nid=0x3830 waiting on condition [0x000000000ed0f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.zqz.test.threadstate.ThreadStateTest$RTlockLocked.run(ThreadStateTest.java:1445)
Locked ownable synchronizers:
- <0x00000007d616a378> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
Locked ownable synchronizers: 代表当前线程持有了0x00000007d616a378锁
"RTlock wait Lock2" prio=6 tid=0x000000000da34000 nid=0xacc waiting on condition [0x000000000ee9f000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007d616a378> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:867)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1197)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:214)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:290)
at com.zqz.test.threadstate.ThreadStateTest$RTlockWaitLocke.run(ThreadStateTest.java:1470)
Locked ownable synchronizers:
- None
- parking to wait for <0x00000007d616a378> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
代表当前线程正在等待0x00000007d616a378锁
LockSupport.park()锁
"LockSupportPark2--park1" prio=6 tid=0x000000000d9f6800 nid=0x2a44 waiting on condition [0x000000000e66f000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007d616e988> (a com.zqz.test.threadstate.ThreadStateTest$LockSupportPark2)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at com.zqz.test.threadstate.ThreadStateTest$LockSupportPark2.park(ThreadStateTest.java:1610)
at com.zqz.test.threadstate.ThreadStateTest$LockSupportPark2.parkMethod(ThreadStateTest.java:1605)
at com.zqz.test.threadstate.ThreadStateTest$LockSupportPark2.run(ThreadStateTest.java:1602)
Locked ownable synchronizers:
- None
代表它执行LockSupport.park把自己给阻塞了,需要另外一个线程unpark这个线程才能继续执行;这种情况并没有其它线程持有锁