当前位置:网站首页>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
边栏推荐
- MySQL---Create and manage databases and data tables
- This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!
- 【码蹄集新手村600题】通向公式与程序相结合
- Go record - slice
- Cache and Database Consistency Solutions
- MySQL - multi-table query
- Architect 04 - Application Service Encryption Design and Practice
- OSPFv3的基本配置
- MySQL---创建和管理数据库和数据表
- pytorch lstm时间序列预测问题踩坑「建议收藏」
猜你喜欢
Cache and Database Consistency Solutions

手把手教你学会部署Nestjs项目

学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8

Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds

2022年Android 面经总结(附含面试题 | 源码 | 面试资料)

第七章

【码蹄集新手村600题】不通过字符数组来合并俩个数字

What's wrong with the sql syntax in my sql

Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules

Apache EventMesh 分布式事件驱动多运行时
随机推荐
AI 自动写代码插件 Copilot(副驾驶员)
京东获取商品历史价格信息 API
Go basic part study notes
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
手把手教你学会部署Nestjs项目
Getting Started with Tkinter
【码蹄集新手村600题】通向公式与程序相结合
常用的安全渗透测试工具(渗透测试工具)
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
Short-circuit characteristics and protection of SiC MOSFETs
浅谈网络安全之算法安全
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
How can we improve the real yourself, become an excellent architect?
【AcWing】The 62nd Weekly Match 【2022.07.30】
Made with Flutter and Firebase!counter application
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
返回一个零长度的数组或者空的集合,不要返回null
leetcode 665. Non-decreasing Array
中文编码的设置与action方法的返回值
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具