可重入锁ReentrantLock源码解析

2024-06-04 08:32

本文主要是介绍可重入锁ReentrantLock源码解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

ReentrantLock:一个可重入互斥锁,具有与使用synchronized方法和语句访问的隐式监视锁相同的基本行为和语义,但具有扩展功能。比如实现公平锁、超时处理、锁中断。

ReentrantLock由最后一个成功锁定且尚未解锁的线程拥有 。当锁不是由另一个线程拥有时,调用lock的线程将成功获取锁。 如果当前线程已经拥有该锁,该方法将立即返回,加锁次数+1。 当前线程是否持有该锁可以使用方法isHeldByCurrentThread()和getHoldCount()进行检查。
该类的构造函数接受可选的公平参数,当设置true ,即可以使用公平锁。在竞争时,锁有利于授予等待最长时间的线程。 否则,该锁不保证任何特定的访问顺序。 锁竞争比较激烈时,公平锁比非公平锁的整体吞吐量小(通常要慢很多),但是具有更小的时间差异来获得锁定并保证更少的饥饿死锁,饥饿死锁的意思是某一个线程一直获取锁但是一直获取不到,这个在非公平锁的场景是会存在的。 但是请注意,锁的公平性不能保证线程调度的公平性。 因此,使用公平锁的线程可以连续获得多次。
建议的做法是unlock一定要在finally中调用,这样能保证锁的释放,避免死锁,如:

class X {private final ReentrantLock lock = new ReentrantLock();// ...public void m() {lock.lock();  // block until condition holds(阻塞,直到条件成立)try {// ... method body} finally {lock.unlock()}}}} 

除了实现Lock接口,这个类定义了许多public种protected方法用于检查锁的状态。 其中一些方法仅适用于仪器和监控。
此类的序列化与内置锁的操作方式相同:反序列化锁处于未锁定状态,无论其序列化时的状态如何。
此锁最多支持同一个线程的2147483647递归锁。 尝试超过此限制会导致Error从锁定方法中抛出。

我们看下Lock源码:

/*** Lock实现提供比使用synchronized方法和语句可以获得的更广泛的锁定操作。* 它们允许更灵活的结构化,可能具有完全不同的属性,并且可以支持多个相关联的对象Condition 。*/
public interface Lock {void lock();void lockInterruptibly() throws InterruptedException;boolean tryLock();boolean tryLock(long time, TimeUnit unit) throws InterruptedException;void unlock();Condition newCondition();
}

ReentrantLock使用了NonfairSyncFairSync这两个Sync的子类来实现公平锁与非公平锁,默认是非公平锁。所以我们先看看SyncNonfairSyncFairSync的实现。

一、Sync源码

