通過關鍵字 synchronized 獲取的鎖,我們稱為同步鎖,上一篇有介紹到:Java 多線程并發編程 Synchronized 關鍵字。
java.util.concurrent(JUC)包里的鎖,如通過繼承接口 Lock 而實現的 ReentrantLock(互斥鎖),繼承 ReadWriteLock 實現的 ReentrantReadWriteLock(讀寫鎖)。
本篇主要介紹 ReentrantLock(互斥鎖)。
ReentrantLock(互斥鎖)
ReentrantLock 互斥鎖,在同一時間只能被一個線程所占有,在被持有后并未釋放之前,其他線程若想獲得該鎖只能等待或放棄。
ReentrantLock 互斥鎖是可重入鎖,即某一線程可多次獲得該鎖。
公平鎖 and 非公平鎖
public ReentrantLock() { sync = new NonfairSync(); } public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }
由 ReentrantLock 的構造函數可見,在實例化 ReentrantLock 的時候我們可以選擇實例化一個公平鎖或非公平鎖,而默認會構造一個非公平鎖。
公平鎖與非公平鎖區別在于競爭鎖時的有序與否。公平鎖可確保有序性(FIFO 隊列),非公平鎖不能確保有序性(即使也有 FIFO 隊列)。
然而,公平是要付出代價的,公平鎖比非公平鎖要耗性能,所以在非必須確保公平的條件下,一般使用非公平鎖可提高吞吐率。所以 ReentrantLock 默認的構造函數也是“不公平”的。
一般使用
DEMO1:
public class Test { private static class Counter { private ReentrantLock mReentrantLock = new ReentrantLock(); public void count() { mReentrantLock.lock(); try { for (int i = 0; i < 6; i++) { System.out.println(Thread.currentThread().getName() + ", i = " + i); } } finally { // 必須在 finally 釋放鎖 mReentrantLock.unlock(); } } } private static class MyThread extends Thread { private Counter mCounter; public MyThread(Counter counter) { mCounter = counter; } @Override public void run() { super.run(); mCounter.count(); } } public static void main(String[] var0) { Counter counter = new Counter(); // 注:myThread1 和 myThread2 是調用同一個對象 counter MyThread myThread1 = new MyThread(counter); MyThread myThread2 = new MyThread(counter); myThread1.start(); myThread2.start(); } }
DEMO1 輸出:
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1, i = 0 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-1, i = 4 Thread-1, i = 5
DEMO1 僅使用了 ReentrantLock 的 lock 和 unlock 來提現一般鎖的特性,確保線程的有序執行。此種場景 synchronized 也適用。
鎖的作用域
DEMO2:
public class Test { private static class Counter { private ReentrantLock mReentrantLock = new ReentrantLock(); public void count() { for (int i = 0; i < 6; i++) { mReentrantLock.lock(); // 模擬耗時,突出線程是否阻塞 try{ Thread.sleep(100); System.out.println(Thread.currentThread().getName() + ", i = " + i); } catch (InterruptedException e) { e.printStackTrace(); } finally { // 必須在 finally 釋放鎖 mReentrantLock.unlock(); } } } public void doOtherThing(){ for (int i = 0; i < 6; i++) { // 模擬耗時,突出線程是否阻塞 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i); } } } public static void main(String[] var0) { final Counter counter = new Counter(); new Thread(new Runnable() { @Override public void run() { counter.count(); } }).start(); new Thread(new Runnable() { @Override public void run() { counter.doOtherThing(); } }).start(); } }
DEMO2 輸出:
Thread-0, i = 0 Thread-1 doOtherThing, i = 0 Thread-0, i = 1 Thread-1 doOtherThing, i = 1 Thread-0, i = 2 Thread-1 doOtherThing, i = 2 Thread-0, i = 3 Thread-1 doOtherThing, i = 3 Thread-0, i = 4 Thread-1 doOtherThing, i = 4 Thread-0, i = 5 Thread-1 doOtherThing, i = 5
DEMO3:
public class Test { private static class Counter { private ReentrantLock mReentrantLock = new ReentrantLock(); public void count() { for (int i = 0; i < 6; i++) { mReentrantLock.lock(); // 模擬耗時,突出線程是否阻塞 try{ Thread.sleep(100); System.out.println(Thread.currentThread().getName() + ", i = " + i); } catch (InterruptedException e) { e.printStackTrace(); } finally { // 必須在 finally 釋放鎖 mReentrantLock.unlock(); } } } public void doOtherThing(){ mReentrantLock.lock(); try{ for (int i = 0; i < 6; i++) { // 模擬耗時,突出線程是否阻塞 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i); } }finally { mReentrantLock.unlock(); } } } public static void main(String[] var0) { final Counter counter = new Counter(); new Thread(new Runnable() { @Override public void run() { counter.count(); } }).start(); new Thread(new Runnable() { @Override public void run() { counter.doOtherThing(); } }).start(); } }
DEMO3 輸出: