当前位置:网站首页>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轻量级锁 重量级锁
边栏推荐
- The log causes these pits in the thread block, you have to prevent
- 表的创建、修改与删除
- 实战演练 Navicat 中英文模式切换
- dbeaver连接MySQL数据库及错误Connection refusedconnect处理
- return; represents meaning
- matlab simulink 粒子群优化模糊pid控制的电机泵
- 信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
- NIO programming
- MATLAB program design and application of MATLAB 2.5
- AspNet.WebApi.Owin custom Token request parameters
猜你喜欢
NIO programming
Win任务栏图标异常解决
Using FiddlerScript caught poly FiddlerScript 】 【 download
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
matlab 风速模型 小波滤波
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
从购买服务器到网站搭建成功保姆级教程~超详细
从零开始—仿牛客网讨论社区项目(一)
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
Matlab simulink particle swarm optimization fuzzy pid control motor pump
随机推荐
CSP-S2019兴奋不已
ORACLE modify another user package (package)
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
爬虫基本原理介绍、实现以及问题解决
图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
特殊的日子,值得纪念
权重等比分配
2022杭电多校第二场1011 DOS Card(线段树)
weight distribution
【视觉SLAM十四讲】第一章理论详解
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
Data organization -- singly linked list of the linear table
企业员工人事管理系统(数据库课设)
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
uva10825
mysql中添加字段的相关问题
深度比较两个对象是否相同
Practical training Navicat Chinese and English mode switching
Using FiddlerScript caught poly FiddlerScript 】 【 download
rhcsa 第四天