ReentrantLock和CountDownLatch
2021-07-05 14:17:26 14 举报
ReentrantLock分析
作者其他创作
大纲/内容
tail
ThreadB
shouldParkAfterFailedAcquire
thread=threadAwaitStatus=0->SIGNAL
waitStatus=SIGNAL
waitStatus=0->SIGINAL
next
竞争
AQS
ThreadC
CountDownLatch
没有获得锁的线程
prev
state=0exclusiveOwnerThread=Null
state=1--->0
thread=threadCwaitStatus=0
继续执行后续业务逻辑
Thread=nullwaitStatus=SIGNAL
head
threadA doAcquireSharedInterruptibly
Node
释放资源
thread=nullwaitStatus=0
Thread=ThreadCwaitStatus=0
NodewaitStatus=0
countDownLatch.await()
thread=threadBwaitStatus=0->SIGNAL
waitStatus=0
Thread=ThreadBwaitStatus=0
lock.lock();
唤醒
假如ThreadA独占
Thread=ThreadBwaitStatus=SIGNAL
lock.unlock()
ReentrantLock
state=1
state=0----->1exclusiveOwnerThread=ThreadA
Thread=ThreadDwaitStatus=0
修改state
ThreadA
ThreadB、ThreadD同时抢占锁,如果B失败,会自旋一次,如果仍然失败会被重新阻塞。如果成功,则D入队,则将所在结点设置为head
countDownLatch.countDown()
threadC doAcquireSharedInterruptibly
state=1exclusiveOwnerThread=ThreadB
0 条评论
下一页