Java中的ReentrantLock鎖

ReentrantLock鎖

ReentrantLock是Java中常用的鎖,屬於樂觀鎖類型,多線程併發情況下。能保證共享數據安全性,線程間有序性
ReentrantLock通過原子操作和阻塞實現鎖原理,一般使用lock獲取鎖,unlock釋放鎖,
下面說一下鎖的基本使用和底層基本實現原理,lock和unlock底層

lock的時候可能被其他線程獲得所,那麼此線程會阻塞自己,關鍵原理底層用到Unsafe類的API: CAS和park

使用方式

lock unlock對應

lock

拿到鎖,開始執行代碼邏輯

unlock

執行完代碼後,釋放鎖,讓其他線程去獲取,需要注意的是,多個線程使用的鎖對象必須是同一個

//創建鎖對象
ReentrantLock lock = new ReentrantLock();
lock.lock(); //獲取鎖(鎖定)
// 中間執行代碼,保證同一時間只有一個線程能運行此處的代碼
lock.unlock(); //鎖釋放

示例

爲了體現鎖的作用,這裏sleep睡眠0.1秒,增加哪個線程獲取鎖的隨機性
因爲線程喚醒後,會開始嘗試獲取鎖,多個線程下競爭一把鎖是隨機的

package javabasis.threads;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest implements Runnable {
    
	public static ReentrantLock lock = new ReentrantLock();//鎖
	private int thold;
    
	public LockTest(int h) {
		this.thold = h;
	}
	
	public static void main(String[] args) {
		for (int i = 10; i < 15; i++) {
			new Thread(new LockTest(i),"name-" + i).start();
		}
	}

	@Override
	public void run() {
		try {
			Thread.sleep(100);
			lock.lock(); //獲取鎖
			System.out.println("lock threadName:" + Thread.currentThread().getName());
			{
				System.out.print(" writeStart ");
				for (int i = 0; i < 15; i++) {
						Thread.sleep(100);
					System.out.print(thold+",");
				}
				System.out.println(" writeEnd");
			}
			System.out.println("unlock threadName:" + Thread.currentThread().getName() + "\r\n");
			lock.unlock(); //鎖釋放 
		} catch (InterruptedException e) {	
		}		
	}	
}

運行main方法輸出結果:

lock threadName:name-10
 writeStart 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, writeEnd
unlock threadName:name-10

lock threadName:name-14
 writeStart 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, writeEnd
unlock threadName:name-14

lock threadName:name-13
 writeStart 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, writeEnd
unlock threadName:name-13

lock threadName:name-11
 writeStart 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, writeEnd
unlock threadName:name-11

lock threadName:name-12
 writeStart 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, writeEnd
unlock threadName:name-12

這體現在多線程情況下,鎖能做到讓線程之間有序運行,

如果沒有鎖,情況可能是 12,13,13,10,10,10,12,沒有鎖其他線程可能插隊執行System.out.print

原理

ReentrantLock主要用到unsafe的CAS和park兩個功能實現鎖(CAS + park )

多個線程同時操作一個數N,使用原子(CAS)操作,原子操作能保證同一時間只能被一個線程修改,而修改數N成功後,返回true,其他線程修改失敗,返回false,
這個原子操作可以定義線程是否拿到鎖,返回true代表獲取鎖,返回false代表爲沒有拿到鎖。

拿到鎖的線程,自然是繼續執行後續邏輯代碼,而沒有拿到鎖的線程,則調用park,將線程(自己)阻塞。

線程阻塞需要其他線程喚醒,ReentrantLock中用到了鏈表用於存放等待或者阻塞的線程,每次線程阻塞,先將自己的線程信息放入鏈表尾部,再阻塞自己;之後需要拿到鎖的線程,在調用unlock 釋放鎖時,從鏈表中獲取阻塞線程,調用unpark 喚醒指定線程

Unsafe

sun.misc.Unsafe是關鍵類,提供大量偏底層的API 包括CAS park
sun.misc.Unsafe 此類在openjdk中可以查看

CAS 原子操作

compare and swapz(CAS)比較並交換,是原子性操作,
原理:當修改一個(內存中的)變量o的值N的時候,首先有個期望值expected,和一個更新值x,先比較N是否等於expected,等於,那麼更新內存中的值爲x值,否則不更新。

public final native boolean compareAndSwapInt(Object o, long offset,
                                              int expected,
                                              int x);

這裏offset據瞭解,是對象的成員變量在內存中的偏移地址,
即底層一個對象object存放在內存中,讀取的地址是0x2110,此對象的一個成員變量state的值也在內存中,但內存地址肯定不是0x2110

java中的CAS使用

java.util.concurrent.locks.AbstractQueuedSynchronizer

protected final boolean compareAndSetState(int expect, int update) {
        // See below for intrinsics setup to support this
        return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
    }

在Java中,這個操作如果更新成功,返回true,失敗返回false,通過這個機制,可以定義鎖(樂觀鎖)。
如三個線程A,B,C,在目標值爲0的情況下,同時執行compareAndSetState(0,1) 去修改它
期望值是0,更新值是1,因爲是原子操作,在第一個線程操作成功之後目標值變爲1,返回true
所以另外兩個線程就因爲期望值爲0不等於1,返回false。
我們可以理解爲,返回true的線程拿到了鎖。

最終調用的Java類是sun.misc.Unsafe

park 阻塞