	/*** Base of synchronization control for this lock. Subclassed* into fair and nonfair versions below. Uses AQS state to* represent the number of holds on the lock.*/abstract static class Sync extends AbstractQueuedSynchronizer {private static final long serialVersionUID = -5179523762034025860L;abstract void lock();/*** Performs non-fair tryLock.  tryAcquire is implemented in* subclasses, but both need nonfair try for trylock method.*/final boolean nonfairTryAcquire(int acquires) {final Thread current = Thread.currentThread();int c = getState();if (c == 0) {//这个线程第一次加锁,使用CAS设置state=1if (compareAndSetState(0, acquires)) {//设置当前锁的当前持有线程,然后退出并返回成功setExclusiveOwnerThread(current);return true;}} else if (current == getExclusiveOwnerThread()) {//如果不是第一次加锁,则state向上加acquires(这里默认是1)int nextc = c + acquires;if (nextc < 0) // overflowthrow new Error("Maximum lock count exceeded"); //最大支持2147483647次重入setState(nextc);return true;}return false;}protected final boolean tryRelease(int releases) {int c = getState() - releases;if (Thread.currentThread() != getExclusiveOwnerThread())throw new IllegalMonitorStateException();boolean free = false;if (c == 0) {//只有释放锁的次数与加锁次数一致,才能完全释放锁free = true;setExclusiveOwnerThread(null);}setState(c);return free;}protected final boolean isHeldExclusively() {return getExclusiveOwnerThread() == Thread.currentThread();}final ConditionObject newCondition() {return new ConditionObject();}// Methods relayed from outer classfinal Thread getOwner() {return getState() == 0 ? null : getExclusiveOwnerThread();}final int getHoldCount() {return isHeldExclusively() ? getState() : 0;}final boolean isLocked() {return getState() != 0;}/*** Reconstitutes the instance from a stream (that is, deserializes it).* 反序列化,重置为未加锁状态*/private void readObject(java.io.ObjectInputStream s)throws java.io.IOException, ClassNotFoundException {s.defaultReadObject();setState(0); // reset to unlocked state}}

二、NonfairSync源码

    static final class NonfairSync extends Sync {private static final long serialVersionUID = 7316153563782823691L;/*** Performs lock.  Try immediate barge, backing up to normal* acquire on failure.*/final void lock() {//因为是非公平锁,只要锁没有被占有,那么直接加锁成功,不必调用acquire()方法去阻塞等待。if (compareAndSetState(0, 1))setExclusiveOwnerThread(Thread.currentThread());else//调用acquire()方法去阻塞等待acquire(1);}protected final boolean tryAcquire(int acquires) {return nonfairTryAcquire(acquires);}}

三、FairSync源码

static final class FairSync extends Sync {private static final long serialVersionUID = -3000897897090466540L;final void lock() {//直接调用acquire()方法去阻塞等待acquire(1);}/*** Fair version of tryAcquire.  Don't grant access unless* recursive call or no waiters or is first.*/protected final boolean tryAcquire(int acquires) {final Thread current = Thread.currentThread();int c = getState();if (c == 0) {//为保证公平锁,就算当前锁没有被持有,除非自己是第一个,否则不能加锁,因为是tryAcquire尝试加锁,所以不用阻塞,尝试失败就返回if (!hasQueuedPredecessors() &&compareAndSetState(0, acquires)) {setExclusiveOwnerThread(current);return true;}}else if (current == getExclusiveOwnerThread()) {int nextc = c + acquires;if (nextc < 0)throw new Error("Maximum lock count exceeded");setState(nextc);return true;}return false;}}

四、ReentrantLock源码实现

public class ReentrantLock implements Lock, java.io.Serializable {private static final long serialVersionUID = 7373984872572414699L;private final Sync sync;abstract static class Sync extends AbstractQueuedSynchronizer {// ... 省略 ...}static final class NonfairSync extends Sync {// ... 省略 ...}static final class FairSync extends Sync {// ... 省略 ...}/*** Creates an instance of {@code ReentrantLock}.* This is equivalent to using {@code ReentrantLock(false)}.*/public ReentrantLock() {sync = new NonfairSync();}/*** Creates an instance of {@code ReentrantLock} with the* given fairness policy.** @param fair {@code true} if this lock should use a fair ordering policy*/public ReentrantLock(boolean fair) {sync = fair ? new FairSync() : new NonfairSync();}public void lock() {sync.lock();}//如果当前线程不是中断状态则获得锁, 如果当前线程被中断则出现异常。如果是已经在阻塞队列,这个里面会有个循环,不断去获取锁,并判断当前线程是否中断状态,如果是中断状态,则抛出异常,并退出阻塞队列。这样可以在等待一段时间后,及时退出阻塞队列,去做别的事情,避免死锁。public void lockInterruptibly() throws InterruptedException {sync.acquireInterruptibly(1);}//tryLock()方法默认调用nonfairTryAcquire(),只要锁可用,即使当前有其他线程正在等待,它也会成功public boolean tryLock() {return sync.nonfairTryAcquire(1);}public boolean tryLock(long timeout, TimeUnit unit)throws InterruptedException {return sync.tryAcquireNanos(1, unit.toNanos(timeout));}public void unlock() {sync.release(1);}public Condition newCondition() {return sync.newCondition();}public int getHoldCount() {return sync.getHoldCount();}public boolean isHeldByCurrentThread() {return sync.isHeldExclusively();}public boolean isLocked() {return sync.isLocked();}public final boolean isFair() {return sync instanceof FairSync;}protected Thread getOwner() {return sync.getOwner();}public final boolean hasQueuedThreads() {return sync.hasQueuedThreads();}public final boolean hasQueuedThread(Thread thread) {return sync.isQueued(thread);}public final int getQueueLength() {return sync.getQueueLength();}protected Collection<Thread> getQueuedThreads() {return sync.getQueuedThreads();}public boolean hasWaiters(Condition condition) {if (condition == null)throw new NullPointerException();if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))throw new IllegalArgumentException("not owner");return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);}public int getWaitQueueLength(Condition condition) {if (condition == null)throw new NullPointerException();if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))throw new IllegalArgumentException("not owner");return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);}protected Collection<Thread> getWaitingThreads(Condition condition) {if (condition == null)throw new NullPointerException();if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))throw new IllegalArgumentException("not owner");return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);}public String toString() {Thread o = sync.getOwner();return super.toString() + ((o == null) ?"[Unlocked]" :"[Locked by thread " + o.getName() + "]");}
}

能看出来,无论公平锁还是非公平锁,lock()方法都是阻塞的,公平与否区别在于当state是0时,非公平锁此时调用lock()方法会直接加锁,公平锁会判断自己是不是阻塞队列中最靠前的一个,是则加锁,否则就进入阻塞队列等待。
阻塞可能会导致线程一直等待,造成资源的浪费或者线程死锁,此时我们可以通过trylock()方法,它调用获取锁失败后是直接返回,不会进入阻塞队列。或者通过lockInterruptibly()方法,可以随时中断当前线程的阻塞等待状态并退出阻塞队列。

五、Condition类详解

在Java中,Condition类是Java.util.concurrent包下的一个接口,用于支持线程的等待和通知机制。它通常与Lock接口一起使用,用于实现线程间的同步和协调。
Condition类提供了以下方法:

  1. await():使当前线程等待,直到被其他线程调用signal()或signalAll()方法唤醒。
  2. awaitUninterruptibly():类似于await()方法,但是在等待期间不会响应线程中断。
  3. await(long time, TimeUnit unit):使当前线程等待一段时间,在指定的时间内没有被其他线程调用signal()或signalAll()方法唤醒,将自动唤醒。
  4. awaitNanos(long nanosTimeout):使当前线程等待一段纳秒时间,在指定的时间内没有被其他线程调用signal()或signalAll()方法唤醒,将自动唤醒。
  5. awaitUntil(Date deadline):使当前线程等待直到某个时间,如果在指定时间内没有被其他线程调用signal()或signalAll()方法唤醒,将自动唤醒。
  6. signal():唤醒一个等待在Condition上的线程,并使其从await()方法返回。
  7. signalAll():唤醒所有等待在Condition上的线程,并使它们从await()方法返回。使用示例:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ConditionExample {private final Lock lock = new ReentrantLock();private final Condition condition = lock.newCondition();public void doSomething() {lock.lock();try {// 等待条件condition.await();// 执行其他操作} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}//由其它线程来调用这个方法,唤醒线程public void notifyThread() {lock.lock();try {// 唤醒线程condition.signal();} finally {lock.unlock();}}
}

这篇关于可重入锁ReentrantLock源码解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s