ReentrantLock之AQS原理与lock()流程图
2019-12-30 09:43:36 3 举报
ReentrantLock源码流程图与AQS原理
作者其他创作
大纲/内容
否
signal()
AQS
条件等待
获取了state就代表加锁成功
NULL
state
head
返回true
volatile Node head;volatile Node tail;volatile int state;Thread exclusiveOwnerThread
Node
tryAcquire()
thread3
currentThread==exclusiveOwnerThread
state+1
head节点是获得锁的
阻塞自旋
添加到队尾释放锁资源
失败
是
addWaiter添加到队尾
acquiredQueued()
thread0 释放锁release()中LockSupport.unpark(thread1)thread1在自旋中通过CAS获取锁状态并将自己设为head,获得锁成功
tryAcquire()之nonfairTryAcquire()
acquireQueued
end
thread2
自旋
重入
firstWaiter
tail
thread1
成功
tryAcquire失败
await()
非公平锁是nonfairTryAcquire()
acquire
start
setHead()
thread0
nextWaiter
获得锁
设置当前线程exclusiveOwnerThread
当前线程是否拥有锁判断是否重入
返回false
acquire()
volatile Node prev;volatile Node next;volatile Thread thread;volatile int waitStatus;Node nextWaiter;
非公平锁lock()
Condition
thread4
lastWaiter
LockSupport.park()阻塞
唤醒
Condition复用Node内部类
node.prev==head且tryAcquire()成功
state==0
收藏
0 条评论
下一页