当前位置:网站首页>Synchronized lock
Synchronized lock
2022-07-27 07:37:00 【The season when the monsoon dies】
Usage mode :
- Method lock : The lock object is the caller itself .
- Code block lock : Lock object for synchronized(Object o) Incoming objects .
Realization principle :
Mark Structure

characteristic :
The lock flag bits of non lock state and biased lock state are 01, All fields that are biased to lock are distinguished .
The deflection lock has no place to store hashcode, Lightweight lock hashcode Stored in the recorded stack frame , Heavyweight locks will be in Monitor It's recorded in hashCode.
Synchronized Four states
- No lock state : The lock is created to handle the state that is not acquired by any thread . Of the newly created object Mark Word Medium Thread Id by 0, That at this time In a biased but not biased to any thread , It's also called Anonymous bias state (anonymously biased).
- Biased locking : When the lock is acquired by the thread for the first time, it enters the biased lock mode . In the process of acquiring the lock , Threads pass through CAS operation , In the lock object markword Record the biased lock state and the thread of the current thread ID, When the thread requests the lock again , Just check the lock object markword Whether the lock mark of is a thread that biases locks and records ID Is it the current thread's ID, There is no need to do anything about applying for a lock , even CAS No need , So the performance is high . Biased locking uses a mechanism that waits until there is competition to release the lock , So when other threads try to compete for biased locks , The thread holding the biased lock will release the lock . When the deflection lock is released , The thread holding the biased lock will be run to a safe point , Then check whether the thread survives and whether it needs to continue holding the biased lock , If the thread survives or needs to continue holding the biased lock , The preference lock is upgraded to lightweight lock . If the thread does not survive or does not need to continue to hold the biased lock , Then the lock state will be reset to the unlocked state , And then through CAS Operations are biased towards new threads . Bias locked in jdk1.6 After the default on , Can pass jvm Parameter to close the bias lock :-XX:-UseBiasedLocking = false.
// Closing delay opening bias lock
‐XX:BiasedLockingStartupDelay=0
// Do not bias the lock
‐XX:‐UseBiasedLocking
// Enable bias lock
-XX:+UseBiasedLocking- Lightweight lock : When a thread acquires a lightweight lock, it will create a space in the stack frame for storing lock records (Lock Record), Then lock the markword Copy to this space , be called Displaced MarkWord( be homeless markword). After use CAS The mechanism keeps trying to lock the middle of the object's header markword Replace with a pointer to the lock record in the current stack frame , And the current stack frame owner The pointer changes to the lock object header MarkWord The pointer . If this operation succeeds , Then the current thread obtains a lightweight lock . If it doesn't work , Then the lock is upgraded to a heavyweight lock . That is, the scenario for lightweight locks is where threads alternately execute synchronous blocks , If there are occasions where multiple threads access the same lock at the same time , It will cause the lightweight lock to expand to heavy Magnitude lock .
- Heavyweight lock : Except for the thread that owns the lock , Other threads block . For the pursuit of throughput , A scenario where a synchronization block or synchronization method takes a long time to execute .
The calling object of partial lock revocation HashCode
- Lightweight locks will be recorded in the lock record hashCode
- Heavyweight locks will be in Monitor It's recorded in hashCode
- When the object can be biased ,MarkWord Will become unlocked , And can only be upgraded to a lightweight lock ;
- When the object is in a bias lock , call HashCode The deflection lock will be forcibly upgraded to a weight lock .
Call of partial lock revocation wait/notify
Let's say I have two threads , A The first thing to get is the deflection lock , that B Spin waiting lock acquisition will occur . When B When the spin acquires the lock , Will check the A Is it still alive , If you don't , Then the lock is reset to the unlocked state , here B Compete to lock , Set it to bias lock . If you survive , Then the lock is upgraded to a lightweight lock . When B When the spin fails and the lock is not obtained , The lock is automatically upgraded to a heavyweight lock , All threads will not spin when competing for heavyweight locks . And in the future competition , This lock will only be in the state of heavyweight lock . Unless GC, Recycle the lock as garbage . The second scenario , If there is N Threads compete for locks at the same time , Only one spin failed to acquire the lock , Then the lock will also be upgraded to a heavyweight lock .
边栏推荐
- Use of tigervnc
- Chapter 6 Shell Logic and Arithmetic
- (2022牛客多校三)A-Ancestor(LCA)
- 连接MySQL时报错:Public Key Retrieval is not allowed 【解决方法】
- 杂谈:高考
- ARP广播实践案例
- Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
- SQL statement batch update time minus 1 day
- 单元测试系统化讲解之Mockito
- Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query
猜你喜欢

flink1.14 sql基础语法(一) flink sql表查询详解

Am I delayed by the code... Unfortunately, I became a programmer

(2022牛客多校三)J-Journey(dijkstra)

(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)

Zabbix: map collected values to readable statements

(2022 Niuke multi school III) a-ancestor (LCA)

drawImage方法第一次调用不显示图片的解决方式

Routing between VLANs (explanation + verification)

vlan间路由(讲解+验证)

Use of tigervnc
随机推荐
(2022 Niuke multi school III) j-journey (Dijkstra)
单元测试系统化讲解之Mockito
在rhel7.3中编译和使用log4cxx
Demonstrate the use of foreign keys with Oracle
杂谈:高考
UiAutomator常用类之UI手势动作
Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL
单片机多级菜单
ARP broadcasting practice cases
Help send a recruitment, base all over the country. If you are interested, you can come and have a look
js做一个红绿灯
Systematic explanation of unit testing: mockito
临界区(critical section 每个线程中访问 临界资源 的那段代码)和互斥锁(mutex)的区别(进程间互斥量、共享内存、虚拟地址)
Expose Prometheus metrics in Perl programs
用shell来计算文本中的数字之和
Temperature and humidity measurement and display device based on Arduino
SQL statement batch update time minus 1 day
闭散列和开散列解决哈希冲突
VLAN trunk实验
Single arm routing (explanation + experiment)