当前位置:网站首页>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入门
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
MySQL---多表查询
统计UTF-8字符串中的字符函数
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
Apache EventMesh 分布式事件驱动多运行时
请问我的这段sql中sql语法哪里出了错
STM32 full series development firmware installation guide under Arduino framework
idea中搜索具体的字符内容的快捷方式
随机推荐
学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
linux查看redis版本命令(linux查看mysql版本号)
leetcode 665. Non-decreasing Array
请问我的这段sql中sql语法哪里出了错
基于STM32 环形队列来实现串口接收数据
架构实战营模块 8 作业
Made with Flutter and Firebase!counter application
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
MySQL---单行函数
35道MySQL面试必问题图解,这样也太好理解了吧
How programmers learn open source projects, this article tells you
10 Ways to Keep Your Interface Data Safe
OSPFv3的基本配置
手把手教你学会部署Nestjs项目
BOW/DOM(上)
INeuOS industrial Internet operating system, the equipment operational business and "low code" form development tools
Apache EventMesh distributed event-driven multi-runtime
统计UTF-8字符串中的字符函数
MySQL---aggregate function
【AcWing】第 62 场周赛 【2022.07.30】