当前位置:网站首页>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
边栏推荐
- Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
- spark报错OutOfMemory「建议收藏」
- 常用的安全渗透测试工具(渗透测试工具)
- 【码蹄集新手村600题】通向公式与程序相结合
- Three.js入门
- 10 Ways to Keep Your Interface Data Safe
- MySQL---多表查询
- MySQL - single function
- c语言解析json字符串(json对象转化为字符串)
- matplotlib ax bar color Set the color, transparency, label legend of the ax bar
猜你喜欢

ResNet的基础:残差块的原理

基于WPF重复造轮子,写一款数据库文档管理工具(一)

嵌入式开发没有激情了,正常吗?

MATLAB程序设计与应用 2.4 MATLAB常用内部函数

使用 Flutter 和 Firebase 制作!计数器应用程序

How programmers learn open source projects, this article tells you

35道MySQL面试必问题图解,这样也太好理解了吧

GAC Honda Safety Experience Camp: "Danger" is the best teacher

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

ReentrantLock原理(未完待续)
随机推荐
京东获取商品历史价格信息 API
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
关注!海泰方圆加入《个人信息保护自律公约》
STM32 full series development firmware installation guide under Arduino framework
Apache EventMesh distributed event-driven multi-runtime
【码蹄集新手村600题】不通过字符数组来合并俩个数字
SiC MOSFET的短路特性及保护
leetcode 665. Non-decreasing Array
多线程之锁
Linux环境redis集群搭建「建议收藏」
grep命令 笔试题
MySQL---创建和管理数据库和数据表
npm 更改为淘宝镜像的方法[通俗易懂]
Thymeleaf是什么?该如何使用。
C# 之 扑克游戏 -- 21点规则介绍和代码实现
Performance optimization: remember a tree search interface optimization idea
中文编码的设置与action方法的返回值
统计UTF-8字符串中的字符函数
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
MySQL---排序与分页