AQS最全源码解析
2021-06-04 15:54:26 0 举报
AQS逻辑图
作者其他创作
大纲/内容
模板方法模式(钩子函数)
new NonfairSync()
selfInterrupt()
true
new FairSync()
acquire方法详解
Node(Thread)
AQS_Source_Read.java
否
AQS结构
return turn
设置独占线程
AbstractQueuedSynchronizer.java
getState()==0
是
throw new Error(\"Maximum lock count exceeded\");
继承自
lock()方法解析
lock()
if (failed) cancelAcquire(node);
Thread
return true
State
p == head && tryAcquire(arg)
更新state值
等待队列
tryAcquire(int acquires)
lock.lock()
nextc < 0
return false
setExclusiveOwnerThread(Thread thread)
acquire(int arg)
FairSync extends Sync extends AbstractQueuedSynchronizer.java
prev
公平
boolean failed = true;boolean interrupted = false;
中断当前线程
共同路线
tryAcquire(arg)
tail
获得独占线程
ReentrantLock lock = new ReentrantLock(true);
setHead(node);p.next = null; // help GCfailed = false;
比较并设置state值
getExclusiveOwnerThread()
biz Code
ReentrantLock.java
更新state值setState(nextc)
Node p = node.predecessor()
final Thread current = Thread.currentThread()
判断当前线程是否已经存在于等待队列
int state=0 state>0
非公平
setState(int newState)
tryAcquire(int arg)
int nextc =state值加1
getState()
获取state的值
lock.unlock()
tryAquire方法详解
设置独占线程setExclusiveOwnerThread(current)
判断当前线程是否是独占线程current == getExclusiveOwnerThread()
acquire(1)
interrupted = true;
0 条评论
下一页