通过关键字 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 输出: