本文實例為大家分享了tryAcquire()、addWaiter()、acquireQueued()的用法 ,供大家參考,具體內容如下
tryAcquire()
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()) { int nextc = c + acquires; if (nextc < 0) // overflow throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; }
先判斷state是否為0,如果為0就執(zhí)行上面提到的lock方法的前半部分,通過CAS操作將state的值從0變?yōu)?,否則判斷當前線程是否為exclusiveOwnerThread,然后把state++,也就是重入鎖的體現,我們注意前半部分是通過CAS來保證同步,后半部分并沒有同步的體現,原因是:后半部分是線程重入,再次獲得鎖時才觸發(fā)的操作,此時當前線程擁有鎖,所以對ReentrantLock的屬性操作是無需加鎖的。如果tryAcquire()獲取失敗,則要執(zhí)行addWaiter()向等待隊列中添加一個獨占模式的節(jié)點。
addWaiter()
/** * 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)) { pred.next = node; return node; } } enq(node); return node; }
這個方法的注釋:創(chuàng)建一個入隊node為當前線程,Node.EXCLUSIVE 是獨占鎖, Node.SHARED 是共享鎖。
先找到等待隊列的tail節(jié)點pred,如果pred!=null,就把當前線程添加到pred后面進入等待隊列,如果不存在tail節(jié)點執(zhí)行enq()
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; } } } }
這里進行了循環(huán),如果此時存在了tail就執(zhí)行同上一步驟的添加隊尾操作,如果依然不存在,就把當前線程作為head結點。
插入節(jié)點后,調用acquireQueued()進行阻塞
acquireQueued()
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()) interrupted = true; } } finally { if (failed) cancelAcquire(node); } }
先獲取當前節(jié)點的前一節(jié)點p,如果p是head的話就再進行一次tryAcquire(arg)操作,如果成功就返回,否則就執(zhí)行shouldParkAfterFailedAcquire、parkAndCheckInterrupt來達到阻塞效果;
以上所述是小編給大家介紹的tryAcquire()、addWaiter()、acquireQueued()的用法詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
|
新聞熱點
疑難解答
圖片精選