/** * Acquires in exclusive mode, ignoring interrupts. Implemented * by invoking at least once {@link #tryAcquire}, * returning on success. Otherwise the thread is queued, possibly * repeatedly blocking and unblocking, invoking {@link * #tryAcquire} until success. This method can be used * to implement method {@link Lock#lock}. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquire} but is otherwise uninterpreted and * can represent anything you like. */ publicfinalvoidacquire(int arg){ // 前面第一种情况是tryAcquire直接成功了,这个if判断第一个条件就是false,就不往下执行了 // 如果是第二个线程,第一个条件获取锁不成功,条件判断!tryAcquire(arg) == true,就会走 // acquireQueued(addWaiter(Node.EXCLUSIVE), arg) if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
/** * 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 node = new Node(Thread.currentThread(), mode); // Try the fast path of enq; backup to full enq on failure // 最快的方式就是把当前线程的节点放在阻塞队列的最后 Node pred = tail; // 只有当tail,也就是pred不为空的时候可以直接接上 if (pred != null) { node.prev = pred; // 如果这里cas成功了,就直接接上返回了 if (compareAndSetTail(pred, node)) { pred.next = node; return node; } } // 不然就会继续走到这里 enq(node); return node; }
/** * Attempts to release this lock. * * <p>If the current thread is the holder of this lock then the hold * count is decremented. If the hold count is now zero then the lock * is released. If the current thread is not the holder of this * lock then {@link IllegalMonitorStateException} is thrown. * * @throws IllegalMonitorStateException if the current thread does not * hold this lock */ publicvoidunlock(){ // 释放锁 sync.release(1); } /** * 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} */ publicfinalbooleanrelease(int arg){ // 尝试去释放 if (tryRelease(arg)) { Node h = head; if (h != null && h.waitStatus != 0) unparkSuccessor(h); returntrue; } returnfalse; } protectedfinalbooleantryRelease(int releases){ int c = getState() - releases; if (Thread.currentThread() != getExclusiveOwnerThread()) thrownew IllegalMonitorStateException(); boolean free = false; // 判断是否完全释放锁,因为可重入 if (c == 0) { free = true; setExclusiveOwnerThread(null); } setState(c); return free; } // 这段代码和上面的一致,只是为了顺序性,又拷下来看下
publicfinalbooleanrelease(int arg){ // 尝试去释放,如果是完全释放,返回的就是true,否则是false if (tryRelease(arg)) { Node h = head; // 这里判断头结点是否为空以及waitStatus的状态,前面说了head节点其实是 // 在第二个线程进来的时候初始化的,如果是空的话说明没后续节点,并且waitStatus // 也表示了后续的等待状态 if (h != null && h.waitStatus != 0) unparkSuccessor(h); returntrue; } returnfalse; }
/** * Wakes up node's successor, if one exists. * * @param node the node */ // 唤醒后继节点 privatevoidunparkSuccessor(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); }