JUC-ReentrantLock 锁粒度为什么更小

1. ReentrantLock 可中断

  • ReentrantLock 可以选择在等待锁的时候中断退出,而不必一直等待锁释放

    lock.lockInterruptibly();
    
  • synchronized 中的线程一旦进入阻塞状态,除非锁被释放,否则无法中断退出

2. ReentrantLock 可以设置获取锁的超时时间

在一定时间内如果没有获取锁,就会放弃锁的竞争,而不必一直等待。可以有效避免死锁

    /*** Acquires the lock if it is not held by another thread within the given* waiting time and the current thread has not been* {@linkplain Thread#interrupt interrupted}.** <p>Acquires the lock if it is not held by another thread and returns* immediately with the value {@code true}, setting the lock hold count* to one. If this lock has been set to use a fair ordering policy then* an available lock <em>will not</em> be acquired if any other threads* are waiting for the lock. This is in contrast to the {@link #tryLock()}* method. If you want a timed {@code tryLock} that does permit barging on* a fair lock then combine the timed and un-timed forms together:**  <pre> {@code* if (lock.tryLock() ||*     lock.tryLock(timeout, unit)) {*   ...* }}</pre>** <p>If the current thread* already holds this lock then the hold count is incremented by one and* the method returns {@code true}.** <p>If the lock is held by another thread then the* current thread becomes disabled for thread scheduling* purposes and lies dormant until one of three things happens:** <ul>** <li>The lock is acquired by the current thread; or** <li>Some other thread {@linkplain Thread#interrupt interrupts}* the current thread; or** <li>The specified waiting time elapses** </ul>** <p>If the lock is acquired then the value {@code true} is returned and* the lock hold count is set to one.** <p>If the current thread:** <ul>** <li>has its interrupted status set on entry to this method; or** <li>is {@linkplain Thread#interrupt interrupted} while* acquiring the lock,** </ul>* then {@link InterruptedException} is thrown and the current thread's* interrupted status is cleared.** <p>If the specified waiting time elapses then the value {@code false}* is returned.  If the time is less than or equal to zero, the method* will not wait at all.** <p>In this implementation, as this method is an explicit* interruption point, preference is given to responding to the* interrupt over normal or reentrant acquisition of the lock, and* over reporting the elapse of the waiting time.** @param timeout the time to wait for the lock* @param unit the time unit of the timeout argument* @return {@code true} if the lock was free and was acquired by the*         current thread, or the lock was already held by the current*         thread; and {@code false} if the waiting time elapsed before*         the lock could be acquired* @throws InterruptedException if the current thread is interrupted* @throws NullPointerException if the time unit is null*/public boolean tryLock(long timeout, TimeUnit unit)throws InterruptedException {return sync.tryAcquireNanos(1, unit.toNanos(timeout));}

3. ReentrantLock 可以设置公平锁

ReentrantLock 可以通过构造方法传参,设置为公平锁,就会按照线程的请求顺序获得锁,先到先得

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

4. ReentrantLock 可以尝试获得锁

可以在不阻塞的情况下获得锁,通过 tryLock() 实现

/*** Acquires the lock only if it is not held by another thread at the time* of invocation.** <p>Acquires the lock if it is not held by another thread and* returns immediately with the value {@code true}, setting the* lock hold count to one. Even when this lock has been set to use a* fair ordering policy, a call to {@code tryLock()} <em>will</em>* immediately acquire the lock if it is available, whether or not* other threads are currently waiting for the lock.* This &quot;barging&quot; behavior can be useful in certain* circumstances, even though it breaks fairness. If you want to honor* the fairness setting for this lock, then use* {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }* which is almost equivalent (it also detects interruption).** <p>If the current thread already holds this lock then the hold* count is incremented by one and the method returns {@code true}.** <p>If the lock is held by another thread then this method will return* immediately with the value {@code false}.** @return {@code true} if the lock was free and was acquired by the*         current thread, or the lock was already held by the current*         thread; and {@code false} otherwise*/public boolean tryLock() {return sync.nonfairTryAcquire(1);}

