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

发布时间 - 2026-01-11 01:14:28    点击率:

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多线程并发  # 互斥锁  # Java并发编程之常用的多线程实现方式分析  # Java多线程并发编程和锁原理解析  # 深入理解Java多线程与并发编程  # Java多线程并发编程 Volatile关键字  # Java多线程之并发编程的核心AQS详解  # 互斥  # 死锁  # 等了  # 也有  # 还在  # 两种  # 时间内  # 来自于  # 上一篇  # 可以选择  # 不公平  # 不带  # 此种  # 多线程  # 如何选择  # 正常运行  # 包里  # 性好  # 与非  # 更胜 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: EditPlus中的正则表达式 实战(4)  魔方云NAT建站如何实现端口转发?  javascript基本数据类型及类型检测常用方法小结  深圳网站制作平台,深圳市做网站好的公司有哪些?  Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives  JavaScript中如何操作剪贴板_ClipboardAPI怎么用  jQuery中的100个技巧汇总  如何在Tomcat中配置并部署网站项目?  安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出  如何使用 jQuery 正确渲染 Instagram 风格的标签列表  Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】  Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法  Laravel怎么使用Collection集合方法_Laravel数组操作高级函数pluck与map【手册】  如何在腾讯云服务器快速搭建个人网站?  JavaScript数据类型有哪些_如何准确判断一个变量的类型  C#如何调用原生C++ COM对象详解  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?  常州企业网站制作公司,全国继续教育网怎么登录?  Laravel如何与Docker(Sail)协同开发?(环境搭建教程)  香港服务器选型指南:免备案配置与高效建站方案解析  Python文件操作最佳实践_稳定性说明【指导】  Laravel怎么实现验证码功能_Laravel集成验证码库防止机器人注册  5种Android数据存储方式汇总  Bootstrap CSS布局之列表  Laravel如何使用API Resources格式化JSON响应_Laravel数据资源封装与格式化输出  laravel服务容器和依赖注入怎么理解_laravel服务容器与依赖注入解析  Laravel如何集成微信支付SDK_Laravel使用yansongda-pay实现扫码支付【实战】  如何在IIS7中新建站点?详细步骤解析  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  Laravel如何从数据库删除数据_Laravel destroy和delete方法区别  高端云建站费用究竟需要多少预算?  如何确保西部建站助手FTP传输的安全性?  如何快速搭建自助建站会员专属系统?  Laravel如何处理CORS跨域请求?(配置示例)  Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】  javascript中数组(Array)对象和字符串(String)对象的常用方法总结  如何用西部建站助手快速创建专业网站?  Windows Hello人脸识别突然无法使用  Laravel怎么使用Intervention Image库处理图片上传和缩放  如何在云主机快速搭建网站站点?  Laravel如何生成API文档?(Swagger/OpenAPI教程)  Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】  如何实现建站之星域名转发设置?  php 三元运算符实例详细介绍  详解Android——蓝牙技术 带你实现终端间数据传输  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  微信小程序 配置文件详细介绍  如何快速生成ASP一键建站模板并优化安全性?  Windows10如何更改计算机工作组_Win10系统属性修改Workgroup