1. 程式人生 > 實用技巧 >咀嚼Lock和Synchronized鎖

咀嚼Lock和Synchronized鎖

1.Synchronized鎖

底層是monitor監視器,每一個物件再建立的時候都會常見一個monitor監視器,在使用synchronized程式碼塊的時候,會在程式碼塊的前後產生一個monitorEnter和monitorexit指令,來標識這是一個同步程式碼塊。

1.1 執行流程

執行緒遇到同步程式碼塊,給這個物件monitor物件加1,當執行緒退出當前程式碼塊以後,給這個物件的monitor物件減一,如果monitor指令的值為0則當前執行緒釋放鎖。

1.2 反編譯原始碼

同步程式碼塊反編譯

public void test01(){
        synchronized (this){
            int num = 1 ;
        }
    }

兩次monitorexit的作用是避免同步程式碼塊無法跳出,因此存在兩種,正常退出和異常退出

同步方法反編譯

public synchronized  void test01(){
            int num = 1 ;
    }

可以發現其沒有在同步方法前後新增monitor指令,但是在其底層實際上也是通過monitor指令實現的,只不過相較於同步程式碼塊來說,他是隱式的。

1.3 鎖升級

JDK1.5的時候對於synchronzied做了一系列優化操作,增加了諸如:偏向鎖,輕量級鎖,自旋鎖,鎖粗化,重量級鎖的概念。

1.3.1 偏向鎖

在一個執行緒在執行獲取鎖的時候,當前執行緒會在monitor

物件中儲存指向該執行緒的ID。當執行緒再次進入的時候,不需要通過CAS的方法再來進行加鎖或者解鎖,而是檢測偏向鎖的ID是不是當前要進行的執行緒,如果是,直接進入。

偏向鎖,適用於一個執行緒執行任務的情況

JDK1.6中,預設是開啟的。可以通過-XX:-UseBiasedLocking=false引數關閉偏向鎖

1.3.2 輕量級鎖

輕量級鎖是指鎖為偏向鎖的時候,該鎖被其他執行緒嘗試獲取,此時偏向鎖升級為輕量級鎖,其他執行緒會通過自旋的方式嘗試獲取鎖,執行緒不會阻塞,從而提供效能

升級為輕量級鎖的情況有兩種:

  • 關閉偏向鎖
  • 有多個執行緒競爭偏向鎖的時候

具體實現:

執行緒進行程式碼塊以後,如果同步物件鎖狀態為無鎖的狀態,虛擬機器將首先在當前執行緒的棧幀中建立一個鎖記錄的空間。這個空間記憶體儲了當前獲取鎖的物件。

使用情況:

兩個執行緒的互相訪問

1.3.3 重量級鎖

在有超過2個執行緒訪問同一把鎖的時候,鎖自動升級為重量級鎖,也就是傳統的synchronized,此時其他未獲取鎖的執行緒會陷入等待狀態,不可被中斷。

由於依賴於monitor指令,所以其消耗系統資源比較大

上面的三個階段就是鎖升級的過程

1.3.4 鎖粗化

當在一個迴圈中,我們多次使用對同一個程式碼進行加鎖,這個時候,JVM會自動實現鎖粗化,即在迴圈外進行新增同步程式碼塊。

程式碼案例:

鎖粗化之前:

for (int i = 0; i < 10; i++) {
            synchronized (LockBigDemo.class){
                System.out.println();
            }
        }

鎖粗化之後:

synchronized (LockBigDemo.class){
            for (int i = 0; i < 10; i++) {
                    System.out.println();
            }
        }

本次關於synchronized的底層原理沒有以程式碼的方式展開,之後筆者會出一篇synchronized底層原理剖析的文章

2. Lock鎖

一個類級別的鎖,需要手動釋放鎖。可以選擇性的選擇設定為公平鎖或者不公平鎖。等待執行緒可以被打斷。

底層是基於AQS+AOSAQS類完成具體的加鎖邏輯,AOS儲存獲取鎖的執行緒資訊

2.1 ReentrantLock

我們以ReentrantLock為例解析一下其加鎖的過程。

2.1.1 lock方法

首先通過ReentrantLock的構造方法的布林值判斷建立的鎖是公平鎖還是非公平鎖。

假設現在建立的是非公平鎖,他首先會判斷鎖有沒有被獲取,如果沒有被獲取,則直接獲取鎖;

如果鎖已經被獲取,執行一次自旋,嘗試獲取鎖。