5. 可以设置条件变量

  • ReentrantLock 允许通过 Condition 对象来实现更精细的线程通信和等待唤醒机制。一个 ReentrantLock 可以创建多个Condition 对象,以实现更复杂的线程间同步。
  • 与 synchronized 的 wait() 和 notify() 相比,Condition 更灵活,可以指定多个条件队列,避免了 wait 和 notifyAll 带来的无差别唤醒问题。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1556998.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Spring相关知识补充

目录 一、将Bean存储到spring&#xff08;容器&#xff09;中 1、使用spring-config的方式将对象存储到spring容器中 2、使用类注解的方式将Bean对象存储到容器中 1️⃣、配置扫描路径&#xff08;使用注解的方式存对象的前提&#xff09; 2️⃣、使用五大类注解存储Bean对…

C语言练习

接下来一段时间&#xff0c;博主要参加军训没有时间更新C语言知识点&#xff0c;但博主会每天更新一道C语言的题作为分享。 1.计算并显示整数的差 分析&#xff1a;1.题目并不难&#xff0c;首先我们要知道printf这个库函数&#xff0c;是用来打印数据到屏幕的库函数 2.设置变…

【AI知识点】反向传播(Backpropagation)

反向传播&#xff08;Backpropagation&#xff09; 是训练神经网络的核心算法&#xff0c;它通过反向逐层计算损失函数对每个权重的梯度&#xff0c;来反向逐层更新网络的权重&#xff0c;从而最小化损失函数。 一、反向传播的基本概念 1. 前向传播&#xff08;Forward Propag…

文件丢失一键找回,四大数据恢复免费版工具推荐!

丢失数据的情况虽然不经常出现&#xff0c;但一旦出现都会让人头疼不已&#xff0c;而这时候&#xff0c;要如何恢复丢失的数据呢&#xff1f;一款免费好用的数据恢复工具就派上用场了&#xff01;接下来就为大家推荐几款好用的数据恢复工具&#xff01; 福昕数据恢复 直达链…

Redis list 类型

list类型 类型介绍 列表类型 list 相当于 数组或者顺序表 list内部的编码方式更接近于 双端队列 &#xff0c;支持头插 头删 尾插 尾删。 需要注意的是&#xff0c;Redis的下标支持负数下标。 比如数组大小为5&#xff0c;那么要访问下标为 -2 的值可以理解为访问 5 - 2 3 …

【韩顺平Java笔记】第8章:面向对象编程(中级部分)【272-284】

272. 包基本介绍 272.1 看一个应用场景 272.2 包的三大作用 272.3 包的基本语法 273. 包原理 274. 包快速入门 在不同的包下面创建不同的Dog类 275. 包命名 276. 常用的包 一个包下,包含很多的类,java 中常用的包有: java.lang.* //lang 包是基本包&#xff0c;默认引入&…

农业政策与市场分析:解读当前政策导向下的农业发展趋势

在快速变化的全球经济格局中&#xff0c;农业作为国家稳定发展的基石&#xff0c;其政策走向与市场动态备受瞩目。本文将深入剖析当前的农业政策背景&#xff0c;探讨其对设计的导向作用&#xff0c;以及市场趋势的反馈与影响&#xff0c;为农业可持续发展提供洞见。 1. 政策背…

【大模型理论篇】大模型相关的周边技术分享-关于《NN and DL》的笔记

本文所要介绍的一本书《Neural Networks and Deep Learning》&#xff0c;该书作者Michael Nielsen&#xff0c;Y Combinator Research的研究员&#xff0c;是多年之前自己看的一本基础书籍&#xff0c;很适合入门了解一些关于深度学习的概念知识&#xff0c;当然也包含了一些小…

MyBatis 批量插入方案

MyBatis 批量插入 MyBatis 插入数据的方法有几种&#xff1a; for 循环&#xff0c;每次都重新连接一次数据库&#xff0c;每次只插入一条数据。 在编写 sql 时用 for each 标签&#xff0c;建立一次数据库连接。 使用 MyBatis 的 batchInsert 方法。 下面是方法 1 和 2 的…

三相逆变器中LCL滤波器分析

