AQS 源码分析
独占锁
public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
尝试直接获取资源,如果成功直接返回
addWaiter 把当前node放到队尾并标记为独占锁
acquireQueued实现成阻塞在等待队列中获取资源,直到获取到,如果获取有被中断过返回true
如果有中断过就执行线程中断操作
tryAcquire(args)尝试获取资源,主方法中返回UnsupportedOperationException,各个子类中自行实现
/** * Creates and enqueues node for current thread and given mode. * * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared * @return the new node */ private Node addWaiter(Node mode) { Node node = new Node(Thread.currentThread(), mode); // Try the fast path of enq; backup to full enq on failure Node pred = tail; //获取队尾 if (pred != null) {//如果队尾不为空,将新节点放入队尾 node.prev = pred;// if (compareAndSetTail(pred, node)) {//更新队尾信息cas 如果cas更新失败 pred.next = node; return node; //返回 } } enq(node); //更新失败或者队尾为空执行自旋操作直到将node添加进去 return node; } /** * Inserts node into queue, initializing if necessary. See picture above. * @param node the node to insert * @return node's predecessor */ 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; } } } } /** * Acquires in exclusive uninterruptible mode for thread already in * queue. Used by condition wait methods as well as acquire. * * @param node the node * @param arg the acquire argument * @return {@code true} if interrupted while waiting */ final boolean acquireQueued(final Node node, int arg) { boolean failed = true;//初始化尝试获取失败 try { boolean interrupted = false;//初始化没有被中断过,若果有被中断过就要执行线程中断操作 for (;;) {//这里是一个自旋操作,会不停的去判断直到获取到资源 final Node p = node.predecessor(); //获取前驱节点 if (p == head && tryAcquire(arg)) { //如果前驱节点是头结点并且也获取到了资源,把当前节点放到头节点,并返回是否被中断过,个人理解 如果获取到资源了那就说明前一个节点已经执行完了或者说中断过,或者已经cancle了(前一个节点指的是他的pre或者通过抢占方式获取资源的节点) 因为是独占锁嘛 setHead(node); p.next = null; // help GC failed = false; return interrupted; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt())//如果是头结点但是没有获取到资源或者不是头节点,如果需要暂停并且挂起当前线程并返回是否线程中断过,如果两者都满足就返回true,线程中断过 interrupted = true; } } finally { if (failed) cancelAcquire(node);//如果获取资源失败了,就取消获取资源 } } /** * Checks and updates status for a node that failed to acquire. * Returns true if thread should block. This is the main signal * control in all acquire loops. Requires that pred == node.prev. * * @param pred node's predecessor holding status * @param node the node * @return {@code true} if thread should block */ private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) { int ws = pred.waitStatus; //前驱节点状态 if (ws == Node.SIGNAL)//如果是等待唤醒状态 返回需要把线程挂起 /* * This node has already set status asking a release * to signal it, so it can safely park. */ return true; if (ws > 0) {//如果状态值大于0说明已经取消了 //不停的往前找,直到找到node的状态 = 0(初始化的时候status = 0 ) 或则<0的, /* * Predecessor was cancelled. Skip over predecessors and * indicate retry. */ do { pred = pred.prev; node.prev = pred ; } while (pred.waitStatus > 0); pred.next = node; } else {//<0 说明正在等待中 更新pred的值,node有前驱而且也在队列中,那说明先执行前驱才回执行后面的,所以当前的线程进来是不会执行的,所以返回false /* * 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; } /** * Cancels an ongoing attempt to acquire. * * @param node the node */ private void cancelAcquire(Node node) { // Ignore if node doesn't exist if (node == null) return; node.thread = null; // Skip cancelled predecessors Node pred = node.prev; while (pred.waitStatus > 0)//找当前节点已经取消的节点,然后更新 node.prev = pred = pred.prev; // predNext is the apparent node to unsplice. CASes below will // fail if not, in which case, we lost race vs another cancel // or signal, so no further action is necessary. Node predNext = pred.next; // Can use unconditional write instead of CAS here. // After this atomic step, other Nodes can skip past us. // Before, we are free of interference from other threads. node.waitStatus = Node.CANCELLED;//更新当前节点的状态值=1也就是取消状态 // If we are the tail, remove ourselves. if (node == tail && compareAndSetTail(node, pred)) { compareAndSetNext(pred, predNext, null); } else { // If successor needs signal, try to set pred's next-link // so it will get one. Otherwise wake it up to propagate. 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; // help GC } release(int) 释放资源 /** * Status field, taking on only the values: * SIGNAL: The successor of this node is (or will soon be) * blocked (via park), so the current node must * unpark its successor when it releases or * cancels. To avoid races, acquire methods must * first indicate they need a signal, * then retry the atomic acquire, and then, * on failure, block. * CANCELLED: This node is cancelled due to timeout or interrupt. * Nodes never leave this state. In particular, * a thread with cancelled node never again blocks. * CONDITION: This node is currently on a condition queue. * It will not be used as a sync queue node * until transferred, at which time the status * will be set to 0. (Use of this value here has * nothing to do with the other uses of the * field, but simplifies mechanics.) * PROPAGATE: A releaseShared should be propagated to other * nodes. This is set (for head node only) in * doReleaseShared to ensure propagation * continues, even if other operations have * since intervened. * 0: None of the above * * The values are arranged numerically to simplify use. * Non-negative values mean that a node doesn't need to * signal. So, most code doesn't need to check for particular * values, just for sign. * * The field is initialized to 0 for normal sync nodes, and * CONDITION for condition nodes. It is modified using CAS * (or when possible, unconditional volatile writes). */ volatile int waitStatus; /** * Releases in exclusive mode. Implemented by unblocking one or * more threads if {@link #tryRelease} returns true. * This method can be used to implement method {@link Lock#unlock}. * * @param arg the release argument. This value is conveyed to * {@link #tryRelease} but is otherwise uninterpreted and * can represent anything you like. * @return the value returned from {@link #tryRelease} */ public final boolean release(int arg) { if (tryRelease(arg)) {//由各个子类去重写 默认是抛个异常 //尝试释放资源 Node h = head; if (h != null && h.waitStatus != 0) // waitStatus 有5种状态 unparkSuccessor(h); return true; } return false; } ReentrantLock 中的实现 protected final boolean tryRelease(int releases) { int c = getState() - releases;//unlock每次减1 lock每次加1 if (Thread.currentThread() != getExclusiveOwnerThread()) //如果不是当前线程直接抛异常 set ExclusiveOwnerThread() 在tryAccquired()中设置是当前线程 throw new IllegalMonitorStateException(); boolean free = false; if (c == 0) { //如果当前状态为0 说明释放完了 也就是lock几次 unlock也几次了 就等于说资源释放完了 执行下面操作 free = true; setExclusiveOwnerThread(null); } setState(c); return free; } /** * Wakes up node's successor, if one exists. * * @param node the node */ 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; //获取状态 如果<0就更新当前的状态,因为既然是释放那就是执行完了嘛 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为空或者已经取消了 就从队尾往前找最前面那个正在等待唤醒的 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); } 共享锁(如countDownLatch , semaphore) /** * Acquires in shared mode, ignoring interrupts. Implemented by * first invoking at least once {@link #tryAcquireShared}, * returning on success. Otherwise the thread is queued, possibly * repeatedly blocking and unblocking, invoking {@link * #tryAcquireShared} until success. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquireShared} but is otherwise uninterpreted * and can represent anything you like. */ public final void acquireShared(int arg) { //tryAcquireShared 返回值 负数代表失败 ,正数代表成功但后续也可以获取成功,0代表当前获取成功但后续获取不成功 没有剩余资源 if (tryAcquireShared(arg) < 0) doAcquireShared(arg); } /** * Acquires in shared uninterruptible mode. * @param arg the acquire argument */ private void doAcquireShared(int arg) { final Node node = addWaiter(Node.SHARED);//加入队尾设置类型是共享类型 boolean failed = true; try { boolean interrupted = false; for (;;) {//自旋 final Node p = node.predecessor();//获取前驱节点 if (p == head) { //和独占模式不一样 独占锁是获取到后才进去下面方法 ,这里是先进去再去尝试获取,判断获取到资源没有,这里还是个自旋也就是说会一直获取直到获取成功 当然也会通过shouldParkAfterFailedAcquire 更新节点的状态 int r = tryAcquireShared(arg); if (r >= 0) { setHeadAndPropagate(node, r); p.next = null; // help GC if (interrupted) selfInterrupt();//如果中断过 挂起线程, failed = false; return; } } //和独占锁操作一样 获取失败了是不是要挂起 线程有没有中断过 if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } finally { if (failed) cancelAcquire(node); } } /** * Sets head of queue, and checks if successor may be waiting * in shared mode, if so propagating if either propagate > 0 or * PROPAGATE status was set. * * @param node the node * @param propagate the return value from a tryAcquireShared */ private void setHeadAndPropagate(Node node, int propagate) { Node h = head; // Record old head for check below setHead(node); /* * Try to signal next queued node if: * Propagation was indicated by caller, * or was recorded (as h.waitStatus either before * or after setHead) by a previous operation * (note: this uses sign-check of waitStatus because * PROPAGATE status may transition to SIGNAL.) * and * The next node is waiting in shared mode, * or we don't know, because it appears null * * The conservatism in both of these checks may cause * unnecessary wake-ups, but only when there are multiple * racing acquires/releases, so most need signals now or soon * anyway. */ //如果已经分配到资源 并且原来的头结点为空或者现成装在是带唤醒状态或者新设置的头结点是空或者待唤醒状态 (个人理解 如果头节点为空了 那是不是说明当前节点可以执行了? 不明白为什么这么做) if (propagate > 0 || h == null || h.waitStatus < 0 || (h = head) == null || h.waitStatus < 0) { Node s = node.next; if (s == null || s.isShared())//如果s是null或者是共享 节点线程唤醒 doReleaseShared(); } }
/** * Releases in shared mode. Implemented by unblocking one or more * threads if {@link #tryReleaseShared} returns true. * * @param arg the release argument. This value is conveyed to * {@link #tryReleaseShared} but is otherwise uninterpreted * and can represent anything you like. * @return the value returned from {@link #tryReleaseShared} */ public final boolean releaseShared(int arg) { if (tryReleaseShared(arg)) { //就是返回状态为不为0 为零就是释放了然后唤起线程 释放资源 doReleaseShared();//唤起下个线程 return true; } return false; } /** * Release action for shared mode -- signals successor and ensures * propagation. (Note: For exclusive mode, release just amounts * to calling unparkSuccessor of head if it needs signal.) */ private void doReleaseShared() { /* * Ensure that a release propagates, even if there are other * in-progress acquires/releases. This proceeds in the usual * way of trying to unparkSuccessor of head if it needs * signal. But if it does not, status is set to PROPAGATE to * ensure that upon release, propagation continues. * Additionally, we must loop in case a new node is added * while we are doing this. Also, unlike other uses of * unparkSuccessor, we need to know if CAS to reset status * fails, if so rechecking. */ for (;;) { Node h = head; if (h != null && h != tail) { int ws = h.waitStatus; if (ws == Node.SIGNAL) {//如果当前节点线程是等待被唤醒状态,就更新当前节点的状态 因为要获取资源执行了嘛(更新失败继续自旋更新 直到更新成功),要执行了嘛 >0是取消 <0是等待 ,0是无状态 if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) continue; // loop to recheck cases unparkSuccessor(h);//唤醒线程了 = else if (ws == 0 && !compareAndSetWaitStatus(h, 0, Node.PROPAGATE)) continue; // loop on failed CAS = if (h == head) // loop if head changed 状态修改成功后 break break; = } /** * Wakes up node's successor, if one exists. * * @param node the node */ 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; //如果node的下个节点已经执取消或者没有 那么就找到最前面一个等待被唤醒的线程 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);//唤醒线程 }
参考博客:https://www.cnblogs.com/waterystone/p/4920797.html#!comments