public class ReentrantLock implements Lock, java.io.Serializable { //ReentrantLock 有兩種鎖:公平鎖,非公平鎖 private final Sync sync; //併發包基本 都是基於aqs abstract static class Sync extends AbstractQueuedSynchronizer {...} //非公平鎖 static final class NonfairSync extends Sync {...} //公平鎖 static final class FairSync extends Sync {...} //默認非公平鎖 public ReentrantLock() { sync = new NonfairSync(); } public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }
}
先看看lock方法(非公平爲例):
public void lock() { sync.lock(); } final void lock() { //這邊首先要知道 state 是個鎖定標誌,0 說明是空閒 //如果空閒,修改爲 1,設置當前線程獲取鎖 if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else //獲取鎖 acquire(1); } public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
從字面理解:嘗試獲取鎖,如果失敗,則加入獲取鎖的隊列,加入之前 需要先創建node節點 ,默認是獨佔式的,這邊先聲明aqs有兩種鎖模式(共享式,獨佔式),這裏可以看到ReentrantLock是獨佔式的;
final boolean nonfairTryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { //再次嘗試獲取鎖,如果失敗說明出現併發 if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { //考慮到ReentrantLock可以重入鎖 ,獲取鎖跟釋放鎖都是成雙成對出現, //對上線做一個校驗,如果重入鎖 返回true int nextc = c + acquires; if (nextc < 0) // overflow throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; }
如果獲取 鎖失敗,回到acquire()方法,加入 獲取鎖隊列,先看增加節點的方法:
private Node addWaiter(Node mode) { Node node = new Node(Thread.currentThread(), mode); Node pred = tail; if (pred != null) { //如果尾部node不爲空,則把新增的node加到尾部,添加也是基於CAS //如果添加失敗,說明出現併發,走enq node.prev = pred; if (compareAndSetTail(pred, node)) { pred.next = node; return node; } } enq(node); return node; } private Node enq(final Node node) { //如果是FIFO,是從head的下個node開始 !! for (;;) { //這裏是死循環,確保把新增的節點加到tail Node t = tail; if (t == null) { //如果尾部爲空,new一個node爲頭部,尾部也爲這個頭部的節點 if (compareAndSetHead(new Node())) tail = head; } else { //把新增node加到尾部 node.prev = t; if (compareAndSetTail(t, node)) { t.next = node; return t; } } } }
節點創建完,然後是加到 隊列
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)) { //如果上一個節點剛好是頭節點,也許已經釋放鎖,嘗試獲取鎖 setHead(node); p.next = null; // help GC failed = false; return interrupted; } if (shouldParkAfterFailedAcquire(p, node) && //檢查前一個節點的狀態,看當前獲取鎖失敗的線程是否需要掛起。 parkAndCheckInterrupt()) //如果需要,藉助JUC包下的LockSopport類的靜態方法Park掛起當前線程。 //直到被喚醒。 interrupted = true; } } finally { if (failed) cancelAcquire(node); } } private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) { int ws = pred.waitStatus; if (ws == Node.SIGNAL) return true; if (ws > 0) { do { node.prev = pred = pred.prev; } while (pred.waitStatus > 0); pred.next = node; } else { compareAndSetWaitStatus(pred, ws, Node.SIGNAL); } return false; } public static void park(Object blocker) { Thread t = Thread.currentThread(); setBlocker(t, blocker); unsafe.park(false, 0L);//0:永久 setBlocker(t, null); }
上面有提到Node,其實它是 aqs很重要的內部 結構
abstract class AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer implements java.io.Serializable { private transient volatile Node head; private transient volatile Node tail; private volatile int state; static final class Node { static final Node SHARED = new Node(); static final Node EXCLUSIVE = null; static final int CANCELLED = 1;//節點取消 static final int SIGNAL = -1;//節點等待觸發 static final int CONDITION = -2;//節點等待條件 static final int PROPAGATE = -3;//節點狀態需要向後傳播。 //有上面四種狀態 只有當前節點的前一個節點爲SIGNAL時,才能當前節點才能被掛起。 volatile int waitStatus; volatile Node prev; volatile Node next; volatile Thread thread; Node nextWaiter; }