位置: IT常识 - 正文

Java多线程(6):锁与AQS(中)

编辑:rootadmin
您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ Java中的AQS(AbstractQueuedSynchronizer,抽象队列同步器)是用来实现锁及其他同步功能组件的Java底层技术基础,java.util.concurrent包下大部分类的实现都离不开它。 通过继承AQS: 1、Ree ...

推荐整理分享Java多线程(6):锁与AQS(中),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~

Java中的AQS(AbstractQueuedSynchronizer,抽象队列同步器)是用来实现锁及其他同步功能组件的Java底层技术基础,java.util.concurrent包下大部分类的实现都离不开它。

通过继承AQS:

1、ReentrantLock的内部类实现了公平锁和非公平锁;

2、CountDownLatch的内部类实现了发令枪;

3、ReentrantReadWriteLock的内部类实现了独享锁和共享锁;

4、Semaphore的内部类实现了公平锁和非公平锁。

AQS主要实现两大功能:独占(Exclusive,有时也叫排他)和共享(Share)。

AQS在内部维护一个FIFO(First In First Out,先进先出)的CLH(Craig,Landin,and Hagersten)线程阻塞队列和一个资源同步状态的变量volatile int state。

CLH是一个虚拟的双向队列,也就是不存在队列实例,仅存在节点之间的关联关系的队列。AQS是将每一条请求共享资源的线程,封装成一个CLH线程队列节点(Node),从而实现锁的分配。因此,说了一大堆,用一句简单的话来形容AQS就是:基于CLH线程阻塞队列,通过volatile变量 + CAS + 自旋方式来改变线程状态,成功则获取锁,失败则进入CLH队列。

Java多线程(6):锁与AQS(中)

AQS已经实现了CLH线程阻塞队列的维护,所以一般子类自定义实现AQS,要么是独占,要么是共享,也就是要么实现tryAcquire()和tryRelease()等系列方法,要么实现tryAcquireShared()和tryReleaseShared()等系列方法。

CLH队列由多个node节点组成,而且大量使用“CAS自旋volatile变量”这种经典代码:

CLH队列的结构为:

给CLH设置首节点:

给CLH设置尾节点:

整个AQS的流程如图:

AQS特别复杂,如果想把多线程搞透的,就需要深入研究每个方法的流程,拿acquire(int)方法的执行流程为例:

我把AQS的源码做了较为详细的注释,可以结合注释看看。例如:

