詳解tryAcquire()、addWaiter()、acquireQueued()
本文實(shí)例為大家分享了tryAcquire()、addWaiter()、acquireQueued()的用法 ,供大家參考,具體內(nèi)容如下
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方法的前半部分,通過(guò)CAS操作將state的值從0變?yōu)?,否則判斷當(dāng)前線程是否為exclusiveOwnerThread,然后把state++,也就是重入鎖的體現(xiàn),我們注意前半部分是通過(guò)CAS來(lái)保證同步,后半部分并沒(méi)有同步的體現(xiàn),原因是:后半部分是線程重入,再次獲得鎖時(shí)才觸發(fā)的操作,此時(shí)當(dāng)前線程擁有鎖,所以對(duì)ReentrantLock的屬性操作是無(wú)需加鎖的。如果tryAcquire()獲取失敗,則要執(zhí)行addWaiter()向等待隊(duì)列中添加一個(gè)獨(dú)占模式的節(jié)點(diǎn)。
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; }
這個(gè)方法的注釋:創(chuàng)建一個(gè)入隊(duì)node為當(dāng)前線程,Node.EXCLUSIVE 是獨(dú)占鎖, Node.SHARED 是共享鎖。
先找到等待隊(duì)列的tail節(jié)點(diǎn)pred,如果pred!=null,就把當(dāng)前線程添加到pred后面進(jìn)入等待隊(duì)列,如果不存在tail節(jié)點(diǎn)執(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; } } } }
這里進(jìn)行了循環(huán),如果此時(shí)存在了tail就執(zhí)行同上一步驟的添加隊(duì)尾操作,如果依然不存在,就把當(dāng)前線程作為head結(jié)點(diǎn)。
插入節(jié)點(diǎn)后,調(diào)用acquireQueued()進(jìn)行阻塞
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); } }
先獲取當(dāng)前節(jié)點(diǎn)的前一節(jié)點(diǎn)p,如果p是head的話就再進(jìn)行一次tryAcquire(arg)操作,如果成功就返回,否則就執(zhí)行shouldParkAfterFailedAcquire、parkAndCheckInterrupt來(lái)達(dá)到阻塞效果;
以上所述是小編給大家介紹的tryAcquire()、addWaiter()、acquireQueued()的用法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡及Eureka與Ribbon的應(yīng)用小結(jié)
這篇文章主要介紹了Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡:Eureka與Ribbon的應(yīng)用,通過(guò)使用Eureka和Ribbon,我們可以在Java項(xiàng)目中實(shí)現(xiàn)高效的服務(wù)發(fā)現(xiàn)和負(fù)載均衡,需要的朋友可以參考下2024-08-08Java SPI 機(jī)制知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于Java SPI 機(jī)制知識(shí)點(diǎn)總結(jié)內(nèi)容,需要的朋友們可以參考下。2020-02-02Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例
這篇文章主要介紹了Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05Java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,落子動(dòng)作的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03