head节点一定是拿到锁的节点 head后继的尝试获取资源的节点 其他节点都是阻塞的
回顾简单的入队方法
//创建节点 和waitting模式
//cas快速添加到队尾 失败进入enq循环添加
private Node addWaiter(Node mode) {
....
enq(node);
....
}
//入队操作
private Node enq(final Node node) {
for (;;) {
if (t == null) {
//cas head
} else {
//cas tail
}
}
}
释放锁
//释放独占锁
public final boolean release(int arg) {
if (tryRelease(arg)) { //子类决定怎么释放
Node h = head;
//判断头结点状态
//头结点不可能为空 独占模式只能是0或-1 节点在阻塞时会将前一个节点设置为-1(SIGNAL) 以此来等待前一个节点唤醒自己
//这里就是使用这个状态的关键点 如果为-1 那就是有后续节点需要唤醒
//waitStatus volatile字段 线程可见 后面还是要用cas修改 因为volatile操作非原子性
if (h != null && h.waitStatus != 0)
// 同一时间只会有一个线程进入此方法 因为是独占
unparkSuccessor(h); 唤醒后续节点
return true;
}
return false;
}
//释放共享锁 唤醒后续节点 并且保证向后唤醒的传递 独占模式的话只唤醒头结点的后继节点
private void doReleaseShared() {
for (;;) {
Node h = head;
if (h != null && h != tail) {
int ws = h.waitStatus;
//尝试唤醒后续节点 多线程 cas失败就loop 在check一下
if (ws == Node.SIGNAL) {
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
unparkSuccessor(h);
}
//尝试设置 头结点状态PROPAGATE 让
else if (ws == 0 &&
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
if (h == head) // loop if head changed
break;
}
}
//唤醒后续节点
private void unparkSuccessor(Node node) {
int ws = node.waitStatus;
if (ws < 0)
compareAndSetWaitStatus(node, ws, 0);
//唤醒后续节点 如果为null 倒叙向前查找可唤醒节点 不断向前覆盖
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);
}
唤醒之后 回到加锁部分
//独占模式 线程进入队列后在这里阻塞等待被唤醒获取资源
// 独占模式下 通过唤醒 和异常返回此方法
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
//unpark 从阻塞中返回的一定会进入到此方法 因为独占模式的情况下 只能被头结点唤醒
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // 锁被多线程使用 所以部分情况下锁的生命周期大于一些线程run执行完 如果还有引用 就不会回收Thread对象 (这里是猜测
failed = false; //此时failded 为false 表示非异常退出 后面finally里面不需要将节点设置取消状态
return interrupted;
}
//加锁的时候进入这里 阻塞 或者再次获取锁失败
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
private void doAcquireInterruptibly(int arg) throws InterruptedException {
.........
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException(); //请求资源过程中抛异常模式
}
} finally {
if (failed)
cancelAcquire(node);
}
}
//各种模式下的 取消排队的节点
acquireQueued|doAcquireInterruptibly|doAcquireNanos
doAcquireShared|doAcquireSharedInterruptibly|doAcquireSharedNanos
private void cancelAcquire(Node node) {
if (node == null)
return;
node.thread = null;
//跳过取消的节点
Node pred = node.prev;
while (pred.waitStatus > 0)
node.prev = pred = pred.prev;
Node predNext = pred.next;
node.waitStatus = Node.CANCELLED;
//尾结点 直接删除
if (node == tail && compareAndSetTail(node, pred)) {
compareAndSetNext(pred, predNext, null);
} else {
int ws;
if (pred != head &&
((ws = pred.waitStatus) == Node.SIGNAL ||
(ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) &&
pred.thread != null) {
Node next = node.next;
if (next != null && next.waitStatus <= 0)
compareAndSetNext(pred, predNext, next);
} else {
//头结点 唤醒后续节点
unparkSuccessor(node);
}
node.next = node;
}
}