当前位置:网站首页>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轻量级锁 重量级锁
边栏推荐
猜你喜欢

我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

日志导致线程Block的这些坑,你不得不防

滚动条样式修改

Using FiddlerScript caught poly FiddlerScript 】 【 download

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

安装SQL Server详细教程

Golang:go模版引擎的使用

小程序通过云函数操作数据库【使用get取数据库】

Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
随机推荐
datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
matlab 风速模型 小波滤波
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
Golang:go获取url和表单属性值
图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
uva12326
Leetcode第 304 场周赛
Jupyter shortcuts
测试工具(四)Jenkins环境搭建与使用
并发编程13-JUC之CountDownLatch
Go 支持 OOP: 用 struct 代替 class
从离线到实时对客,湖仓一体释放全量数据价值
JSON 与 JS 对象的区别
Self-made a remote control software - VeryControl
Win任务栏图标异常解决
sum of special numbers
JS的运行原理
torch
Srping bean in the life cycle
rhcsa 第三次