Java并发之ReentrantLock详解

2024-08-27 11:38

本文主要是介绍Java并发之ReentrantLock详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文:

http://blog.csdn.net/lipeng_bigdata/article/details/52154637

     一、入题

        ReentrantLock是Java并发包中互斥锁,它有公平锁和非公平锁两种实现方式,以lock()为例,其使用方式为:

[java] view plain copy
  1. ReentrantLock takeLock = new ReentrantLock();  
  2.   
  3. // 获取锁  
  4. takeLock.lock();  
  5.   
  6. try {  
  7.     
  8.   // 业务逻辑  
  9.     
  10. finally {  
  11.   // 释放锁  
  12.   takeLock.unlock();  
  13. }  
         那么,ReentrantLock内部是如何实现锁的呢?接下来我们就以JDK1.7中的ReentrantLock的lock()为例详细研究下。


        二、ReentrantLock类的结构

        ReentrantLock类实现了Lock和java.io.Serializable接口,其内部有一个实现锁功能的关键成员变量Sync类型的sync,定义如下:

[java] view plain copy
  1. /** Synchronizer providing all implementation mechanics */  
  2. private final Sync sync;  
        而这个Sync是继承了AbstractQueuedSynchronizer的内部抽象类,主要由它负责实现锁的功能。关于AbstractQueuedSynchronizer我们会在以后详细介绍,你只要知道它内部存在一个获取锁的等待队列及其互斥锁状态下的int状态位(0当前没有线程持有该锁、n存在某线程重入锁n次)即可,该状态位也可用于其它诸如共享锁、信号量等功能。
        Sync在ReentrantLock中有两种实现类:NonfairSync、FairSync,正好对应了ReentrantLock的非公平锁、公平锁两大类型。

        

        三、获取锁主体流程

        ReentrantLock的锁功能主要是通过继承了AbstractQueuedSynchronizer的内部类Sync来实现的,其lock()获取锁的主要流程如下:

        

        首先,ReentrantLock的lock()方法会调用其内部成员变量sync的lock()方法;

        其次,sync的非公平锁NonfairSync或公平锁FairSync实现了父类AbstractQueuedSynchronizer的lock()方法,其会调用acquire()方法;

        然后,acquire()方法则在sync父类AbstractQueuedSynchronizer中实现,它只有一段代码:

[java] view plain copy
  1. public final void acquire(int arg) {  
  2.     if (!tryAcquire(arg) &&  
  3.         acquireQueued(addWaiter(Node.EXCLUSIVE), arg))  
  4.         selfInterrupt();  
  5. }  
      通过tryAcquire()方法试图获取锁,获取到直接返回结果,否则通过嵌套调用acquireQueued()、addWaiter()方法将请求获取锁的线程加入等待队列,如果成功的话,将当前请求线程阻塞,and,over!

         队列如何实现及如何添加到队列中以后再做详细分析!这里只关注ReentrantLock的实现逻辑。

        上述就是公平锁、非公平锁实现获取锁的主要流程,而针对每种锁来说,其实现方式有很大差别,主要就体现在各自实现类的lock()和tryAcquire()方法中。在sync的抽象类Sync及其抽象父类AbstractQueuedSynchronizer中,lock()方法和tryAcquire()方法被定义为抽象方法或者未实现,而是由具体子类去实现:

[java] view plain copy
  1. /** 
  2.  * Performs {@link Lock#lock}. The main reason for subclassing 
  3.  * is to allow fast path for nonfair version. 
  4.  */  
  5. abstract void lock();  
[java] view plain copy
  1.     /** 
  2.      * Attempts to acquire in exclusive mode. This method should query 
  3.      * if the state of the object permits it to be acquired in the 
  4.      * exclusive mode, and if so to acquire it. 
  5.      * 
  6.      * <p>This method is always invoked by the thread performing 
  7.      * acquire.  If this method reports failure, the acquire method 
  8.      * may queue the thread, if it is not already queued, until it is 
  9.      * signalled by a release from some other thread. This can be used 
  10.      * to implement method {@link Lock#tryLock()}. 
  11.      * 
  12.      * <p>The default 
  13.      * implementation throws {@link UnsupportedOperationException}. 
  14.      * 
  15.      * @param arg the acquire argument. This value is always the one 
  16.      *        passed to an acquire method, or is the value saved on entry 
  17.      *        to a condition wait.  The value is otherwise uninterpreted 
  18.      *        and can represent anything you like. 
  19.      * @return {@code true} if successful. Upon success, this object has 
  20.      *         been acquired. 
  21.      * @throws IllegalMonitorStateException if acquiring would place this 
  22.      *         synchronizer in an illegal state. This exception must be 
  23.      *         thrown in a consistent fashion for synchronization to work 
  24.      *         correctly. 
  25.      * @throws UnsupportedOperationException if exclusive mode is not supported 
  26.      */  
  27.     protected boolean tryAcquire(int arg) {  
  28.         throw new UnsupportedOperationException();  
  29.     }  
        下面,我们分别研究下非公平锁和公平锁的实现。


        四、非公平锁NonfairSync

        1、lock()方法

[java] view plain copy
  1. /** 
  2.  * Performs lock.  Try immediate barge, backing up to normal 
  3.  * acquire on failure. 
  4.  */  
  5. final void lock() {  
  6.     if (compareAndSetState(01))  
  7.         setExclusiveOwnerThread(Thread.currentThread());  
  8.     else  
  9.         acquire(1);  
  10. }  
        通过代码可以看到,非公平锁上来就无视等待队列的存在而抢占锁,通过基于CAS操作的compareAndSetState(0, 1)方法,试图修改当前锁的状态,这个0表示AbstractQueuedSynchronizer内部的一种状态,针对互斥锁则是尚未有线程持有该锁,而>=1则表示存在线程持有该锁,并重入对应次数,这个上来就CAS的操作也是非公共锁的一种体现,CAS操作成功的话,则将当前线程设置为该锁的唯一拥有者。

        抢占不成功的话,则调用父类的acquire()方法,按照上面讲的,继而会调用tryAcquire()方法,这个方法也是由最终实现类NonfairSync实现的,如下:

[java] view plain copy
  1. protected final boolean tryAcquire(int acquires) {  
  2.     return nonfairTryAcquire(acquires);  
  3. }  
        2、tryAcquire()

        而这个nonfairTryAcquire()方法实现如下:

[java] view plain copy
  1. /** 
  2.  * Performs non-fair tryLock.  tryAcquire is 
  3.  * implemented in subclasses, but both need nonfair 
  4.  * try for trylock method. 
  5.  */  
  6. final boolean nonfairTryAcquire(int acquires) {  
  7.     final Thread current = Thread.currentThread();  
  8.     int c = getState();  
  9.     if (c == 0) {  
  10.         if (compareAndSetState(0, acquires)) {  
  11.             setExclusiveOwnerThread(current);  
  12.             return true;  
  13.         }  
  14.     }  
  15.     else if (current == getExclusiveOwnerThread()) {  
  16.         int nextc = c + acquires;  
  17.         if (nextc < 0// overflow  
  18.             throw new Error("Maximum lock count exceeded");  
  19.         setState(nextc);  
  20.         return true;  
  21.     }  
  22.     return false;  
  23. }  
        还是上来先判断锁的状态,通过CAS来抢占,抢占成功,直接返回true,如果锁的持有者线程为当前线程的话,则通过累加状态标识重入次数。抢占不成功,或者锁的本身持有者不是当前线程,则返回false,继而后续通过进入等待队列的方式排队获取锁。可以通过以下简单的图来理解:

        


        五、公平锁FairSync

        1、lock()

        公平锁的lock()方法就比较简单了,直接调用acquire()方法,如下:

[java] view plain copy
  1. final void lock() {  
  2.     acquire(1);  
  3. }  

        2、tryAcquire()

        公平锁的tryAcquire()方法也相对较简单,如下:

[java] view plain copy
  1. /** 
  2.  * Fair version of tryAcquire.  Don't grant access unless 
  3.  * recursive call or no waiters or is first. 
  4.  */  
  5. protected final boolean tryAcquire(int acquires) {  
  6.     final Thread current = Thread.currentThread();  
  7.     int c = getState();  
  8.     if (c == 0) {  
  9.         if (!hasQueuedPredecessors() &&  
  10.             compareAndSetState(0, acquires)) {  
  11.             setExclusiveOwnerThread(current);  
  12.             return true;  
  13.         }  
  14.     }  
  15.     else if (current == getExclusiveOwnerThread()) {  
  16.         int nextc = c + acquires;  
  17.         if (nextc < 0)  
  18.             throw new Error("Maximum lock count exceeded");  
  19.         setState(nextc);  
  20.         return true;  
  21.     }  
  22.     return false;  
  23. }  
        当前线程会在得到当前锁状态为0,即没有线程持有该锁,并且通过!hasQueuedPredecessors()判断当前等待队列没有前继线程(也就是说,没有比我优先级更高的线程在请求锁了)获取锁的情况下,通过CAS抢占锁,并设置自己为锁的当前拥有者,当然,如果是重入的话,和非公平锁处理一样,通过累加状态位标记重入次数。

        而一旦等待队列中有等待者,或当前线程抢占锁失败,则它会乖乖的进入等待队列排队等待。公平锁的实现大致如下:

         


        六、默认实现

        ReentrantLock的默认实现为非公平锁,如下:

[java] view plain copy
  1. /** 
  2.  * Creates an instance of {@code ReentrantLock}. 
  3.  * This is equivalent to using {@code ReentrantLock(false)}. 
  4.  */  
  5. public ReentrantLock() {  
  6.     sync = new NonfairSync();  
  7. }  
        当然,你也可以通过另外一个构造方法指定锁的实现方式,如下:

[java] view plain copy
  1. /** 
  2.  * Creates an instance of {@code ReentrantLock} with the 
  3.  * given fairness policy. 
  4.  * 
  5.  * @param fair {@code true} if this lock should use a fair ordering policy 
  6.  */  
  7. public ReentrantLock(boolean fair) {  
  8.     sync = fair ? new FairSync() : new NonfairSync();  
  9. }  


        七、其它
        即便是公平锁,如果通过不带超时时间限制的tryLock()的方式获取锁的话,它也是不公平的,因为其内部调用的是sync.nonfairTryAcquire()方法,无论抢到与否,都会同步返回。如下:

[java] view plain copy
  1. public boolean tryLock() {  
  2.     return sync.nonfairTryAcquire(1);  
  3. }  

        但是带有超时时间限制的tryLock(long timeout, TimeUnit unit)方法则不一样,还是会遵循公平或非公平的原则的,如下:

[java] view plain copy
  1. public boolean tryLock(long timeout, TimeUnit unit)  
  2.         throws InterruptedException {  
  3.     return sync.tryAcquireNanos(1, unit.toNanos(timeout));  
  4. }  
        其它流程都比较简单,读者可自行翻阅Java源码查看!

这篇关于Java并发之ReentrantLock详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111563

相关文章

Linux线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick