JDK8 new ReentrantLock((true)加鎖流程
更新時間:2023年07月06日 10:35:35 作者:子瞻
這篇文章主要介紹了java面試中常遇到的問題JDK8 new ReentrantLock((true)加鎖流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
new ReentrantLock(true)加鎖流程
protected final boolean tryAcquire(int acquires) {
//獲取當前線程
final Thread current = Thread.currentThread();
//獲取state值
int c = getState();
//還沒有線程占用
if (c == 0) {
//!(頭節(jié)點和尾節(jié)點不是一個節(jié)點 && (頭節(jié)點的next -> NULL 或者 頭節(jié)點.next節(jié)點 不是 當前線程 ))
//也就是說,頭尾是一個節(jié)點 或者 頭節(jié)點.next節(jié)點的線程是當前線程
if (!hasQueuedPredecessors() &&
//cas 0 -> 1
compareAndSetState(0, acquires)) {
/設置獨占線程
setExclusiveOwnerThread(current);
return true;
}
}
//當前線程等于獨占線程
else if (current == getExclusiveOwnerThread()) {
//state值+1
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
//重新賦值 state 值
setState(nextc);
return true;
}
//否則返回false
return false;
}如果獲得鎖失敗,和JDK8 new ReentrantLock()加鎖流程中流程一樣!
以上就是JDK8 new ReentrantLock((true)加鎖流程的詳細內容,更多關于JDK8 new ReentrantLock的資料請關注腳本之家其它相關文章!
相關文章
關于Springboot2.x集成lettuce連接redis集群報超時異常Command timed out afte
這篇文章主要介紹了Springboot2.x集成lettuce連接redis集群報超時異常Command timed out after 6 second(s),本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-03-03
解決IDEA提示:MVC DispatcherServlet servlet contex
在SSM整合配置Spring時出現錯誤,需刪除多余配置文件并重新導入,若屬性解析失敗,清除緩存重啟IDEA,個人經驗,供參考2025-08-08

