Java多线程并发编程(互斥锁Reentrant Lock)

 更新时间:2017年05月22日 23:42:01   投稿:mdxy-dxy  
这篇文章主要介绍了ReentrantLock 互斥锁,在同一时间只能被一个线程所占有,在被持有后并未释放之前,其他线程若想获得该锁只能等待或放弃,需要的朋友可以参考下

Java 中的锁通常分为两种:

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

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 doOtherThing, i = 0
Thread-1 doOtherThing, i = 1
Thread-1 doOtherThing, i = 2
Thread-1 doOtherThing, i = 3
Thread-1 doOtherThing, i = 4
Thread-1 doOtherThing, i = 5

结合 DEMO2 和 DEMO3 输出可见,锁的作用域在于 mReentrantLock,因为所来自于 mReentrantLock。

可终止等待

DEMO4:

public class Test {

  static final int TIMEOUT = 300;

  private static class Counter {

    private ReentrantLock mReentrantLock = new ReentrantLock();

    public void count() {
      try{
        //lock() 不可中断
        mReentrantLock.lock();
        // 模拟耗时,突出线程是否阻塞
        for (int i = 0; i < 6; i++) {
          long startTime = System.currentTimeMillis();
          while (true) {
            if (System.currentTimeMillis() - startTime > 100)
              break;
          }
          System.out.println(Thread.currentThread().getName() + ", i = " + i);
        }
      } finally {
        // 必须在 finally 释放锁
        mReentrantLock.unlock();
      }
    }

    public void doOtherThing(){
      try{
        //lockInterruptibly() 可中断,若线程没有中断,则获取锁
        mReentrantLock.lockInterruptibly();
        for (int i = 0; i < 6; i++) {
          // 模拟耗时,突出线程是否阻塞
          long startTime = System.currentTimeMillis();
          while (true) {
            if (System.currentTimeMillis() - startTime > 100)
              break;
          }
          System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i);
        }
      } catch (InterruptedException e) {
        System.out.println(Thread.currentThread().getName() + " 中断 ");
      }finally {
        // 若当前线程持有锁,则释放
        if(mReentrantLock.isHeldByCurrentThread()){
          mReentrantLock.unlock();
        }
      }
    }
  }

  public static void main(String[] var0) {
    final Counter counter = new Counter();
    new Thread(new Runnable() {
      @Override
      public void run() {
        counter.count();
      }
    }).start();
    Thread thread2 = new Thread(new Runnable() {
      @Override
      public void run() {
        counter.doOtherThing();
      }
    });
    thread2.start();
    long start = System.currentTimeMillis();
    while (true){
      if (System.currentTimeMillis() - start > TIMEOUT) {
        // 若线程还在运行,尝试中断
        if(thread2.isAlive()){
          System.out.println(" 不等了,尝试中断 ");
          thread2.interrupt();
        }
        break;
      }
    }
  }
}

DEMO4 输出:

Thread-0, i = 0
Thread-0, i = 1
Thread-0, i = 2
不等了,尝试中断
Thread-1 中断
Thread-0, i = 3
Thread-0, i = 4
Thread-0, i = 5

线程 thread2 等待 300ms 后 timeout,中断等待成功。

若把 TIMEOUT 改成 3000ms,输出结果:(正常运行)

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 doOtherThing, i = 0
Thread-1 doOtherThing, i = 1
Thread-1 doOtherThing, i = 2
Thread-1 doOtherThing, i = 3
Thread-1 doOtherThing, i = 4
Thread-1 doOtherThing, i = 5

定时锁

DEMO5:

public class Test {

  static final int TIMEOUT = 3000;

  private static class Counter {

    private ReentrantLock mReentrantLock = new ReentrantLock();

    public void count() {
      try{
        //lock() 不可中断
        mReentrantLock.lock();
        // 模拟耗时,突出线程是否阻塞
        for (int i = 0; i < 6; i++) {
          long startTime = System.currentTimeMillis();
          while (true) {
            if (System.currentTimeMillis() - startTime > 100)
              break;
          }
          System.out.println(Thread.currentThread().getName() + ", i = " + i);
        }
      } finally {
        // 必须在 finally 释放锁
        mReentrantLock.unlock();
      }
    }

