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();
收藏
收藏
0 条评论
下一页