1.LCL滤波器 传统三相逆变器使用的是L型滤波器&#xff0c;其设计简单&#xff0c;但也存在着一些问题&#xff0c;如在同样的滤波效果下&#xff0c;L型滤波器电感尺寸、重量较大&#xff0c;成本较高&#xff0c;并且随着电感值的增大&#xff0c;其上的电压降增加比较明显&…

【MySQL必知会】事务

目录 &#x1f308;前言&#x1f308; &#x1f4c1; 事务概念 &#x1f4c1; 事务操作 &#x1f4c1; 事务提交方式 &#x1f4c1; 隔离级别 &#x1f4c1; MVCC &#x1f4c2; 3个隐藏列字段 &#x1f4c2; undo日志 &#x1f4c2; Read View视图 &#x1f4c1; RR和R…

【GESP】C++一级练习BCQM3028,输入-计算-浮点型格式化输出

目前的几道题主要围绕浮点型的计算和格式化输出。强化基础语法练习。 详解详见&#xff1a;https://www.coderli.com/gesp-1-bcqm3028/ 【GESP】C一级练习BCQM3028&#xff0c;输入-计算-浮点型格式化输出 | OneCoder目前的几道题主要围绕浮点型的计算和格式化输出。强化基础语…

说说BPMN概念及应用

BPMN&#xff08;Business Process Modeling and Notation&#xff09;即业务流程建模与标注&#xff0c;是一种由OMG&#xff08;Object Management Group&#xff0c;对象管理组织&#xff09;制定的业务流程建模语言。以下是对BPMN标准的详细解释&#xff1a; 一、BPMN的起…

短剧系统源码短剧平台开发(H5+抖小+微小)部署介绍流程

有想法加入国内短剧赛道的请停下脚步&#xff0c;耐心看完此篇文章&#xff0c;相信一定会对您有所帮助的&#xff0c;下面将排序划分每一个步骤&#xff0c;短剧源码、申请资料、服务器选择、部署上架到正常运行等几个方面&#xff0c;整理了一些资料&#xff0c;来为大家举例…

Spring Boot助力医院数据管理

2相关技术 2.1 MYSQL数据库 MySQL是一个真正的多用户、多线程SQL数据库服务器。 是基于SQL的客户/服务器模式的关系数据库管理系统&#xff0c;它的有点有有功能强大、使用简单、管理方便、安全可靠性高、运行速度快、多线程、跨平台性、完全网络化、稳定性等&#xff0c;非常适…

MySQL进阶学习一(2024.10.07版)

2024-10-06 -------------------------------------------------------------------------------------------------------------------------------- 1.一条SQL语句是如何执行的 单进程的多线程模型 MySQL的物理目录 show global variables like "%basedir%"; …

LSTM时序预测 | Python实现LSTM长短期记忆神经网络时间序列预测

本文内容&#xff1a;Python实现LSTM长短期记忆神经网络时间序列预测&#xff0c;使用的数据集为AirPassengers 目录 数据集简介 1.步骤一 2.步骤二 3.步骤三 4.步骤四 数据集简介 AirPassengers 数据集的来源可以追溯到经典的统计和时间序列分析文献。原始数据集由 Box,…

面向对象特性中 继承详解

目录 概念&#xff1a; 定义&#xff1a; 定义格式 继承关系和访问限定符 基类和派生类对象赋值转换&#xff1a; 继承中的作用域&#xff1a; 派生类的默认成员函数 继承与友元&#xff1a; 继承与静态成员&#xff1a; 复杂的菱形继承及菱形虚拟继承&#xff1a; 虚…

VGG16模型实现MNIST图像分类

MNIST图像数据集 MNIST&#xff08;Modified National Institute of Standards and Technology&#xff09;是一个经典的机器学习数据集&#xff0c;常用于训练和测试图像处理和机器学习算法&#xff0c;特别是在数字识别领域。该数据集包含了大约 7 万张手写数字图片&#xf…

喜讯 | 攸信技术入选第六批专精特新“小巨人”企业

日前&#xff0c;根据工信部评审结果&#xff0c;厦门市工业和信息化局公示了第六批专精特新“小巨人”企业和第三批专精特新“小巨人”复核通过企业名单&#xff0c;其中&#xff0c;厦门攸信信息技术有限公司进入第六批专精特新“小巨人”企业培育。 “专精特新”企业是指具有…