当前位置:网站首页>Synchronized lock upgrade
Synchronized lock upgrade
2022-07-19 11:17:00 【TaoYuanming of the Western Wei Dynasty】

author : TaoYuanming of the Western Wei Dynasty
Blog : https://blog.springlearn.cn/
TaoYuanming of the Western Wei Dynasty
Don't laugh at the young Jianghu dream , Who doesn't dream of Jianghu
One 、 Heavyweight lock
What is heavyweight lock ?
That is, applying for resources must go through kernel( The kernel is also called the operating system ), call .

Two 、 Lightweight lock
Lightweight lock , Is not through the operating system . Lightweight locks are called relative to heavyweight locks , It can also be called Le Guan lock .
stay Java The optimistic lock is cas operation (compare and swap) According to English translation, it means comparison and Exchange .
The bottom layer is called Unsafe The method inside , You can see that these methods are native Method .

By looking at jvm Source code , notice c++ The code has an assembly language support cas

however , This article cmpchg1 It's not atomic , Click in lock_if_mp(%4) in
The ultimate realization is lock cmpxchg Instructions : It means that the hardware will lock a North Bridge bus when executing the following instructions .( Equivalent to locking the bus , But it is lighter than the lock bus ) The following problems have been solved
3、 ... and 、 Biased locking
seeing the name of a thing one thinks of its function , It will favor the first thread to access the lock , If in operation , The synchronization lock has only one thread access , There is no multi-threaded contention , The thread does not need to trigger synchronization , In this case , Will add a biased lock to the thread .
To put it bluntly , There is no competition , What lock is it called . Is to add a mark . Think no one is competing for you .
Four 、 Lock upgrade steps
Biased locking -> Lightweight lock -> Heavyweight lock
stay jdk1.6 Before synchronized Direct is a heavyweight lock , One finished, all is finished .
jdk After optimization , The concept of lock escalation .
So in fact synchronized Implementation process of :
- testing Mark Word Is it the current thread ID, If it is , Indicates that the current thread is in a biased lock
- If not , Then use CAS Will be the current thread of ID Replace Mard Word, If successful, it means that the current thread gets biased lock , Set the bias flag 1( When the previous thread is just released , This can succeed , Otherwise, look at 3)
- If you fail , It means that there is competition , Undo the bias lock , Then upgrade to lightweight lock .
- The current thread uses CAS Put the Mark Word Replace with lock record pointer , If it works , Current thread gets lock
- If you fail , Indicates that other threads are competing for locks , The current thread attempts to acquire a lock using spin .
- If the spin succeeds, it's still in a lightweight state .
- If spin fails , Upgrade to heavyweight lock .
Translated into vernacular :
When no one competes with you, it is a biased lock , When cas failed , It means someone is competing with you , At this time, the lock is upgraded from biased lock to lightweight lock .
In the state of lightweight lock , There are still many threads to compete , So at this time cas It will be more serious and waste cpu perform . Upgrade to a heavyweight lock .
Secondly, other waiting threads enter the blocking state .
Finally, please pay attention , Request subscription , Thanks for reading !
边栏推荐
- Game theory (Depu) and investment (40/100)
- 2022/7/14
- 2022/7/16
- web安全入门-部署Snort开源IDS/IPS系统
- LeetCode 2325. Decrypt message (map)
- Win10安装Apache Jena 3.17
- Nombre d'entrées nombre d'entrées numériques pures limite de longueur maximale
- What should I do if I can't see any tiles on SAP Fiori launchpad?
- Cmake常用命令(五)
- Conversion of unity3d model center point (source code)
猜你喜欢

Documents required for military product development process - advanced version

Evaluation method of machine learning model

Pytoch and weight decay (L2 norm)

Scala's dosing in idea

Detailed explanation of multiple linear regression

Mysql优化系列之limit查询

synchronized锁升级

论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries

Win10 install Apache Jena 3.17

LeetCode 2325. Decrypt message (map)
随机推荐
466-82(3、146、215)
Over fitting and under fitting
Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
A fastandrobust volutionalneuralnetwork based defect detection model inproductqualitycontrol reading notes
pjudge#21652-[PR #4]到底有没有九【数位dp】
LeetCode 2331. Calculate the value of Boolean binary tree (tree traversal)
Unity3d 读取mpu9250 例子原代码
常用getshell工具的下载
2022/7/14
Conversion of unity3d model center point (source code)
A simple output method of promise object to the result in nodejs (it is recommended to use the asynchronous ultimate scheme async+await)
委派双亲之类加载器
LOJ 2324 - "Tsinghua training 2017" small y and binary tree
2022/7/15
Model comparison of material inventory management between sap ECC and s4hana material
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (2)
LeetCode 2319. Judge whether the matrix is an X matrix
(2) Using MySQL
微服务上线规范
OA系统与MES系统的异同点