当前位置:网站首页>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轻量级锁 重量级锁
边栏推荐
- 阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
- 问下 mysql向pg同步多个表的话 有什么好的方案吗?
- 图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
- Dart exception details
- 图像基本操作的其他内容
- Datagrip error "The specified database userpassword combination is rejected..."Solutions
- Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
- Srping bean in the life cycle
- Offer brush questions - 1
- Talk about the bugs in using for in to traverse the array in js
猜你喜欢
mysql的行锁和间隙锁
奇葩问题 npm install 报错 gyp ERR
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
基于MATLAB的BP神经网络进行语音特征信号分类
从购买服务器到网站搭建成功保姆级教程~超详细
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
Motion analysis and parameter optimization of crank-slider mechanism
金山打字通 官网 下载
随机推荐
企业员工人事管理系统(数据库课设)
The Bean's life cycle
2022杭电多校第二场1011 DOS Card(线段树)
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
小白的0基础教程SQL: 关系数据库概述 02
Practical training Navicat Chinese and English mode switching
Data organization -- singly linked list of the linear table
LeetCode 0150. Reverse Polish Expression Evaluation
crypto-js uses
The BP neural network based on MATLAB voice characteristic signal classification
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
NIO programming
上课作业(7)——#598. 取余运算(mod)
插入排序—直接插入排序和希尔排序
JSON 与 JS 对象的区别
Vim三种模式
小白的0基础教程SQL: 安装MYSQL 03
Solve the problem of page flicker caused by browser scroll bars
LeetCode 0149. 直线上最多的点数
05-SDRAM: Arbitration