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

相关文章

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Redis Pipeline(管道) 详解

《RedisPipeline(管道)详解》Pipeline管道是Redis提供的一种批量执行命令的机制,通过将多个命令一次性发送到服务器并统一接收响应,减少网络往返次数(RTT),显著提升执行效率... 目录Redis Pipeline 详解1. Pipeline 的核心概念2. 工作原理与性能提升3. 核

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

JAVA保证HashMap线程安全的几种方式

《JAVA保证HashMap线程安全的几种方式》HashMap是线程不安全的,这意味着如果多个线程并发地访问和修改同一个HashMap实例,可能会导致数据不一致和其他线程安全问题,本文主要介绍了JAV... 目录1. 使用 Collections.synchronizedMap2. 使用 Concurren

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp