ReentrantLock流程图
2023-05-17 18:13:21 0 举报
ReentrantLock流程图
作者其他创作
大纲/内容
thread_2同理
thead = nullnextWaiter = nullwaitStatus = 0
prev
先cas操作,把头结点waitStatus的状态修改成0在唤醒LockSupport.unpark(s.thread)唤醒线程thread_1
node.predecessor:获取当前node的prev,也就是thread1
thead = thread_1nextWaiter = nullwaitStatus = -1
next
thead = nullnextWaiter = nullwaitStatus = -1
thread_1在执行过程与thread_0拿到锁类似
enq(node)
thead = thread_1nextWaiter = nullwaitStatus = 0
thread_2
thead = thread_2nextWaiter = nullwaitStatus = 0
setExclusiveOwnerThread把拥有锁的线程设置为当前线程,用于重入锁的判断
thread_0
getState-releases = 1-1 = 0setExclusiveOwnerThread把当前用来判断重入锁的线程清空setState把状态设置成0,表示其他线程可以抢夺锁资源这里返回true
执行acquire(1)方法
先调用tryRelease(arg)
调用unparkSuccessor(h)
唤醒thread_1
执行业务逻辑
tryAcquire方法
解锁
thread_1
唤醒thread_2
node.predecessor:获取当前node的prev

收藏

收藏
0 条评论
下一页