ReentrantLock Lock
2021-02-27 15:53:22 1 举报
登录查看完整内容
ReentrantLock lock方法流程
作者其他创作
大纲/内容
N
Y
(prev=tail)!=null判断是否队列是否存在数据
pred.next = node;
tryAcquire(1)尝试获取锁
Thread.currentThread().interrupt();
compareAndSetHead(new Node())
pred.waitStatus > 0
pred.next = node;将原来的尾节点的next指向此节点
setHead(node); p.next = null; // help GC failed = false;
int ws = pred.waitStatus;
return true
return interrupted
return false
state=state+acquires信号量加1,重入锁
非公平锁没有这一步
finallyfailed
tryAcquire(int arg)
ws == Node.SIGNAL
node.prev = tail当前节点prev指向尾节点
setExclusiveOwnerThread(current)将获取锁的线程设置为此线程
end
interrupted = true;
node.prev = pred = pred.prev;
cancelAcquire(node)
return node;
Node p = node.predecessor();node的前一个节点,若前一个节点为空抛空指针异常
enq(node);入队CLH
boolean interrupted = false;
node.prev = t;
parkAndCheckInterrupt())休眠此进程返回此线程是否处于中断,并重置中断状态
p==head
t.next = node;
enq(node);入队
t == null
state==0判断此时是否有线程占有锁
tryAcquire(arg)
current == getExclusiveOwnerThread()判断当前获取锁的线程是否是此线程
ws > 0
return t
addWaiter(Node.EXCLUSIVE)将当前线程加入CLH队列
hasQueuedPredecessors()队列中是否存在线程排队,公平锁空队列返回false第二个节点为null或者第二个节点为当前线程返回true
tail = head;
Node t = tail;
ReentrantLock.lock() sync.lock();
![ReentrantLock Lock](https://www.processon.com/chart_image/template/thumb/60350e35e401fd0290f99fcb.png?tid=60350e35e401fd0290f99fc9)
收藏
![ReentrantLock unlock](https://www.processon.com/chart_image/template/thumb/6039fafef346fb114900557d.png?tid=6039fafef346fb114900557b)
收藏
0 条评论
回复 删除
下一页