当前位置:网站首页>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
边栏推荐
猜你喜欢

Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure

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

MySQL---operator

How can we improve the real yourself, become an excellent architect?

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

2022 Android interview summary (with interview questions | source code | interview materials)

程序员如何学习开源项目,这篇文章告诉你
![leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]](/img/91/284de3dcbb8d143d85775b314dd41c.png)
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]

MySQL - single function

Tkinter 入门之旅
随机推荐
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
使用 Flutter 和 Firebase 制作!计数器应用程序
MySQL---多表查询
What's wrong with the sql syntax in my sql
Teach you how to deploy Nestjs projects
How can we improve the real yourself, become an excellent architect?
【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
35道MySQL面试必问题图解,这样也太好理解了吧
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
架构师04-应用服务间加密设计和实践
idea中搜索具体的字符内容的快捷方式
学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
Introduction to Audio Types and Encoding Formats in Unity
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
1161. 最大层内元素和 : 层序遍历运用题
Arduino框架下STM32全系列开发固件安装指南
Three. Introduction to js
leetcode 665. Non-decreasing Array 非递减数列(中等)
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula