当前位置:网站首页>Syncronized lock upgrade process
Syncronized lock upgrade process
2022-07-25 11:17:00 【Fried tomatoes with duck eggs】
synchronized
synchronized Is an object lock ( It's the object that locks, not the reference ), Action granularity is the object ,java Every object in can be locked ( Only one thread can be locked successfully at a time ), And stored inside the object markword Mark the lock status .
stay jdk The old version used heavyweight locks , But in jdk The lock upgrade that appears only after the update .
Locks are generally divided into 4 Transition state : unlocked -> Biased locking -> Lightweight lock -> Heavyweight lock .
The process of lock upgrading is irreversible .
Biased locking :
When there is only one thread a When you get the lock , At this time, the lock is a bias lock , This is the time markword Point to a, When a When you ask for a lock , There is no need for any operation , This can save a lot of time .
Lightweight lock :
When a thread A After getting the lock , This time comes the thread B To apply for the lock , At this time, there is a competitive relationship , At this time, lightweight locks appear , If A At this time, the lock is still used ,B Then it spins ,B Think A It won't take long , If so B Come after A No longer in use , This is the time markword Direct to B. Generally, there are many cases like this , It's better to save cpu Utilization ratio .
Heavyweight lock :
It's similar to the lightweight lock above , But the difference is , When A In use , Now markword It's pointing A Of , Now B coming ,CDE Here we are , Now BCDE Will compete with each other , But the competition is stronger than the lightweight lock , At this time, heavyweight locks depend on the operating system Mutex lock Realization , At this time, the thread waiting for the lock hangs , When the lock is released, the operating system wakes up and tries to acquire the lock again , With the help of the operating system , Cause user mode kernel mode switching , The time cost of this process is relatively high .
In primitive times synchronzie The heavyweight lock is used , The use of cpu Not very efficient , Up to now, the lock upgrade process is used .
边栏推荐
- 企业实践开源的动机
- 2021 qunar written examination summary
- 100W了!
- HCIA experiment (06)
- 【高并发】通过源码深度分析线程池中Worker线程的执行流程
- [flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
- Reinforcement learning (III)
- PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
- 代码的表示学习:CodeBERT及其他相关模型介绍
- [递归] 938. 二叉搜索树的范围和
猜你喜欢
随机推荐
Learn NLP with Transformer (Chapter 4)
Hcip experiment (04)
二合一的集度,任重道远
HCIA实验(09)
HCIA experiment (07) comprehensive experiment
AI系统前沿动态第43期:OneFlow v0.8.0正式发布;GPU发现人脑连接;AI博士生在线众筹研究主题
Signal integrity (SI) power integrity (PI) learning notes (XXXIV) 100 rules of thumb for estimating signal integrity effects
NB-IOT控制液晶屏(日期的设置与读取)
我,AI博士生,在线众筹研究主题
【flask高级】从源码深入理解flask的应用上下文和请求上下文
Hcip experiment (01)
学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具
游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)
Nb-iot control LCD (date setting and reading)
Openstack Skyline 组件安装
PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
三万字速通Servlet
feign客户端请求之LoadBalancerLifecycle生命周期
UE4 framework introduction
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”









