当前位置:网站首页>线程中死锁的成因及解决方案
线程中死锁的成因及解决方案
2022-07-27 14:35:00 【瘦皮猴117】
目录
死锁问题
死锁:锁对象的循环等待问题,使用jconsole来检查死锁
死锁是一种严重的 BUG。导致一个程序的线程 "卡死",无法正常工作!
/** * 死锁 */ public class DeadLockTest { public static void main(String[] args) { Object projectLock = new Object(); Object songLock = new Object(); Thread t1 = new Thread(() -> { synchronized (songLock) { System.out.println("先唱歌,再写项目"); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (projectLock) { System.out.println("歌唱完了,可以写项目了"); } } }, "A"); Thread t2 = new Thread(() -> { synchronized (projectLock) { System.out.println("先写项目,再唱"); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (songLock) { System.out.println("写完项目,来一首歌"); } } }, "B"); t1.start(); t2.start(); } }
使用jconsole检查死锁
哲学家就餐问题
有个桌子,围着一圈哲学家,桌子中间放着一盘意大利面。
每个哲学家两两之间,放着一根筷子。
每个哲学家只做两件事:思考人生 或者 吃面条。
思考人生的时候就会放下筷子,吃面条就会拿起左右两边的筷子(先拿起左边, 再拿起右边)。
如果哲学家发现筷子拿不起来了(被别人占用了),就会阻塞等待。[关键点] 假设同一时刻, 五个哲学家同时拿起左手边的筷子,然后再尝试拿右手的筷子,就会发现右手的筷子都被占用了。由于哲学家们互不相让,这个时候就形成了死锁 。
避免死锁
解除死锁,只需要破坏这个循环等待的条件即可,多个线程获取资源不要成环即可。
各让一步,避免死锁。
/** * 避免死锁 */ public class DeadLockTest { public static void main(String[] args) { Object projectLock = new Object(); Object songLock = new Object(); Thread t1 = new Thread(() -> { synchronized (projectLock) { System.out.println("歌唱完了,可以写项目了"); synchronized (songLock) { System.out.println("先唱歌,再写项目√"); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }, "A"); Thread t2 = new Thread(() -> { synchronized (projectLock) { System.out.println("先写项目,再唱"); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (songLock) { System.out.println("写完项目,来一首歌"); } } }, "B"); t1.start(); t2.start(); } }
边栏推荐
- Spark Bucket Table Join
- md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
- Spark 3.0 测试与使用
- [正则表达式] 匹配开头和结尾
- Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
- “router-link”各种属性解释
- Spark Filter算子在Parquet文件上的下推
- Fluent -- layout principle and constraints
- js操作dom节点
- 使用Lombok导致打印的tostring中缺少父类的属性
猜你喜欢

Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`

Record record record

【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版

C语言:动态内存函数

Spark Bucket Table Join

Complexity analysis

QT (XIII) qchart drawing line chart

QT (IV) mixed development using code and UI files

【剑指offer】面试题54:二叉搜索树的第k大节点

C语言:函数栈帧
随机推荐
Spark Filter算子在Parquet文件上的下推
Jump to the specified position when video continues playing
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
2022-07-27 Daily: IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
一文读懂鼠标滚轮事件(wheelEvent)
Static关键字的三种用法
聊聊ThreadLocal
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
Push down of spark filter operator on parquet file
Spark 3.0 testing and use
JS uses for in and for of to simplify ordinary for loops
How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?
【云享读书会第13期】音频文件的封装格式和编码格式
Tools - common methods of markdown editor
Binder初始化过程
Network equipment hard core technology insider router 19 dpdk (IV)
Google team launches new transformer to optimize panoramic segmentation scheme CVPR 2022
MLX90640 红外热成像仪测温传感器模块开发笔记(七)
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
multimap案例




