这张图里面head是头节点,tail是尾节点,state是判断共享资源是否被占用的标志位
state用int标志用int的原因是因为state需要表示线程占用的数量
其分为独占模式和共享模式
独占模式是:共享资源一旦被占用就不能被其他线程所访问。
共享模式是:共享资源被占用,其他共享模式下的线程也能被占用。\
thread是线程对象
prev,next是前后指针
waitStatus是等待状态,有四种状态
AQS的两个核心方法\
tryAcquire:
这个方法的意图就是这个方法必须重写这个方法,否则就抛出不支持这个操作的异常,在上层调用成功的时候获取锁。
如果选择等待锁则调用acquire这个方法\
所有继承类都调用我的这个方法,不允许继承类擅自重写,意思是我一定能得到锁。
继续解读addWaiter这个方法\
这个方法的意思就是将当前线程封装后加入等待队列,这个队列是先进先出的
if (pred != null) {
node.prev = pred;//先获取尾节点的指针
//如果尾节点不为空,通过CAS将当前节点置为尾节点,然后再将前置节点的next指针指向已经成为尾节点的当前指针
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
//如果当前尾节点为空,或者第一次CAS操作失败则进入入队方法
enq(node);
private Node enq(final Node node) {
/先进行自旋
for (;;) {
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
return node;
这一段代码的意思就是将node插入队尾
然后再出来是acquireQueued这个方法,这个方法配合release方法是对线程进行相应和挂起,以此来实现队列的先进先出
final boolean acquireQueued(final Node node, long arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
//如果当前的前置节点是头节点并且当前线程获取锁成功了,就直接返回
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
//如果当前线程需要被挂起并且成功挂起,并且返回了thread.interrupted为true然后才会进行返回
//在AQS的FIFO队列中,头节点是一个虚节点,意思是当前的头节点并不是需要去那锁的节点,第二个节点才是需要去拿锁的节点,当第二个头节点拿到锁之后,就会变成头节点,然后头节点出队
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
从上述代码块中我们可以看出来程序正常执行的时候都会在finall返回false只有程序抛出异常时failed才会为true,然后去执行cancelAcquire,他的实现简单来说就是将Node的waitStatus置为CANCEL
shouldParkAfterFailedAcquire这个方法是判断是否需要挂起当前线程L
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
int ws = pred.waitStatus;
//前置结点的waitSatus为SIGNNA,说明前置节点也在等待拿锁,所以当前节点是可以挂起休息的,直接返回true
if (ws == Node.SIGNAL)
/*
* This node has already set status asking a release
* to signal it, so it can safely park.
*/
return true;
//如果大于零说明状态是cancel,说明可以进行删除
if (ws > 0) {
/*
* Predecessor was cancelled. Skip over predecessors and
* indicate retry.
*/
do {
node.prev = pred = pred.prev;
} while (pred.waitStatus > 0);
pred.next = node;
} else {
//如果是其他状态既然当前节点已经加入,那么就应该准备好等待锁,将当前节点的node置为SIGNAL然后置为false,进行下一轮判断如果shouldParkAfterFailedAcquire返回true则代表将要挂起
/*
* waitStatus must be 0 or PROPAGATE. Indicate that we
* need a signal, but don't park yet. Caller will need to
* retry to make sure it cannot acquire before parking.
*/
compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
}
return false;
}
parkAndCheckInterrupt这个方法\
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())//在这里被挂起(阻塞)
interrupted = true;
}
最后说一下release方法就是进行唤醒,从尾到头节点的前一个节点进行操作,让这个节点进行不断地自旋尝试获取锁唤醒
public final boolean release(long arg) {
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
protected boolean tryRelease(long arg) {
throw new UnsupportedOperationException();
}