如果鎖已經被獲取,則將當前執行緒封裝為AQS佇列的一個節點,然後判斷當前節點的前驅節點是不是HEAD節點,如果是,嘗試獲取鎖;如果不是。則尋找一個安全點(執行緒狀態位SIGNAL=-1的節點)。

開始不斷自旋。判斷前節點是不是HEAD節點,如果是獲取鎖,如果不是掛起。

原始碼解讀:

  • 非公平鎖lock
final void lock() {
    //判斷是否存在鎖
            if (compareAndSetState(0, 1))
                //獲取鎖
                setExclusiveOwnerThread(Thread.currentThread());
            else
                acquire(1);
        }
public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
//非公平鎖的自旋邏輯
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) {
                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;
        }
//將執行緒封裝為一個執行緒節點,傳入鎖模式,排他或者共享
private Node addWaiter(Node mode) {
        Node node = new Node(Thread.currentThread(), mode);
        // 獲取尾節點
        Node pred = tail;
    //如果尾節點不為Null,直接將這個執行緒節點新增到隊尾
        if (pred != null) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
    //為空,自旋設定尾節點
        enq(node);
        return node;
    }

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;
                }
            }
        }
    }
//嘗試入隊
final boolean acquireQueued(final Node node, int arg) {
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                //獲取節點的前驅節點,如果前驅節點為head節點,則嘗試獲取鎖
                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);
        }
    }
private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;
    //前驅節點已經安全
        if (ws == Node.SIGNAL)
            return true;
    //前驅節點不安全,尋找一個執行緒狀態為`Signal`的節點作為前驅節點
        if (ws > 0) {
            do {
                node.prev = pred = pred.prev;
            } while (pred.waitStatus > 0);
            pred.next = node;
        } else {
            //否則直接設定這個前驅節點的執行緒等待狀態值
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;
    }

//中斷執行緒
private final boolean parkAndCheckInterrupt() {
        LockSupport.park(this);
        return Thread.interrupted();
    }

2.1.2 unlock方法

程式碼解讀:

public void unlock() {
        sync.release(1);
    }
public final boolean release(int arg) {
    //嘗試釋放鎖
        if (tryRelease(arg)) {
            //獲取佇列頭元素,喚醒該執行緒節點,執行任務
            Node h = head;
            if (h != null && h.waitStatus != 0)
                unparkSuccessor(h);
            return true;
        }
        return false;
    }
protected final boolean tryRelease(int releases) {
            int c = getState() - releases;
    //判斷是否為當前執行緒擁有鎖
            if (Thread.currentThread() != getExclusiveOwnerThread())
                throw new IllegalMonitorStateException();
            boolean free = false;
    //釋放成功
            if (c == 0) {
                free = true;
                setExclusiveOwnerThread(null);
            }
            setState(c);
            return free;
        }
private void unparkSuccessor(Node node) {
    
        int ws = node.waitStatus;
        if (ws < 0)
            compareAndSetWaitStatus(node, ws, 0);
      
        Node s = node.next;
        if (s == null || s.waitStatus > 0) {
            s = null;
            for (Node t = tail; t != null && t != node; t = t.prev)
                if (t.waitStatus <= 0)
                    s = t;
        }
    //喚醒下一個節點
        if (s != null)
            LockSupport.unpark(s.thread);
    }
2.1.3 Node節點
/** 共享鎖,讀鎖使用 */
        static final Node SHARED = new Node();
        /** 獨佔鎖*/
        static final Node EXCLUSIVE = null;

        /** 不安全執行緒 */
        static final int CANCELLED =  1;
        /** 需要進行執行緒喚醒的執行緒 */
        static final int SIGNAL    = -1;
        /**condition等待中 */
        static final int CONDITION = -2;

		//執行緒等待狀態
		volatile int waitStatus;

        volatile Node prev;

        volatile Node next;

        volatile Thread thread;
        Node nextWaiter;

3. Lock鎖和Synchronized的區別

  • Lock鎖是API層面,synchronizedCPU源語級別的
  • Lock鎖等待執行緒可以被中斷,synchronized等待執行緒不可以被中斷
  • Lock鎖可以指定公平鎖和非公平鎖,synchronized只能為非公平鎖
  • Lock鎖需要主動釋放鎖,synchronized執行完程式碼塊以後自動釋放鎖

更多原創文章請關注筆者公眾號@MakerStack