AQS中的acquire方法解释,
首先调用 tryAcquire方法判断当前线程是否拿到锁,没有拿到的话执行addWaiter方法把失败的这个线程打包成一个节点,通过循环CAS机制强行加入到队列的尾部,该方法的返回值会返回这个节点,
再打包交给acquireQueued方法,acquireQueued方法会再次自旋尝试拿锁,如果尝试成功则首先找到前一个节点,也就是从锁上下来的节点,并将其脱离队列,
如果获取锁又失败则进入shouldParkAfterFailedAcquire方法,该方法用来判断失败后的线程应不应该被阻塞,如果需要则阻塞自己,等待当其他线程释放锁并将其唤醒
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
AQS中的release方法解释,首先找到头节点,
public final boolean release(int arg) {
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
执行unparkSuccessor方法,该方法表示找到头节点后面一个节点,也就是后继节点,对这个节点做一个判断,判断其是否注意一个cancel状态,如果不是则这个后继节点又会把头节点指为自己,如果是cancel状态的话就不从头开始找了,这时候会从队列的尾巴开始找,如果找到了就执行 unpark方法来唤醒找到的这个线程,被唤醒的线程重复之前的流程进行自旋拿锁
总结就是拿到了就拿,拿不到继续自旋拿锁
private void unparkSuccessor(Node node) {
/*
* If status is negative (i.e., possibly needing signal) try
* to clear in anticipation of signalling. It is OK if this
* fails or if status is changed by waiting thread.
*/
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
/*
* Thread to unpark is held in successor, which is normally
* just the next node. But if cancelled or apparently null,
* traverse backwards from tail to find the actual
* non-cancelled successor.
*/
Node s = node.next;
if (s == null || s.waitStatus > 0) {
s = null;
for (Node t = tail; t != null && t != node; t = t.prev)
if (t.waitStatus <= 0)
s = t;
}
if (s != null)
LockSupport.unpark(s.thread);
}