AQS源碼解析(4)tryAcquire

之前的acquire函數(shù)會先調(diào)用tryAcquire去嘗試獲得鎖,這個(gè)在每個(gè)具體類中實(shí)現(xiàn),這里看ReentrantLock中2個(gè)實(shí)現(xiàn)。
公平鎖FairSync中:

protected final boolean tryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {//狀態(tài)為0表示可以加鎖
                if (!hasQueuedPredecessors() && //hasQueuedPredecessors表示之前的線程是否有在排隊(duì)的,這里加了!表示沒有排隊(duì)
                    compareAndSetState(0, acquires)) { //那么就去嘗試cas state
                    setExclusiveOwnerThread(current); //如果cas成功設(shè)置排他線程為當(dāng)前線程,表示成功得到鎖
                    return true;
                }
            }
            else if (current == getExclusiveOwnerThread()) {//如果當(dāng)前的排他線程是當(dāng)前線程,表示是重入
                int nextc = c + acquires; //重入計(jì)數(shù)器增加
                if (nextc < 0)
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);//因?yàn)橐呀?jīng)獲得鎖了,所以不用cas去設(shè),直接設(shè)值就行
                return true;
            }
            return false;
        }

非公平鎖中:

protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
        }

final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {//不同于公平鎖中,沒有hasQueuedPredecessors這個(gè)函數(shù),表示當(dāng)非公平鎖去加鎖的時(shí)候,不會去看有沒有線程在排隊(duì),直接去搶鎖,如果搶到了后續(xù)一樣。否則會去排隊(duì)(后續(xù)代碼再看)
          //之前課程上講過”一朝排隊(duì)永遠(yuǎn)排隊(duì)“就是這個(gè)意思,排隊(duì)中的非公平并不會去搶先
                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;
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容