    public void doOtherThing(){
      try{
        //tryLock(long timeout, TimeUnit unit) 尝试获得锁
        boolean isLock = mReentrantLock.tryLock(300, TimeUnit.MILLISECONDS);
        System.out.println(Thread.currentThread().getName() + " isLock:" + isLock);
        if(isLock){
          for (int i = 0; i < 6; i++) {
            // 模拟耗时,突出线程是否阻塞
            long startTime = System.currentTimeMillis();
            while (true) {
              if (System.currentTimeMillis() - startTime > 100)
                break;
            }
            System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i);
          }
        }else{
          System.out.println(Thread.currentThread().getName() + " timeout");
        }
      } catch (InterruptedException e) {
        System.out.println(Thread.currentThread().getName() + " 中断 ");
      }finally {
        // 若当前线程持有锁,则释放
        if(mReentrantLock.isHeldByCurrentThread()){
          mReentrantLock.unlock();
        }
      }
    }
  }

  public static void main(String[] var0) {
    final Counter counter = new Counter();
    new Thread(new Runnable() {
      @Override
      public void run() {
        counter.count();
      }
    }).start();
    Thread thread2 = new Thread(new Runnable() {
      @Override
      public void run() {
        counter.doOtherThing();
      }
    });
    thread2.start();
  }
}

DEMO5 输出:

Thread-0, i = 0
Thread-0, i = 1
Thread-0, i = 2
Thread-1 isLock:false
Thread-1 timeout
Thread-0, i = 3
Thread-0, i = 4
Thread-0, i = 5

tryLock() 尝试获得锁,tryLock(long timeout, TimeUnit unit) 在给定的 timeout 时间内尝试获得锁,若超时,则不带锁往下走,所以必须加以判断。

ReentrantLock or synchronized

ReentrantLock 、synchronized 之间如何选择?

ReentrantLock 在性能上 比 synchronized 更胜一筹。

ReentrantLock 需格外小心,因为需要显式释放锁,lock() 后记得 unlock(),而且必须在 finally 里面,否则容易造成死锁。
synchronized 隐式自动释放锁,使用方便。

ReentrantLock 扩展性好,可中断锁,定时锁,自由控制。
synchronized 一但进入阻塞等待,则无法中断等待。

相关文章

  • java+sqlserver实现学生信息管理系统

    java+sqlserver实现学生信息管理系统

    这篇文章主要介绍了利用java和sqlserver实现学生信息管理系统,违章内容主要建立了与sqlserver数据库的连接开始展开内容,能学到了解JDBC执行SQL的语法,需要的朋友可以参考一下
    2021-12-12
  • 使用@Autowired 注入RedisTemplate报错的问题及解决

    使用@Autowired 注入RedisTemplate报错的问题及解决

    这篇文章主要介绍了使用@Autowired 注入RedisTemplate报错的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Spring中事务用法示例及实现原理详解

    Spring中事务用法示例及实现原理详解

    这篇文章主要给大家介绍了关于Spring中事务用法示例及实现原理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • 详解Spring连接数据库的几种常用的方式

    详解Spring连接数据库的几种常用的方式

    本篇文章主要介绍了Spring连接数据库的几种常用的方式,具有一定的参考价值,有需要的可以了解一下。
    2016-12-12
  • java解析xml的4种方式的优缺点对比及实现详解

    java解析xml的4种方式的优缺点对比及实现详解

    这篇文章主要介绍了java解析xml的4种方式的优缺点对比及实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 使用IDEA配置Mybatis-Plus框架图文详解

    使用IDEA配置Mybatis-Plus框架图文详解

    这篇文章主要介绍了使用IDEA配置Mybatis-Plus框架,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • 提高开发效率Live Templates使用技巧详解

    提高开发效率Live Templates使用技巧详解

    这篇文章主要为大家介绍了提高开发效率Live Templates使用技巧详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • vue+springboot读取git的markdown文件并展示功能

    vue+springboot读取git的markdown文件并展示功能

    Markdown-it 是一个用于解析和渲染 Markdown 标记语言的 JavaScript 库,使用 Markdown-it,你可以将 Markdown 文本解析为 HTML 输出,并且可以根据需要添加功能、扩展语法或修改解析行为,本文介绍vue+springboot读取git的markdown文件并展示,感兴趣的朋友一起看看吧
    2024-01-01
  • MybatisPlus 不修改全局策略和字段注解如何将字段更新为null

    MybatisPlus 不修改全局策略和字段注解如何将字段更新为null

    这篇文章主要介绍了MybatisPlus 不修改全局策略和字段注解如何将字段更新为null,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Java中泛型通配符的使用方法示例

    Java中泛型通配符的使用方法示例

    这篇文章主要介绍了Java中泛型通配符的使用方法,结合实例形式分析了java中泛型通配符的功能、语法及在泛型类创建泛型对象中的使用方法,需要的朋友可以参考下
    2019-08-08

最新评论