查看 ReentrantLock 實現 源碼 的理解記錄!,reentrantlock源碼
ReentrantLock lock = new ReentrantLock(); lock.lock();lock.unlock();//ReentrantLock 源碼中的使用 class X {//執行個體化 ReentrantLock時,//如果添加boolean參數,//true是公平模式,//flase是非公平模式,//預設非公平,非公平模式下新來的線程並不一定就能先拿到鎖,後來的也並不一定是後拿到鎖 ReentrantLock lock = new ReentrantLock(); public void m() { lock.lock(); try { // ... method body } finally { lock.unlock(); } } }//公平模式 下的lock,直接進行 acquire操作 final void lock() { acquire(1); } // 非公平模式 下的lock,//會先 根據 compareAndSetState 方法返回的結果,//去嘗試把當前線程設定成為 獨佔所有者線程,//如果不成功再進行 acquire操作。 final void lock() {//compareAndSetState 根據cas 設定,//如果目前狀態值等於期望值,//則將同步狀態設定為給定的更新值。//compareAndSetState(0, 1)) 預期值為0,修改值為1,//如果現在的state值為0,就把state值設定為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();//中斷線程 } protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); } //執行不公平的tryLock。 //tryAcquire是在子類中實現的,但都需要不公平的嘗試trylock方法。 final boolean nonfairTryAcquire(int acquires) { //擷取當前線程 final Thread current = Thread.currentThread(); //擷取state的值 int c = getState(); //如果state的值為空白,說明沒有人使用鎖 //如果預期值 為0,設定state為 設定值 if (c == 0) { if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } //如果state的值不是0,判斷 獨佔所有者線程 是否為自己, //如果 獨佔所有者線程 是自己,state的值加上 要修改的值 else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) // overflow throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } 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; //節點在等待隊列中,節點線程等待在Condition上, //當其他線程對Condition調用了Signal()方法後,//該節點將會從等待隊列轉移到同步隊列中,加入到對同步狀態的擷取中 static final int CONDITION = -2; //表示下一次共用式同步狀態擷取將會無條件地被傳播下去 static final int PROPAGATE = -3; //線程的等待狀態volatile int waitStatus; volatile int waitStatus; }// 為當前線程和給定的模式建立節點 private Node addWaiter(Node mode) { Node node = new Node(Thread.currentThread(), mode); Node pred = tail; if (pred != null) {//如果 尾節點 不為null//設定當前新建立節點的 前驅 節點為 尾節點 node.prev = pred;//判斷目前狀態值是否等於預期的尾節點pred//如果 預期值 尾節點 等於就將當前同步狀態值更新尾節點為node if (compareAndSetTail(pred, node)) {//尾節點的後繼結點為 新建立的節點 pred.next = node; return node; } } enq(node); return node; } private Node enq(final Node node) { for (;;) {//擷取尾節點 Node t = tail; if (t == null) { //初始化同步隊列// 建立新的節點new node()作為前端節點//把 前端節點 作為尾節點//而此時隊列的尾節點和頭結點是同一個節點 //由於enq是一個死迴圈,會再次進入判斷 尾節點是否為空白,//已經設定 前端節點和尾節點,進入else語句 if (compareAndSetHead(new Node())) tail = head; } else {//尾節點不為空白,把傳入的node的 前驅 節點設定為尾節點 node.prev = t; if (compareAndSetTail(t, node)) {//判斷node節點是否是預期的尾節點t,如果是就更新尾節點t為node,//把 t 的後繼節點設定為 傳入的節點,此時的 尾節點為 傳入的node t.next = node; return t; } } } } //以死迴圈的方式不停的擷取同步狀態
//如果擷取不到則阻塞節點中的線程, //而被阻塞線程只能依靠其前驅節點的出隊列操作或者阻塞線程中斷 final boolean acquireQueued(final Node node, int arg) { boolean failed = true; try { boolean interrupted = false; for (;;) {//擷取 node節點的 前驅節點 final Node p = node.predecessor(); if (p == head && tryAcquire(arg)) {//如果傳入node節點的前驅節點為head,並且//當前 線程還能夠獲得到 鎖,就把傳入node節點,設定為head setHead(node); p.next = null; // help GC failed = false; return interrupted; }// shouldParkAfterFailedAcquire檢查並更新未能擷取的節點的狀態//parkAndCheckInterrupt 阻塞當前線程,返回 線程是否被中斷 if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } finally { if (failed)//取消進行中的擷取嘗試 cancelAcquire(node); } } //檢查並更新未能擷取的節點的狀態。private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) { int ws = pred.waitStatus; if (ws == Node.SIGNAL)//如果線程阻塞,則返回true。 return true; if (ws > 0) { do { node.prev = pred = pred.prev; } while (pred.waitStatus > 0); pred.next = node; } else {//判斷pred節點的waitStatus值,是否為預期的ws//如果是就更新 為 Node.SIGNAL,阻塞pred compareAndSetWaitStatus(pred, ws, Node.SIGNAL); } return false; }