• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java多线程并发编程(互斥锁Reentrant Lock)

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了reentrant,reentrant是什么意思,fs reentrant,d reentrant,reentrant翻译等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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();
  }
</div>

由 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();
  }
}
</div>

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
</div>

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();
  }
}
</div>

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
</div>

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();
  }
}
</div>

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
</div>

结合 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 (tru



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28Spring Boot无缝集成MongoDB
  • 2017-05-28listview点击无效的处理方法(推荐)
  • 2017-05-28详解SpringBoot的事务管理
  • 2017-05-28EJB轻松进阶之一
  • 2017-05-28Java数据结构之队列(动力节点Java学院整理)
  • 2017-05-28java 实现微信服务器下载图片到自己服务器
  • 2017-05-28Java中IO流 字节流实例详解
  • 2017-05-28详解spring面向切面aop拦截器
  • 2017-05-28详谈Java多线程的几个常用关键字
  • 2017-05-28详解Spring通过@Value注解注入属性的几种方式

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • spring之Bean的生命周期详解
    • 详解Spring Boot实现日志记录 SLF4J
    • 使用Lucene实现一个简单的布尔搜索功能
    • springboot集成spring cache缓存示例代码
    • Java经典排序算法之希尔排序详解
    • Java中JDBC连接数据库详解
    • Java缓存池代码实例详解
    • java 基础之final、finally和finalize的区别
    • 详解SpringBoot的事务管理
    • 详解Java中Collections.sort排序

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有