軟體定製開發按需定價,如有需要可聯絡客服進行諮詢

云豹科技-苏凌霄發表於2024-10-26

軟體定製開發按需定價,如有需要可聯絡客服進行諮詢

公平鎖和非公平鎖

公平鎖:多個執行緒按照申請鎖的順序來獲取鎖

公平鎖的優點是等待鎖的執行緒不會餓死。缺點是整體吞吐效率相對非公平鎖要低,等待佇列中除第一個執行緒以外的所有執行緒都會阻塞,CPU喚醒阻塞執行緒的開銷比非公平鎖大。

非公平鎖:多個執行緒加鎖時直接嘗試獲取鎖,獲取不到才會到等待佇列的隊尾等待。但如果此時鎖剛好可用,那麼這個執行緒可以無需阻塞直接獲取到鎖,所以非公平鎖有可能出現後申請鎖的執行緒先獲取鎖的場景。

非公平鎖的優點是可以減少喚起執行緒的開銷,整體的吞吐效率高,因為執行緒有機率不阻塞直接獲得鎖,CPU不必喚醒所有執行緒。缺點是處於等待佇列中的執行緒可能會餓死,或者等很久才會獲得鎖。

在JAVA中,ReentrantLock可透過建構函式至指定是否是公平鎖,預設是非公平鎖

//********************* 公平鎖加鎖 ***********************
protected final boolean tryAcquire(int acquires) {
    final Thread current = Thread.currentThread();
    int c = getState();
    if (c == 0) {
        if (!hasQueuedPredecessors() && //區別在這,!hasQueuedPredecessors()
            compareAndSetState(0, acquires)) {
            setExclusiveOwnerThread(current);
            return true;
        }
    }
    else if (current == getExclusiveOwnerThread()) {
        int nextc = c + acquires;
        if (nextc < 0)
            throw new Error("Maximum lock count exceeded");
        setState(nextc);
        return true;
    }
    return false;
}

//********************* 非公平鎖加鎖 ***********************
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;
}

透過上面的原始碼對比,可以明顯的看出公平鎖與非公平鎖的lock()方法唯一的區別就在於公平鎖在獲取同步狀態時多了一個限制條件:hasQueuedPredecessors()。這個方法主要是判斷當前執行緒是否位於同步佇列中的第一個。如果是則返回true,否則返回false。也就是說公平鎖按照佇列等待順序來加鎖的

public final boolean hasQueuedPredecessors() {
    Node t = tail; // Read fields in reverse initialization order
    Node h = head;
    Node s;
    return h != t &&
        ((s = h.next) == null || s.thread != Thread.currentThread());
}

synchronized預設是非公平鎖並且不能變為公平鎖

以上就是

軟體定製開發按需定價,如有需要可聯絡客服進行諮詢, 更多內容歡迎關注之後的文章

相關文章