当前位置:网站首页>Concurrent programming - deadlock troubleshooting and handling
Concurrent programming - deadlock troubleshooting and handling
2022-07-05 07:05:00 【Handling Gong】
Preface
I believe many programmers have encountered deadlock in their work , For example, database deadlock 、 Application deadlock . So how do we deal with this situation ? This paper will sort out the relevant ideas and solutions through a simple case , I hope it can help children's shoes in similar situations .
1. What is a deadlock ?
A deadlock is when two or more threads are executing , One caused by competition for resources The phenomenon of waiting for each other , If there is no external force to interfere, they will not be able to push forward , If system resources are sufficient , Process resource requests can be satisfied , The possibility of deadlock is very low , Otherwise, we will be locked in a deadlock due to competing for limited resources .
The main cause of deadlock :
① Insufficient system resources ;
② The recommended sequence of process operation is out of time ;
③ Misallocation of resources ;
2. Handwriting a deadlock case Case study
Above we know what a deadlock is and the conditions under which it occurs , Then let's experience deadlock by writing a case :
public static void main(String[] args) {
final Object objectA = new Object();
final Object objectB = new Object();
new Thread(() -> {
synchronized (objectA) {
System.out.println(" Threads " + Thread.currentThread().getName() + " start-up , hold A lock ...");
try {
TimeUnit.SECONDS.sleep(1L);
synchronized (objectB) {
System.out.println(" Threads " + Thread.currentThread().getName() + " Try to get B lock , success !!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(() -> {
synchronized (objectB) {
System.out.println(" Threads " + Thread.currentThread().getName() + " start-up , hold B lock ...");
try {
TimeUnit.SECONDS.sleep(1L);
synchronized (objectA) {
System.out.println(" Threads " + Thread.currentThread().getName() + " Try to get A lock , success !!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
You can see , Two threads compete for resources , Causes deadlock , The program is running all the time , Can't end .
3. Deadlock detection
By handwriting deadlock case, Know that there is a deadlock , Then the real production environment cannot locate whether there is a deadlock , For example, I wrote a while(true) {} Infinite loop a block of code .
3.1 Command line troubleshooting
How to locate the deadlock ? It's simple , Use JDK The order that comes with you - jstack Check it out. JVM Stack information for :
① jps - See what's running Java process ;
② jstack process ID;
Through the output information, you can see , Deadlock detected , It also points out the specific number of code lines that cause deadlock .
3.2 Graphical interface troubleshooting
There is another way to troubleshoot deadlocks , It's also JDK A graphical command provided jconsole, Through this command, you can connect to specific Java process , View it graphically .
Two ways , Can locate the location where the deadlock occurs . After positioning , Optimize the program to avoid deadlock ( Avoid the cause of deadlock ).
above , It is the two simplest and most practical methods for checking deadlocks , Encounter program jam in real production environment 、 When there is no response , This way can be used to check whether the deadlock is caused by the non release of a large amount of resources .
边栏推荐
- Database mysql all
- Vant weapp swippecell set multiple buttons
- Rehabilitation type force deduction brush question notes D1
- Volcano resource reservation feature
- . Net core stepping on the pit practice
- 你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
- Ros2 - common command line (IV)
- Huawei bracelet, how to add medicine reminder?
- Sum of two numbers, the numbers in the array are converted to decimal, added, and output inversely
- SD_CMD_RECEIVE_SHIFT_REGISTER
猜你喜欢
三体目标管理笔记
Ros2 - install ros2 (III)
A brief introduction to heading/pitch/roll and omega/phi/kappa
Ros2 - workspace (V)
An article was opened to test the real situation of outsourcing companies
Ros2 - ros2 vs. ros1 (II)
摄像头的MIPI接口、DVP接口和CSI接口
Utf8 encoding
[algorithm post interview] interview questions of a small factory
【软件测试】02 -- 软件缺陷管理
随机推荐
NVM Downloading npm version 6.7.0... Error
Qt项目中的日志库log4qt使用
Vant weapp swippecell set multiple buttons
Ros2 - node (VII)
代码中的英语全部
The difference between new and malloc
Xiaomi written test real question 1
U-Boot初始化及工作流程分析
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
Markdown syntax
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
Orin 安装CUDA环境
【obs】x264编码:“buffer_size“
Instruction execution time
Executealways of unity is replacing executeineditmode
ROS2——安装ROS2(三)
Vscode configures the typera editor for MD
PHY驱动调试之 --- MDIO/MDC接口22号和45号条款(一)
PowerManagerService(一)— 初始化
What is linting