/** * Provides a framework for implementing blocking locks and related * synchronizers (semaphores, events, etc) that rely on first-in-first-out * (FIFO) wait queues. 提供了一个实现阻塞锁和依赖FIFO的等待队列的相关的同步器(信号灯、事件等)框架 * * This class is designed to be a useful basis for most kinds of synchronizers * that rely on a single atomic {@code int} value to represent state. * 这个类对于大多数使用一个单独原子类的int值来表示状态的同步器很有用 * * Subclasses must define the protected methods that change this state, and * which define what that state means in terms of this object being acquired or * released. 子类必须定义protected方法来改变这个状态值,并且定义状态值是获取还是释放对象 * * Given these, the other methods in this class carry out all queuing and * blocking mechanics. 鉴于此,这个类中的其他方法实现了所有排队和阻塞的机制 * * Subclasses can maintain other state fields, but only the atomically updated * {@code int} value manipulated using methods {@link #getState}, * {@link #setState} and {@link #compareAndSetState} is tracked with respect to * synchronization. 子类可以维护其他的状态值字段,但只有getState、setState和compareAndSetState * 方法是通过原子更新来实现同步的 * * <p> * Subclasses should be defined as non-public internal helper classes that are * used to implement the synchronization properties of their enclosing class. * 子类应该定义成非public的内部helper工具类,用于实现其封闭类的同步属性 * * Class {@code AbstractQueuedSynchronizer} does not implement any * synchronization interface. AbstractQueuedSynchronizer类没有实现任何同步接口 * * Instead it defines methods such as {@link #acquireInterruptibly} that can be * invoked as appropriate by concrete locks and related synchronizers to * implement their public methods. * 取而代之的是,它定义了像acquireInterruptibly这样的方法,通过调用恰当的具体 锁和相关同步器方法,以便实现他们自己的公共方法 * * <p> * This class supports either or both a default <em>exclusive</em> mode and a * <em>shared</em> mode. 这个类既支持默认的独占模式,也支持共享模式,也支持两种模式一起实现 * * When acquired in exclusive mode, attempted acquires by other threads cannot * succeed. 当在独占模式获取到锁时,其他线程再尝试获取锁会失败 * * Shared mode acquires by multiple threads may (but need not) succeed. * 共享模式,多个线程都能成功获取到锁 * * This class does not understand these differences except in the mechanical * sense that when a shared mode acquire succeeds, the next waiting thread (if * one exists) must also determine whether it can acquire as well. * 这个类不会理解机制的不同,共享模式中的一个线程获取锁成功了,下一个线程 (如果存在)仍然会去确定它自己是否也可以获取 * * Threads waiting in the different modes share the same FIFO queue. * 线程虽在不同的模式中,却都在等待共享相同的FIFO队列 * * Usually, implementation subclasses support only one of these modes, but both * can come into play for example in a {@link ReadWriteLock}. * 通常,子类只需要实现这两种模式中的一种,但也能两种都实现,例如ReadWriteLock * * Subclasses that support only exclusive or only shared modes need not define * the methods supporting the unused mode. 仅支持一种模式的子类,不必定义另一种模式下的方法 * * <p> * This class defines a nested {@link ConditionObject} class that can be used as * a {@link Condition} implementation by subclasses supporting exclusive mode * for which method {@link#isHeldExclusively} reports whether synchronization is * exclusively held with respect to the current thread, method {@link #release} * invoked with the current {@link #getState} value fully releases this object, * and {@link #acquire}, given this saved state value, eventually restores this * object to its previous acquired state. * 这个类定义了一个嵌套的ConditionObject类,该类可以被支持独占模式的子类用作 * Condition实现,为此,isHeldExclusively()报告当前线程是否持续保持同步, * release方法通过调用getState来完全释放当前对象,并且将当前的资源状态 再保存到state中,最后会将此对象恢复为先前的获取状态 * * No {@code AbstractQueuedSynchronizer} method otherwise creates such a * condition, so if this constraint cannot be met, do not use it. * 没有AbstractQueuedSynchronizer方法去创建condition,因此如果不能满足 这个约束,就不要使用它 * * The behavior of {@link ConditionObject} depends of course on the semantics of * its synchronizer implementation. ConditionObject的行为依赖于其同步器实现的语义 * * <p> * This class provides inspection, instrumentation, and monitoring methods for * the internal queue, as well as similar methods for condition objects. * 这个类提供检查、追踪和监控内部队列的方法,类似于condition对象的方法 * * These can be exported as desired into classes using an * {@code AbstractQueuedSynchronizer} for their synchronization mechanics. * 可以根据需要使用AbstractQueuedSynchronizer,将它们导入到类中以实现其同步机制 * * <p> * Serialization of this class stores only the underlying atomic integer * maintaining state, so deserialized objects have empty thread queues. * 这个类仅序列化state的原子值,因此反序列化出来的对象中的线程队列是空的 * * Typical subclasses requiring serializability will define a {@code readObject} * method that restores this to a known initial state upon deserialization. * 需要序列化的子类可以在反序列化的时候定义一个readObject方法来恢复已知的初始状态 * * * <h3>Usage</h3> 使用 * * <p> * To use this class as the basis of a synchronizer, redefine the following * methods, as applicable, by inspecting and/or modifying the synchronization * state using {@link #getState}, {@link #setState} and/or * {@link #compareAndSetState}: 使用这个类作为同步器锁,需要重新定义以下方法: * * <ul> * <li>{@link #tryAcquire} * <li>{@link #tryRelease} * <li>{@link #tryAcquireShared} * <li>{@link #tryReleaseShared} * <li>{@link #isHeldExclusively} * </ul> * * Each of these methods by default throws * {@link UnsupportedOperationException}. * 这些方法默认抛出UnsupportedOperationException异常 * * Implementations of these methods must be internally thread-safe, and should * in general be short and not block. 这些方法的实现必须在内部是线程安全的,而且通常都很简短,没有阻塞 * * Defining these methods is the <em>only</em> supported means of using this * class. 定义这些方法是使用这个类唯一可行的方式 * * All other methods are declared {@code final} because they cannot be * independently varied. 所有其他的方法都被
本文链接地址:https://www.jiuchutong.com/zhishi/304134.html 转载请保留说明!

上一篇:phpcms如何更换模板(phpcms怎么用)

下一篇:帝国cms怎么替换网址域名(帝国cms怎么用)

  • 小米watchs1pro怎么开启睡眠监测(小米watch s pro)

    小米watchs1pro怎么开启睡眠监测(小米watch s pro)

  • 微信可以设置特别关心吗(微信可以设置特别关心提示音吗)

    微信可以设置特别关心吗(微信可以设置特别关心提示音吗)

  • 打印机连接线(惠普打印机连接线)

    打印机连接线(惠普打印机连接线)

  • 无墨打印机是什么原理(无墨打印机什么牌子好)

    无墨打印机是什么原理(无墨打印机什么牌子好)

  • 三星激光打印机怎么加墨粉(三星激光打印机怎么样)

    三星激光打印机怎么加墨粉(三星激光打印机怎么样)

  • 小米手机街拍模式怎么关闭声音(小米手机街拍模式录像)

    小米手机街拍模式怎么关闭声音(小米手机街拍模式录像)

  • 隔空投送总是自动拒绝(隔空投送为什么会自动开启)

    隔空投送总是自动拒绝(隔空投送为什么会自动开启)

  • 苹果7怎么关闭系统更新(苹果7怎么关闭消息来的时候闪光灯)

    苹果7怎么关闭系统更新(苹果7怎么关闭消息来的时候闪光灯)

  • 退出群聊群主有提醒吗(退出群聊群主有消息吗)

    退出群聊群主有提醒吗(退出群聊群主有消息吗)

  • 电话手表没有网络怎么设置(手表电话怎么连接手机)

    电话手表没有网络怎么设置(手表电话怎么连接手机)

  • 微信语音发的慢怎么回事(微信语音发送缓慢)

    微信语音发的慢怎么回事(微信语音发送缓慢)

  • ecr50clp压缩机多少瓦(ecq50clc压缩机参数)

    ecr50clp压缩机多少瓦(ecq50clc压缩机参数)

  • 键盘是计算机的什么设备(鼠标和键盘是计算机的)

    键盘是计算机的什么设备(鼠标和键盘是计算机的)

  • word文本无法编辑(word文本编辑不了)

    word文本无法编辑(word文本编辑不了)

  • 单模光纤中间接一段多模可以吗(单模光纤断了怎么接)

    单模光纤中间接一段多模可以吗(单模光纤断了怎么接)

  • 微信加好友能不能隐藏来源(微信加好友能不能看到来源)

    微信加好友能不能隐藏来源(微信加好友能不能看到来源)

  • 小米9pro是什么屏(小米9pro是什么屏幕)

    小米9pro是什么屏(小米9pro是什么屏幕)

  • 滴滴快车有人数限制吗(滴滴快车app)

    滴滴快车有人数限制吗(滴滴快车app)

  • 微信怎么关闭手机号码显示(微信怎么关闭手机联系人添加好友)

    微信怎么关闭手机号码显示(微信怎么关闭手机联系人添加好友)

  • 钉钉怎么同时加入两个公司(钉钉如何加两个人)

    钉钉怎么同时加入两个公司(钉钉如何加两个人)

  • 高校校园app开发怎么做(高校校园app开发)

    高校校园app开发怎么做(高校校园app开发)

  • Linux怎么配置双机SSH互相信任实现免密码登录?(linux双ip路由设置方法)

    Linux怎么配置双机SSH互相信任实现免密码登录?(linux双ip路由设置方法)

  • 基于Pytorch的MNIST手写数字识别实现(含代码+讲解)(基于Pytorch的风格转换)

    基于Pytorch的MNIST手写数字识别实现(含代码+讲解)(基于Pytorch的风格转换)

  • 土地增值税间接费用扣除
  • 电汇汇票和电汇的异同点
  • 如何确定固定资产的计税基础
  • 运输部门计入什么会计科目
  • 餐费可以抵扣吗
  • 事业单位合并财务交接
  • 进销存明细账怎么填写
  • 增值税专用发票几个点
  • 白酒在哪个环节征收消费税
  • 一般纳税人从小规模纳税人处进货
  • 固定资产认证进项在勾选平台勾吗
  • 企业支付小额劳务费税率
  • 农牧业没有发票怎么办
  • 技术开发合同可以开专票吗
  • 已验旧和未验旧
  • 稳岗补贴需要缴税吗
  • 结转实物发放的成本分录
  • 土地出让金如何缴纳
  • 民间非营利组织会计制度最新版
  • 自然人股息红利免征个人所得税?
  • 应发工资包含
  • 商品促销有关问题
  • 留抵税额如何抵扣
  • 任务管理器网络占用率100%怎么办
  • 鸿蒙系统如何删除桌面图标
  • 内存条松动导致黑屏
  • windows10如何关闭病毒和威胁防护设置
  • 应收票据借贷方核算什么内容
  • 年初未分配利润计算公式
  • macbookpro怎么打开活动监视器
  • php字符串变量
  • 用java做项目
  • 长期借款的业务处理
  • 销售材料计提存入什么
  • 房产证和不动产权证都要办理吗
  • php proc_open
  • 季节性用工政策
  • 财务人员如何管控费用支出
  • php常见错误
  • html表白代码动态
  • faster rcnn详解
  • sar参数设置4个数
  • 酒店的销售费用和管理费用如何进行区分
  • 账户验证是什么意思
  • 未形成无形资产计入当期损益的
  • 丧葬补贴金和抚恤金怎样领取
  • 工会经费税前扣除是什么意思
  • 其他应收账款为个人借款
  • 哪些税费需要计提
  • 销售黄金的会计分录
  • 企业财务部门对业务部门的监管要求
  • 人工成本与工资的关系
  • 企业每年需要做什么
  • 自来水厂的供水井
  • 企业所得税汇算清缴操作流程
  • 私车公用协议可以过户吗
  • 开农贸市场拿补贴找哪个部门
  • 对方开票少开几角会计分录
  • 行政单位收到的待报解预算收入怎么做账
  • 申报系统中印花税报表怎么填
  • 软件测试费用明细
  • 物流公司挂靠车辆如何做账?
  • 工程款打入个人账户扣税吗
  • 主营业务成本包括工资吗
  • 会计报表的主要构成内容
  • 会计从业资格证取消了吗
  • myeclipse中连接数据库的地方在哪
  • ahqtb.exe是啥进程 ahqtb进程信息查询
  • c盘里面放什么
  • WIN10系统英文怎么改中文?
  • 移动端网页开发技术
  • [android] intent实例
  • 用javascript
  • 解决在基层
  • javascript entries
  • jq复选框选中触发事件
  • 数据类型 python
  • 丹阳税务局一分局领导
  • 百旺开票软件打不开怎么回事
  • 长沙市税收排名
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设