深入理解Java中的鎖(二)
- 2019 年 10 月 7 日
- 筆記
locks包結構層次

Lock 接口

代碼示例:
public class GetLockDemo { // 公平鎖 // static Lock lock =new ReentrantLock(true); // 非公平鎖 static Lock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { // 主線程 拿到鎖 lock.lock(); Thread thread = new Thread( () -> { // 子線程 獲取鎖(不死不休) System.out.println("begain to get lock..."); lock.lock(); System.out.println("succeed to get lock..."); // // 子線程 獲取鎖(淺嘗輒止) // boolean result = lock.tryLock(); // System.out.println("是否獲得到鎖:" + result); // // // 子線程 獲取鎖(過時不候) // try { // boolean result1 = lock.tryLock(5, TimeUnit.SECONDS); // System.out.println("是否獲得到鎖:" + result1); // } catch (InterruptedException e) { // e.printStackTrace(); // } // // // 子線程 獲取鎖(任人擺布) // try { // System.out.println("start to get lock Interruptibly"); // lock.lockInterruptibly(); // } catch (InterruptedException e) { // e.printStackTrace(); // System.out.println("dad asked me to stop..."); // } }); thread.start(); Thread.sleep(10000L); lock.unlock(); } }
結論:
- lock() 最常用
- lockInterruptibly() 方法一般更昂貴,有的實現類可能沒有實現 lockInterruptible() 方法。 只有真的需要用中斷時,才使用,使用前應看清實現類對該方法的描述。
Condition

condition代碼示例:
public class ConditionDemo { static Lock lock = new ReentrantLock(); static Condition condition = lock.newCondition(); public static void main(String[] args) throws InterruptedException { Thread thread = new Thread( () -> { lock.lock(); System.out.println("condition.await()"); try { condition.await(); System.out.println("here i am..."); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }); thread.start(); Thread.sleep(2000L); lock.lock(); condition.signalAll(); lock.unlock(); } }
ReetrantLock
ReentrantLock是可重入鎖,同一線程可以多次獲取到鎖

ReentrantLock實現原理分析
- ReentrantLock需要一個owner用來標記那個線程獲取到了鎖,一個count用來記錄加鎖的次數和一個waiters等待隊列用來存放沒有搶到鎖的線程列表
- 當有線程進來時,會先判斷count的值,如果count為0說明鎖沒有被佔用
- 然後通過CAS操作進行搶鎖
- 如果搶到鎖則count的值會加1,同時將owner設置為當前線程的引用
- 如果count不為0同時owner指向當前線程的引用,則將count的值加1
- 如果count不為0同時owner指向的不是當前線程的引用,則將線程放入等待隊列waiters中
- 如果CAS搶鎖失敗,則將線程放入等待隊列waiters中
- 當線程使用完鎖後,會釋放其持有的鎖,釋放鎖時會將count的值減1,如果count值為0則將owner設為null
- 如果count值不為0則會喚醒等待隊列頭部的線程進行搶鎖
手動實現ReentrantLock代碼示例:
public class MyReentrantLock implements Lock { // 標記重入次數的count值 private AtomicInteger count = new AtomicInteger(0); // 鎖的擁有者 private AtomicReference<Thread> owner = new AtomicReference<>(); // 等待隊列 private LinkedBlockingDeque<Thread> waiters = new LinkedBlockingDeque<>(); @Override public boolean tryLock() { // 判斷count是否為0,若count!=0,說明鎖被佔用 int ct = count.get(); if (ct != 0) { // 判斷鎖是否被當前線程佔用,若被當前線程佔用,做重入操作,count+=1 if (owner.get() == Thread.currentThread()) { count.set(ct + 1); return true; } else { // 若不是當前線程佔用,互斥,搶鎖失敗,return false return false; } } else { // 若count=0, 說明鎖未被佔用,通過CAS(0,1) 來搶鎖 if (count.compareAndSet(ct, ct + 1)) { // 若搶鎖成功,設置owner為當前線程的引用 owner.set(Thread.currentThread()); return true; } else { return false; } } } @Override public void lock() { // 嘗試搶鎖 if (!tryLock()) { // 如果失敗,進入等待隊列 waiters.offer(Thread.currentThread()); // 自旋 for (; ; ) { // 判斷是否是隊列頭部,如果是 Thread head = waiters.peek(); if (head == Thread.currentThread()) { // 再次嘗試搶鎖 if (!tryLock()) { // 若搶鎖失敗,掛起線程,繼續等待 LockSupport.park(); } else { // 若成功,就出隊列 waiters.poll(); return; } } else { // 如果不是隊列頭部,就掛起線程 LockSupport.park(); } } } } public boolean tryUnlock() { // 判斷,是否是當前線程佔有鎖,若不是,拋異常 if (owner.get() != Thread.currentThread()) { throw new IllegalMonitorStateException(); } else { // 如果是,就將count-1 若count變為0 ,則解鎖成功 int ct = count.get(); int nextc = ct - 1; count.set(nextc); // 判斷count值是否為0 if (nextc == 0) { owner.compareAndSet(Thread.currentThread(), null); return true; } else { return false; } } } @Override public void unlock() { // 嘗試釋放鎖 if (tryUnlock()) { // 獲取隊列頭部, 如果不為null則將其喚醒 Thread thread = waiters.peek(); if (thread != null) { LockSupport.unpark(thread); } } } @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { return false; } @Override public void lockInterruptibly() throws InterruptedException {} @Override public Condition newCondition() { return null; } }
synchronized VS Lock
synchronized: 優點:
- 使用簡單,語義清晰,哪裡需要點哪裡
- 由JVM提供,提供了多種優化方案(鎖粗化,鎖消除,偏向鎖,輕量級鎖)
- 鎖的釋放由虛擬機完成,不用人工干預,降低了死鎖的可能性 缺點: 悲觀的排他鎖,無法實現鎖的高級功能如公平鎖,讀寫鎖等
Lock:
優點:可以實現synchronized無法實現的鎖的高級功能如公平鎖,讀寫鎖等,同時還可以實現更多的功能 缺點:需手動釋放鎖unlock,使用不當容易造成死鎖
結論: 兩者都是可重入鎖,synchronized可以類比為傻瓜相機,提供了固定的功能,而Lock可以類比為單方,可以根據需要調節所需的功能