詳解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方法的前半部分,通過CAS操作將state的值從0變?yōu)?,否則判斷當(dāng)前線程是否為exclusiveOwnerThread,然后把state++,也就是重入鎖的體現(xiàn),我們注意前半部分是通過CAS來保證同步,后半部分并沒有同步的體現(xiàn),原因是:后半部分是線程重入,再次獲得鎖時(shí)才觸發(fā)的操作,此時(shí)當(dāng)前線程擁有鎖,所以對ReentrantLock的屬性操作是無需加鎖的。如果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來達(dá)到阻塞效果;
以上所述是小編給大家介紹的tryAcquire()、addWaiter()、acquireQueued()的用法詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析
這篇文章主要為大家介紹了TransactionSynchronization的invokeAfterCompletion事務(wù)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Springboot實(shí)現(xiàn)人臉識別與WebSocket長連接的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot實(shí)現(xiàn)人臉識別與WebSocket長連接的實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
Springboot升級到2.7.2結(jié)合nacos遇到的坑及解決
這篇文章主要介紹了Springboot升級到2.7.2結(jié)合nacos遇到的坑及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
在Mybatis @Select注解中實(shí)現(xiàn)拼寫動(dòng)態(tài)sql
這篇文章主要介紹了在Mybatis @Select注解中實(shí)現(xiàn)拼寫動(dòng)態(tài)sql,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Spring5中SpringWebContext方法過時(shí)的解決方案
這篇文章主要介紹了Spring5中SpringWebContext方法過時(shí)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

