当前位置:网站首页>Causes and solutions of deadlock in threads
Causes and solutions of deadlock in threads
2022-07-27 15:43:00 【Skinny monkey 117】
Catalog
The dining problem of philosophers
The deadlock problem
Deadlock : The circular waiting problem of locking objects , Use jconsole To check for deadlocks
Deadlock is a serious BUG. The thread that causes a program " stuck ", Not working properly !
/** * Deadlock */ 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(" Sing first , Write the project again "); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (projectLock) { System.out.println(" The song is over , You can write the project "); } } }, "A"); Thread t2 = new Thread(() -> { synchronized (projectLock) { System.out.println(" Write the project first , Sing again "); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (songLock) { System.out.println(" Finish the project , A song "); } } }, "B"); t1.start(); t2.start(); } }
Use jconsole Check for deadlocks
The dining problem of philosophers
There's a table , Surrounded by philosophers , There is a plate of spaghetti in the middle of the table .
Between two philosophers , There is a chopstick .
Every philosopher does only two things : Think about life perhaps Eat noodles .
When I think about life, I will put down my chopsticks , When you eat noodles, you will pick up the chopsticks on the left and right sides ( First pick up the left , Then pick up the right ).
If philosophers find that chopsticks can't be picked up ( Occupied by someone else ), It will block and wait .[ Key points ] Suppose at the same time , The five philosophers picked up the chopsticks on their left hand side at the same time , Then try holding right-handed chopsticks , You will find that the chopsticks on your right hand are occupied . Because philosophers don't give in to each other , This is when Deadlock .
Avoid deadlock
Deadlock Relieving , Just break the condition of this cycle waiting , Multiple threads acquire resources without forming a ring .
Let's go one step at a time , Avoid deadlock .
/** * Avoid deadlock */ 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(" The song is over , You can write the project "); synchronized (songLock) { System.out.println(" Sing first , Write the project again √"); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }, "A"); Thread t2 = new Thread(() -> { synchronized (projectLock) { System.out.println(" Write the project first , Sing again "); try { Thread.sleep(300); } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (songLock) { System.out.println(" Finish the project , A song "); } } }, "B"); t1.start(); t2.start(); } }
边栏推荐
- JS uses extension operators (...) to simplify code and simplify array merging
- 使用解构交换两个变量的值
- Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
- 【剑指offer】面试题51:数组中的逆序对——归并排序
- C语言:自定义类型
- 折半插入排序
- C语言:扫雷小游戏
- Spark3中Catalog组件设计和自定义扩展Catalog实现
- NPM install error unable to access
- Spark troubleshooting finishing
猜你喜欢

Spark 3.0 adaptive execution code implementation and data skew optimization

C语言:函数栈帧

QT (five) meta object properties

Binder初始化过程

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

HaoChen CAD building 2022 software installation package download and installation tutorial

Spark 3.0 Adaptive Execution 代码实现及数据倾斜优化

Leetcode 783. binary search tree node minimum distance tree /easy

Complexity analysis

Leetcode 74. search two-dimensional matrix bisection /medium
随机推荐
Analysis of spark task scheduling exceptions
Read the wheelevent in one article
使用双星号代替Math.pow()
Spark RPC
复杂度分析
JS uses unary operators to simplify string to number conversion
What format is this data returned from the background
JS uses extension operators (...) to simplify code and simplify array merging
Spark 3.0 testing and use
md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
面试重点——传输层的TCP协议
Spark 本地程序启动缓慢问题排查
表格插入行内公式后,单元格失去焦点
C language: factorial recursive implementation of numbers
Spark troubleshooting finishing
使用Lombok导致打印的tostring中缺少父类的属性
聊聊面试必问的索引
Leetcode 1143. dynamic programming of the longest common subsequence /medium
设置提示框位置随鼠标移动,并解决提示框显示不全的问题




