当前位置:网站首页>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 ;
边栏推荐
- stm32F407------SPI
- ROS learning notes (IV) ROS cannot solve rosdep init or update
- I want to manage money. I don't understand. Is there a principal guaranteed financial product?
- 约瑟夫环问题
- 十九岁的总结
- Idea 必备插件
- Enumeration classes and magic values
- What is metauniverse gamefi chain game system development? Development and application case and analysis of gamefi metauniverse NFT chain game system
- ROS学习笔记(四)ros 无法rosdep init 或者update解决方法
- Wu Enda logistic regression 2
猜你喜欢

EDI docking commercehub orderstream

四六级

How to install govendor and open a project

Three dimensional function display of gray image

WPF 实现用户头像选择器

I'm also drunk. Eureka delayed registration and this pit!

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

EDI 对接CommerceHub OrderStream

Idea essential plug-ins

Food safety | eight questions and eight answers take you to know crayfish again! This is the right way to eat!
随机推荐
交友活动记录
ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
Highlights
我们被一个 kong 的性能 bug 折腾了一个通宵
Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?
P2P 之 UDP穿透NAT的原理与实现
Lvgl 7.11 tileview interface cycle switching
爬虫框架-crawler
OSPF---开放式最短优先路径协议
带你初步了解多方安全计算(MPC)
STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解
吴恩达机器学习编程作业无法暂停pause问题解决
做智能硬件要考虑的产品生命周期
Several implementations of PHP to solve concurrency problems
Thesis reading_ Multi task learning_ MMoE
Redis源码与设计剖析 -- 17.Redis事件处理
Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
我也是醉了,Eureka 延迟注册还有这个坑!
实时黄金交易平台哪个可靠安全?
I'm also drunk. Eureka delayed registration and this pit!