当前位置:网站首页>Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
2022-08-01 07:03:00 【Pippi Young 233】
目录
前言

近日,看到有些文章说:“产生竞争时,轻量级锁升级为重量级锁,When a thread lock CAS 操作(锁重入) 会失败,So will release lock”
首先,A lock threads might even not synchronized block completes for no release the lock,怎么想都不合理
The next experiment will certify that the above “提前释放锁” Conclusion the mistake
实验
如何验证
After lock to heavyweight lock,If it releases the lock,So lock reentrant will surely fail;So only need to verify after upgrading,Reentrant lock does not block can be
流程说明
main
public static void main(String[] args) throws InterruptedException {
new Thread(() -> {
try {
// Thread0 In method of acquiring a lock for the first time
first();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Thread.sleep(5000);
new Thread(() -> {
log.debug("---------------------------Thread1 The first attempt to lock---------------------------");
synchronized (lock){
log.debug(ClassLayout.parseInstance(lock).toPrintable());
}
}).start();
}- 创建 Thread0,Perform include acquiring a lock operation method
- 主线程睡眠 5s,确保 Thread0 先执行,先获取锁,And at this time for lightweight lock
- 创建 Thread1,尝试获取锁,Produce lock competition,导致锁升级
first
static void first() throws InterruptedException {
log.debug("---------------------------Thread0 The first attempt to get the lock---------------------------");
synchronized(lock){
log.debug("Thread0 首次获取锁成功!");
log.debug(ClassLayout.parseInstance(lock).toPrintable());
Thread.sleep(10000);
// 第二次获取锁(锁重入)
reentrant();
}
}- Thread0 首次获取锁,Print lock object object head
- 先睡眠 10s(Thead1 During this period was created and attempts to acquire lock,Causing a lock escalation)
- 然后调用 reentrant() 方法,Into this method will perform a acquiring a lock operation again
reentrant
static void reentrant(){
log.debug("---------------------------Thread0 Try to lock reentrant---------------------------");
synchronized(lock){
log.debug("Thread0 锁重入成功!");
log.debug(ClassLayout.parseInstance(lock).toPrintable());
}
}- Thread0 尝试再次获取锁(锁重入)
- If not blocked,则会执行打印,To verify the conclusion of the mistake
- 若阻塞,On behalf of the lock is released and by other threads to obtain,That conclusion is right
结果

- 红色框:表示 Thread0 的时间线
- 蓝色框:表示 Thread1 的时间线
- 紫色框:表示锁的状态
时间线:
第 26s 主线程创建 Thread0 并启动,然后进入 5s 睡眠;Thread0 Try to acquiring a lock for the first time,成功了,锁状态 00 代表轻量级锁,然后进入 10s 睡眠
5s 之后的第 31s,主线程创建 Thread1 并启动;Thread1 Try to acquiring a lock for the first time,由于 Thread0 Is still in sleep,And not releasing the lock,This time the lock competition,锁升级为重量级锁;Thread1 获取锁失败,进入阻塞
10s 之后的第 36s,Thread0 Perform lock reentrant,成功了,锁状态 10 Representatives have upgraded to a heavyweight lock;Thread0 释放锁,Thread1 Will be awakened from blocking,并且获取锁成功
总结
实验结果表明,Even if the lock has been upgraded to a heavyweight lock,Weighing a lock thread lock into the operation is not happening,So nonexistent also releases the lock you say
Lock has been upgraded,But the thread lock may do not know,本人猜测:When reentrant lock or unlock found has been upgraded to heavyweight lock,And the next is not to lock the object first perform CAS 操作,But for it to monitor 对象中的 owner 属性执行 CAS 操作
(补充:轻量级锁,Lock reentrant perform CAS 操作都会失败,But it will only generate lock records,Rather than direct releases the lock)
并发编程 synchronized (五) 重量级锁、轻量级锁_Pipi Yang233的博客-CSDN博客_synchronized轻量级锁 重量级锁
边栏推荐
- 旋度(7)连接失败localhost8080;连接拒绝了
- Qt Widget project loading example of qml
- 拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播
- 支付宝如何生成及配置公钥证书
- MATLAB program design and application of MATLAB 2.5
- Bean的生命周期
- datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
- Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
- 2022杭电多校第二场1011 DOS Card(线段树)
- The BP neural network based on MATLAB voice characteristic signal classification
猜你喜欢

爬虫框架 Scrapy 详解

「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面

Data organization -- singly linked list of the linear table

升级为重量级锁,锁重入会导致锁释放?

曲柄滑块机构运动分析和参数优化

【音视频】srs直播平台搭建

仿牛客网项目总结

Self-made a remote control software - VeryControl

特别数的和

Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
随机推荐
NUMPY
仿牛客网项目总结
支付宝如何生成及配置公钥证书
Golang:go连接和使用mysql
Classwork (7) - #598. remainder operation (mod)
图像基本操作的其他内容
JSON 与 JS 对象的区别
Using FiddlerScript caught poly FiddlerScript 】 【 download
05-SDRAM:仲裁
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
weight distribution
LeetCode 0149. Maximum number of points on a line
"By sharing" northwestern university life service | | bytes a second interview on three sides by HR
2022年牛客多校第四场补题
Motion analysis and parameter optimization of crank-slider mechanism
Golang:go获取url和表单属性值
我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
Golang: go to connect and use mysql
Detailed explanation of the crawler framework Scrapy
【视觉SLAM十四讲】第一章理论详解