当前位置:网站首页>One interview question a day - the underlying implementation of synchronize and the lock upgrade process
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
2022-06-30 04:34:00 【Garfield cat】
Scope of action
synchronized You can modify static methods 、 Decorated instance method 、 Decorated code block
Method underlying principle
jvm Through... In the method table structure in the method constant pool ACC_SYNCHRONIZED Flag to distinguish whether it is a synchronous method , If it's a synchronous method , The thread will hold monitor, And then execute the method , At the end of the execution ( Whether the execution is normal or abnormal ) Release monitor
The underlying principles of code blocks
utilize monitorenter and monitorexit These two bytecode instructions . They are at the beginning and end of the synchronization block, respectively . When jvm Execute to monitorenter When the command , The current thread is trying to get monitor Ownership of objects , If not locked or held by the current thread , Just lock the counter +1; When executed monitorexit When the command , Lock counter -1; When the lock counter is 0 when , The lock is released . If you get monitor Object failed , The thread is blocked , Until the other thread releases the lock .
Lock escalation
jdk1.6 The default is to open the biased lock ,synchronize When the first thread enters , The default is modified to bias lock , Will be the current thread of ID Update to the... Of the object header makeword The thread of ID in , Add the timestamp of the biased lock and modify the flag of the biased lock to 1
When a biased lock is upgraded to a lightweight lock, there is competition between threads , Threads 1 Delayed exit code block , Threads 2 And get this lock again , And threads 2 adopt cas Spin has never been successful , At this point, it will be upgraded to a lightweight lock , If the thread is spinning 1 After execution, the lock is obtained , At this point, it will not be upgraded to a lightweight lock
Upgrading a lightweight lock to a heavyweight lock is a time when the competition between threads is particularly fierce , There are many threads competing for the same lock , Threads 1 Executing code , The remaining threads are spinning and waiting for the lock , When the number of spins reaches the set maximum, the lock is still not obtained , At this point, it is upgraded to a heavyweight lock , Heavyweight locks block the rest of the waiting threads , When the thread that obtains the lock releases the lock, it wakes up the blocked thread , Open a new round of lock competition .
边栏推荐
- 01 backpack, dynamic planning
- Redis cache avalanche, breakdown and penetration
- Basic knowledge of redis
- With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming
- Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
- What is the difference between synchronized and lock
- IO stream, buffer stream, automatic resource management
- FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
- FortiGate firewall quick initialization administrator password
- What is an optocoupler circuit and what should be paid attention to in actual use?
猜你喜欢
Basic knowledge of redis
How to repair expired SSL certificates?
Geotrustov wildcard
Myrpc version 2
史上最全的Redis基础+进阶项目实战总结笔记
File system and directory operations
什么是光耦电路,实际使用中应该注意些什么?
Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
Matlab reads fig file and restores signal
Myrpc version 3
随机推荐
Thinkphp5 implements import function
internship:接口案例实现
FortiGate creates multiple corresponding DDNS dynamic domain names for multiple ADSL interfaces
FortiGate firewall configuration link detection link monitor and status query
Idea grey screen problem
Refers to the difference between IP and *ip at output
Encapsulating JDBC tool classes
Detailed explanation of data link layer
Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
史上最全的Redis基础+进阶项目实战总结笔记
Detailed explanation of network layer
Geotrustov wildcard
JS file block to Base64 text
Internship: interface case implementation
Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
Modifier of JS regular expression
数据链路层详解
Ora-00907: missing right parenthesis problem supplement
【WEBRTC】ADM: rtc_ include_ internal_ audio_ Device triggers RTC_ Dcheck (ADM) assertion
【WEBRTC】ADM: rtc_include_internal_audio_device 触发 RTC_DCHECK(adm) 断言