Java中可以通過unsafe.park()去阻塞(停止)一個線程,也可以通過unsafe.unpark()讓一個阻塞線程恢復繼續執行

unsafe.park()

阻塞(停止)當前線程

public native void park(boolean isAbsolute, long time); 

根據debug測試,此方法能停止線程自己,最後通過其他線程喚醒

unsafe.unpark()

取消阻塞(喚醒)線程

public native void unpark(Object thread);

根據debug測試,此方法可以喚醒其他被park調用阻塞的線程

park與interrupt的區別

interrupt是Thread類的的API,park是Unsafe類的API,兩者是有區別的。
測試瞭解,Thread.currentThread().interrupt(),線程會繼續運行,而Unsafe.park(Thread.currentThread())就是直接阻塞線程,不繼續運行代碼。

獲取鎖

線程cas操作失敗,可以park阻塞自己,讓其他擁有鎖的線程在unlock的時候釋放自己,達到鎖的效果

java.util.concurrent.locks.ReentrantLock的lock方法是

public void lock() {
        sync.lock();
    }

而sync的實現類其中一個是java.util.concurrent.locks.ReentrantLock.NonfairSync 不公平鎖,它的邏輯比較直接

/**
NonfairSync
*/
final void lock() {
    if (compareAndSetState(0, 1))//cas操作,如果true 則表示操作成功,獲取鎖
        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) { 
			//如果期望值爲0,內存值也爲0,再次嘗試獲取鎖(此時其他線程也可能嘗試獲取鎖)
                if (compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current); //第二次獲取成功,放回true
                    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; //沒有獲取到鎖,返回false,則 !tryAcquire(arg) 爲true,執行acquireQueued(addWaiter(Node.EXCLUSIVE), arg)
        }

獲取鎖失敗,線程會進入循環,acquireQueued 方法中for是個無限循環,除非獲取鎖成功後,纔會return。

//獲取鎖失敗後,準備阻塞線程(自己)
//阻塞之前,添加節點存放到鏈表,其他線程可以通過這個鏈表喚醒此線程
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)) {//cas操作
                pred.next = node;
                return node;
            }
        }
        enq(node);
        return node;
    }

// 在此方法直到獲取鎖成功纔會跳出循環
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; //獲取鎖成功之後纔會return跳出此方法
                }
                if (shouldParkAfterFailedAcquire(p, node) && //如果滿足阻塞條件
                    parkAndCheckInterrupt()) 
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    private final boolean parkAndCheckInterrupt() {
        LockSupport.park(this);//停止線程(自己)
        return Thread.interrupted();
    }

釋放鎖

一個線程拿到鎖之後,執行完關鍵代碼,必須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;
    }
//java.util.concurrent.locks.ReentrantLock.Sync 的tryRelease
 protected final boolean tryRelease(int releases) {
            int c = getState() - releases; //這裏一般是 1 - 1 = 0
            if (Thread.currentThread() != getExclusiveOwnerThread()) //只能是鎖的擁有者釋放鎖
                throw new IllegalMonitorStateException();
            boolean free = false;
            if (c == 0) {
                free = true;
                setExclusiveOwnerThread(null);
            }
            setState(c); //設置state爲0,相當於釋放鎖,讓其他線程compareAndSetState(0, 1)可能成功
			
            return free;
        }

protected final void setState(int newState) {
        state = newState; //沒有cas操作
    }

setState不做cas操作是因爲,只有擁有鎖的線程才調用unlock,不存才併發混亂問題

其他線程沒拿到鎖不會設值成功,其他線程在此線程設置state爲0之前,compareAndSetState(0, 1)都會失敗,拿不到鎖,此線程設置state爲0之後,其他線程compareAndSetState(0, 1)纔有可能成功,返回true從而拿到鎖

釋放線程

線程在獲取鎖失敗後,有可能阻塞線程(自己),在阻塞之前把阻塞線程信息放入鏈表的
釋放鎖之後,線程會嘗試通過鏈表釋放其他線程(一個),讓一個阻塞線程恢復運行

private void unparkSuccessor(Node node) {
        /*
         * If status is negative (i.e., possibly needing signal) try
         * to clear in anticipation of signalling.  It is OK if this
         * fails or if status is changed by waiting thread.
         */
        int ws = node.waitStatus;
        if (ws < 0)
            compareAndSetWaitStatus(node, ws, 0);

        /*
         * Thread to unpark is held in successor, which is normally
         * just the next node.  But if cancelled or apparently null,
         * traverse backwards from tail to find the actual
         * non-cancelled successor.
         */
        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); //喚醒(釋放)被阻塞的線程
    }

阻塞線程被取消阻塞後如何拿到鎖(ReentrantLock中)

有時候線程被中斷後,喚醒繼續執行後面的代碼,
線程沒有拿到鎖之後主動阻塞自己的,但所還沒拿到,被喚醒之後怎麼去嘗試重新獲取鎖呢? 裏面有一個for循環

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();
    }

// 存好節點後,將線程(自己)中斷,等其他線程喚醒(自己)
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; //如果拿到鎖了,纔會return
                }
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt()) //沒拿到鎖時,主動中斷Thread.currentThread()
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

被喚醒後繼續執行compareAndSetState(0, 1)返回false沒拿到鎖,則繼續循環或阻塞

compareAndSetState(0, 1) 這個操作是獲取鎖的關鍵

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章