ReentrantLock是JDK的并发包下locks包下的工具类,ReentrantLock比synchonized性能要好,但是jdk1.8之后synchonized的性能也得到大幅度提升,不亚于ReentrantLock。
锁的演变
ReentrantLock是由synchonized和对象锁演变而来。回忆以下之前使用synchronized和Object锁的方式:
public void method1(){ synchronized (this) { //对象锁 try { System.out.println("do method1.."); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }
public void method2(){ //类锁 synchronized (ObjectLock.class) { try { System.out.println("do method2.."); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }
private Object lock = new Object(); public void method3(){ //任何对象锁 synchronized (lock) { try { System.out.println("do method3.."); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }
而ReentrantLock的使用则灵活很多:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class UseReentrantLock { private Lock lock = new ReentrantLock(); public void method1(){ try { lock.lock(); System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1.."); Thread.sleep(1000); System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1.."); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public void method2(){ try { lock.lock(); System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2.."); Thread.sleep(2000); System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2.."); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { final UseReentrantLock ur = new UseReentrantLock(); Thread t1 = new Thread(new Runnable() { @Override public void run() { ur.method1(); ur.method2(); } }, "t1"); t1.start(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } //System.out.println(ur.lock.getQueueLength()); } }
打印:
当前线程:t1进入method1.. 当前线程:t1退出method1.. 当前线程:t1进入method2.. 当前线程:t1退出method2..
分析:我们定义了一个线程t1,在t1中执行method1和method2两个方法,这两个方法都加了ReentrantLock,那么在执行时如果method1先获得lock,就会执行method1的方法,unlock释放锁后,才能进入method2再次获得锁并执行method2,然后释放锁。
锁的通信
Object对象的wait和notify必须配合synchonized使用。而ReentrantLock配合Condition使用。
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class UseCondition { private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void method1(){ try { lock.lock(); System.out.println("当前线程:" + Thread.currentThread().getName() + "进入等待状态.."); Thread.sleep(3000); System.out.println("当前线程:" + Thread.currentThread().getName() + "释放锁.."); condition.await(); //阻塞并释放锁 类似于 Object wait System.out.println("当前线程:" + Thread.currentThread().getName() +"继续执行..."); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void method2(){ try { lock.lock(); System.out.println("当前线程:" + Thread.currentThread().getName() + "进入.."); Thread.sleep(3000); System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒.."); condition.signal(); //唤醒 不释放锁 类似于Object notify } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { final UseCondition uc = new UseCondition(); Thread t1 = new Thread(new Runnable() { @Override public void run() { uc.method1(); } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { uc.method2(); } }, "t2"); t1.start(); t2.start(); } }
打印:
当前线程:t1进入等待状态.. 当前线程:t1释放锁.. 当前线程:t2进入.. 当前线程:t2发出唤醒.. 当前线程:t1继续执行...
分析:我们定义t1和t2两个线程,t1执行method1,t2执行method2,t1进入method1后获得lock,进入等待,3s后调用condition.await();释放锁,进入阻塞状态。然后t2线程获得lock,3s后发出唤醒condition.signal();接着t1被唤醒,打印“继续执行...”
多Condition
我们可以通过1个lock对象产生多个Condition进行多线程之间的交互,很灵活。可以使得唤醒部分线程,而其他线程继续等待通知。
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class UseManyCondition { private ReentrantLock lock = new ReentrantLock(); private Condition c1 = lock.newCondition(); private Condition c2 = lock.newCondition(); public void m1(){ try { lock.lock(); System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待.."); c1.await(); System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续.."); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m2(){ try { lock.lock(); System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待.."); c1.await(); System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续.."); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m3(){ try { lock.lock(); System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待.."); c2.await(); System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续.."); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m4(){ try { lock.lock(); System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒.."); c1.signalAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void m5(){ try { lock.lock(); System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒.."); c2.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public static void main(String[] args) { final UseManyCondition umc = new UseManyCondition(); Thread t1 = new Thread(new Runnable() { @Override public void run() { umc.m1(); } },"t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { umc.m2(); } },"t2"); Thread t3 = new Thread(new Runnable() { @Override public void run() { umc.m3(); } },"t3"); Thread t4 = new Thread(new Runnable() { @Override public void run() { umc.m4(); } },"t4"); Thread t5 = new Thread(new Runnable() { @Override public void run() { umc.m5(); } },"t5"); t1.start(); // c1先阻塞 t2.start(); // c1先阻塞 t3.start(); // c2先阻塞 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } t4.start(); // 唤醒 c1 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } t5.start(); // 唤醒c2 } }
分析:定义5个线程:t1、t2、t3、t4、t5,分别执行m1、m2、m3、m4、m5这五个方法。m1和m2都阻塞了c1这个Condition,而m3阻塞的是c2这个Condition,m4中唤醒了c1,m5唤醒了c2,t1和t2跟t3先执行,那么线程t1和t2跟t3都分别被c1和c2阻塞,直到t4唤醒了c1,t1和t2继续执行,t5唤醒了t2,t3继续执行。
打印:
当前线程:t1进入方法m1等待.. 当前线程:t2进入方法m2等待.. 当前线程:t3进入方法m3等待.. 当前线程:t4唤醒.. 当前线程:t1方法m1继续.. 当前线程:t2方法m2继续.. 当前线程:t5唤醒.. 当前线程:t3方法m3继续..
了解:ReentrantLock默认非公平,性能比公平锁要高