本文共 6951 字,大约阅读时间需要 23 分钟。
在JDK5.0版本之前,重入锁的性能远远好于synchronized关键字,JDK6.0版本之后synchronized 得到了大量的优化,二者性能也不分伯仲,但是重入锁是可以完全替代synchronized关键字的。除此之外,重入锁又自带一系列高逼格UBFF:可中断响应、锁申请等待限时、公平锁。另外可以结合Condition来使用,使其更是逼格满满。
先来盘花生米:
package somhu;import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockTest implements Runnable{
public static ReentrantLock lock = new ReentrantLock(); public static int i = 0;@Override
public void run() { for (int j = 0; j < 10000; j++) { lock.lock(); // 看这里就可以 //lock.lock(); ① try { i++; } finally { lock.unlock(); // 看这里就可以 //lock.unlock();② } } }public static void main(String[] args) throws InterruptedException {
ReentrantLockTest test = new ReentrantLockTest(); Thread t1 = new Thread(test); Thread t2 = new Thread(test); t1.start();t2.start(); t1.join(); t2.join(); // main线程会等待t1和t2都运行完再执行以后的流程 System.err.println(i); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 从上可以看出,使用重入锁进行加锁是一种显式操作,通过何时加锁与释放锁使重入锁对逻辑控制的灵活性远远大于synchronized关键字。同时,需要注意,有加锁就必须有释放锁,而且加锁与释放锁的分数要相同,这里就引出了“重”字的概念,如上边代码演示,放开①、②处的注释,与原来效果一致。硬菜来了:
1、中断响应 对于synchronized块来说,要么获取到锁执行,要么持续等待。而重入锁的中断响应功能就合理地避免了这样的情况。比如,一个正在等待获取锁的线程被“告知”无须继续等待下去,就可以停止工作了。直接上代码,来演示使用重入锁如何解决死锁: 1 package somhu;import java.util.concurrent.locks.ReentrantLock;
public class KillDeadlock implements Runnable{
public static ReentrantLock lock1 = new ReentrantLock(); public static ReentrantLock lock2 = new ReentrantLock(); int lock;public KillDeadlock(int lock) {
this.lock = lock; }@Override
public void run() { try { if (lock == 1) { lock1.lockInterruptibly(); // 以可以响应中断的方式加锁 try { Thread.sleep(500); } catch (InterruptedException e) {} lock2.lockInterruptibly(); } else { lock2.lockInterruptibly(); // 以可以响应中断的方式加锁 try { Thread.sleep(500); } catch (InterruptedException e) {} lock1.lockInterruptibly(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { if (lock1.isHeldByCurrentThread()) lock1.unlock(); // 注意判断方式 if (lock2.isHeldByCurrentThread()) lock2.unlock(); System.err.println(Thread.currentThread().getId() + "退出!"); } }public static void main(String[] args) throws InterruptedException {
KillDeadlock deadLock1 = new KillDeadlock(1); KillDeadlock deadLock2 = new KillDeadlock(2); Thread t1 = new Thread(deadLock1); Thread t2 = new Thread(deadLock2); t1.start();t2.start(); Thread.sleep(1000); t2.interrupt(); // ③ } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 t1、t2线程开始运行时,会分别持有lock1和lock2而请求lock2和lock1,这样就发生了死锁。但是,在③处给t2线程状态标记为中断后,持有重入锁lock2的线程t2会响应中断,并不再继续等待lock1,同时释放了其原本持有的lock2,这样t1获取到了lock2,正常执行完成。t2也会退出,但只是释放了资源并没有完成工作。2、锁申请等待限时
可以使用 tryLock()或者tryLock(long timeout, TimeUtil unit) 方法进行一次限时的锁等待。前者不带参数,这时线程尝试获取锁,如果获取到锁则继续执行,如果锁被其他线程持有,则立即返回 false ,也就是不会使当前线程等待,所以不会产生死锁。
后者带有参数,表示在指定时长内获取到锁则继续执行,如果等待指定时长后还没有获取到锁则返回false。上代码:
package somhu;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;public class TryLockTest implements Runnable{
public static ReentrantLock lock = new ReentrantLock();@Override
public void run() { try { if (lock.tryLock(1, TimeUnit.SECONDS)) { // 等待1秒 Thread.sleep(2000); //休眠2秒 } else { System.err.println(Thread.currentThread().getName() + "获取锁失败!"); } } catch (Exception e) { if (lock.isHeldByCurrentThread()) lock.unlock(); } }public static void main(String[] args) throws InterruptedException {
TryLockTest test = new TryLockTest(); Thread t1 = new Thread(test); t1.setName("线程1"); Thread t2 = new Thread(test); t1.setName("线程2"); t1.start();t2.start(); } } /** * 运行结果: * 线程2获取锁失败! */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 上述示例中,t1先获取到锁,并休眠2秒,这时t2开始等待,等待1秒后依然没有获取到锁,就不再继续等待,符合预期结果。3、公平锁
所谓公平锁,就是按照时间先后顺序,使先等待的线程先得到锁,而且,公平锁不会产生饥饿锁,也就是只要排队等待,最终能等待到获取锁的机会。使用重入锁(默认是非公平锁)创建公平锁:public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync(); } 1 2 3 上代码:package somhu;
import java.util.concurrent.locks.ReentrantLock;
public class FairLockTest implements Runnable{
public static ReentrantLock lock = new ReentrantLock(true);@Override
public void run() { while (true) { try { lock.lock(); System.err.println(Thread.currentThread().getName() + "获取到了锁!"); } finally { lock.unlock(); } } }public static void main(String[] args) throws InterruptedException {
FairLockTest test = new FairLockTest(); Thread t1 = new Thread(test, "线程1"); Thread t2 = new Thread(test, "线程2"); t1.start();t2.start(); } } /** * 运行结果: * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * 线程1获取到了锁! * 线程2获取到了锁! * ......(上边是截取的一段) */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 可以发现,t1和t2交替获取到锁。如果是非公平锁,会发生t1运行了许多遍后t2才开始运行的情况。ReentrantLock 配合 Conditond 使用
配合关键字synchronized使用的方法如:await()、notify()、notifyAll(),同样配合ReentrantLock 使用的Conditon提供了以下方法:public interface Condition {
void await() throws InterruptedException; // 类似于Object.wait() void awaitUninterruptibly(); // 与await()相同,但不会再等待过程中响应中断 long awaitNanos(long nanosTimeout) throws InterruptedException; boolean await(long time, TimeUnit unit) throws InterruptedException; boolean awaitUntil(Date deadline) throws InterruptedException; void signal(); // 类似于Obejct.notify() void signalAll(); } 1 2 3 4 5 6 7 8 9 ReentrantLock 实现了Lock接口,可以通过该接口提供的newCondition()方法创建Condition对象:public interface Lock {
void lock(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit) throws InterruptedException; void unlock(); Condition newCondition(); } 1 2 3 4 5 6 7 8 上代码:package somhu;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;public class ReentrantLockWithConditon implements Runnable{
public static ReentrantLock lock = new ReentrantLock(true); public static Condition condition = lock.newCondition();@Override
public void run() { lock.newCondition(); try { lock.lock(); System.err.println(Thread.currentThread().getName() + "-线程开始等待..."); condition.await(); System.err.println(Thread.currentThread().getName() + "-线程继续进行了"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }public static void main(String[] args) throws InterruptedException {
ReentrantLockWithConditon test = new ReentrantLockWithConditon(); Thread t = new Thread(test, "线程ABC"); t.start(); Thread.sleep(1000); System.err.println("过了1秒后..."); lock.lock(); condition.signal(); // 调用该方法前需要获取到创建该对象的锁否则会产生 // java.lang.IllegalMonitorStateException异常 lock.unlock(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 好了,到这里重入锁ReentrantLock的基本使用方法就介绍完成了! --------------------- 作者:Somhu 来源:CSDN 原文:https://blog.csdn.net/Somhu/article/details/78874634 版权声明:本文为博主原创文章,转载请附上博文链接!