当前位置:网站首页>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轻量级锁 重量级锁
边栏推荐
- Jupyter shortcuts
- R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图可视化股票月收益率数据、使用百分比显示Y轴坐标数据、使用不同的色彩表征正负收益率
- 信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
- uva12326
- Using FiddlerScript caught poly FiddlerScript 】 【 download
- 奇葩问题 npm install 报错 gyp ERR
- JS的运行原理
- JVM:运行时数据区-PC寄存器(程序计数器)
- 曲柄滑块机构运动分析和参数优化
- 「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
猜你喜欢
Offer brush questions - 1
自制一款远程控制软件——VeryControl
Dbeaver connect the MySQL database and error Connection refusedconnect processing
声音信号处理基频检测和时频分析
戴尔PowerEdge服务器R450 RAID配置步骤
MySQL row locks and gap locks
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
The log causes these pits in the thread block, you have to prevent
测试工具(四)Jenkins环境搭建与使用
Self-made a remote control software - VeryControl
随机推荐
Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
rhcsa 第三次
2022.7.27 Selected lectures on good topics
我的创作纪念日
滚动条样式修改
MySQL row locks and gap locks
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
基于百度OCR的网站验证码在线识别
MATLAB program design and application of MATLAB 2.5
Data organization -- singly linked list of the linear table
小程序通过云函数操作数据库【使用get取数据库】
05-SDRAM:仲裁
crypto-js使用
Dbeaver connect the MySQL database and error Connection refusedconnect processing
Golang:go静态文件处理
Using FiddlerScript caught poly FiddlerScript 】 【 download
LeetCode 0149. Maximum number of points on a line
matlab 风速模型 小波滤波
Srping bean in the life cycle