当前位置:网站首页>The principle of ReentrantLock (to be continued)
The principle of ReentrantLock (to be continued)
2022-07-31 19:29:00 【swofford】
ReentrantLock
1. Overview
The bottom layer of ReentrantLock is based on AQS, and its construction method returns NonfaireSync and faireSync;
Both synchronizers inherit from Sync, and Sync inherits from AQS!
When new ReentrantLock is used, the synchronizer is returned, by defaultis unfair;
2. Unfair lock locking
2. 1 successfully locked
Call the lock() method and use the CAS mechanism to try to change the state attribute modified by the underlying volatile of AQS to 1. If it succeeds, it will be locked;
If it failsIf there is a competition, enter the acquire() method;
2. 2 lock failed
- When the lock is already occupied, the state attribute in the shared memory is 1, compareAndSet(0,1) will fail, and the CAS will fail to change the state attribute, and the acquire() method will be entered;
- Enter the acquire() method of AQS in else;
- Call tryAcquire() and try again, the result is false, the negation is true, and then execute acquireQueued(), it will create a NodeThe node object is associated with the thread and placed in the FIFO waiting queue (doubly linked list);
- After the node enters the queue, it will try to lock in a loop. If it fails, it will be blocked by park. The node's waitStatus will be set to -1, and the node will be woken up by the pre-order node later;
The acquire() method in AQS:
Suppose multiple threads fail to compete and enter the FIFO waiting queue:
2. Unfair lock release
2.1 Competitive Success
- Call the unlock() method, the bottom layer calls the release() method, use tryRelease() to state>Set to 0 to release the lock; After
- tryRelease() returns true, judge whether the head sentinel node is null, if not and the waitStatus of the sentinel node will not be 0(-1), the successor node of the sentinel node is awakened by unParkSuccessor!Then this successor node will be removed from the FIFO queue;
2.1 competition failed
边栏推荐
- Three.js入门
- mysql的备份表的几种方法
- BOW/DOM(上)
- ResNet的基础:残差块的原理
- 1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
- cas与自旋锁(轻量级锁就是自旋锁吗)
- Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
- The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
- 移动web开发02
- Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
猜你喜欢
使用 Flutter 和 Firebase 制作!计数器应用程序
ThreadLocal
统计UTF-8字符串中的字符函数
MySQL---多表查询
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
MySQL---运算符
35 MySQL interview questions and diagrams, this is also easy to understand
2022年Android 面经总结(附含面试题 | 源码 | 面试资料)
全平台GPU通用AI视频补帧超分教程
Chinese encoding Settings and action methods return values
随机推荐
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
财务盈利、偿债能力指标
常用的安全渗透测试工具(渗透测试工具)
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
MySQL - multi-table query
MySQL---operator
给定一个ip地址,子网掩码怎么算网络号(如何获取ip地址和子网掩码)
Basic configuration of OSPFv3
如何才能真正的提高自己,成为一名出色的架构师?
深度学习中的batch(batch size,full batch,mini batch, online learning)、iterations与epoch
Basics of ResNet: Principles of Residual Blocks
Go record - slice
京东获取商品历史价格信息 API
PCB stackup design
MATLAB程序设计与应用 2.4 MATLAB常用内部函数
有一说一,外包公司到底值不值得去?
Chinese encoding Settings and action methods return values
cas与自旋锁(轻量级锁就是自旋锁吗)
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
手把手教你学会部署Nestjs项目