当前位置:网站首页>Briefly describe synchronized and lock upgrade
Briefly describe synchronized and lock upgrade
2022-07-25 17:42:00 【Suddenly】
1、Synchronized lock
adopt Synchronized Implement synchronization mechanism , It belongs to a pessimistic lock , When operating data , Determine whether the object is locked , If it is locked, it enters the blocking state until the occupied thread is released , If there is no lock or the current thread already has a lock on the operation object, lock it , Release the data after operation .
When a class object is locked , By Synchronized The modified method will cause blocking , Not Synchronized The modified method will not cause blocking .
Synchronized Solve the problem of locking yourself through Reentry ; Because it locks by judging that the object has no lock or that the current thread has the lock of the current object , If there is no reentrant class, two synchronous methods calling each other will cause deadlock .
Synchronized It's also a kind of unfair lock , Because it doesn't allocate locks according to the time of applying for locks , Instead, the lock is obtained in the form of competition , This improves execution performance .
Method lock :synchronized When modifying a method , Each instance object corresponds to a lock .
Object lock :synchronized Decorate a method or block of code , Each instance object corresponds to a lock .
Kind of lock :synchronized Modify static methods or static code blocks , All instance objects share the same lock , We call it a class lock .
In a class , There are both class lock methods and object lock methods . When multithreading two methods , Will threads block .
2、 Lock escalation
From low to high is : Biased locking 、 Lightweight lock ( spinlocks )、 Heavyweight lock , Locks can only be upgraded, not demoted ;
Why do I need to upgrade locks : ** When a thread obtains the same lock multiple times , If you have to compete for locks every time, it will increase a lot of unnecessary costs , in order to Reduce CPU The cost of , Provides execution efficiency .**
Implementation of upgrade lock , Depends on... In the object header Mark Word, It will record the status of the first thread to obtain the lock ID, And lock status ;
- Biased locking : When a thread A After getting the lock , The thread A Of ID Record to object header , And the lock is upgraded to bias lock ( Lock sign -01( No lock or bias is 01), Whether the preference is 1); Subsequent threads A When acquiring the lock of the object again , Find out The object is in a biased lock state , Thread in object header ID With threads A Agreement , At this time, the bias lock will be obtained directly ;
- Lightweight lock : When another thread B When you get the lock , The lock is found to be in a biased state , The thread B Will pass CAS Try to scramble for the lock 【 Modify the thread in the object header ID】( Judging threads A Survival , If you live, you can't modify ), If the partial lock fails to grab the lock , Lightweight locks tend to be upgraded , If the lock grabbing is successful, execute the code ;
- Heavyweight lock : Lightweight locks compete for locks by spinning , And it is applicable to the case that the thread does not hold the lock for a long time **( Because blocking threads requires CPU state , It costs a lot , So spin )**; When the number of spins of the thread exceeds the threshold ( The default is 10) In order to prevent cpu Idle , Will upgrade the spin lock to a heavyweight lock , And block the thread that does not acquire the lock ;
Lightweight lock modification thread ID The way :
After the deflection lock is upgraded to a lightweight lock , Threads B Lock the markword Copy to the thread itself markword In the space , And then through cas To set the thread in the lock object ID value ;
边栏推荐
猜你喜欢

I2C通信——时序图

Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant

一篇文章了解超声波加湿器

十九岁的总结

Starting from business needs, open the road of efficient IDC operation and maintenance

Ultimate doll 2.0 | cloud native delivery package

哈夫曼树的构建

博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理

stm32F407------SPI

Highlights
随机推荐
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
如何看一本书
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
Function name pointer and function pointer
Principle and implementation of UDP penetration NAT in P2P
什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
吴恩达机器学习编程作业无法暂停pause问题解决
我也是醉了,Eureka 延迟注册还有这个坑!
pgsql有没有好用的图形化管理工具?
「数字安全」警惕 NFT的七大骗局
stm32F407------SPI
接口自动化测试Postman+Newman+Jenkins
Go defer and recover simple notes
服务器端架构设计期末复习知识点总结
Take you to a preliminary understanding of multiparty secure computing (MPC)
POWERBOARD coco! Dino: let target detection embrace transformer
Update 3dcat real time cloud rendering V2.1.2 release
What is metauniverse gamefi chain game system development? Development and application case and analysis of gamefi metauniverse NFT chain game system
How to prevent the unburned gas when the city gas safety is alarmed again?
Random talk on generation diffusion model: DDPM = Bayesian + denoising