当前位置:网站首页>并发编程 — 死锁排查及处理
并发编程 — 死锁排查及处理
2022-07-05 06:40:00 【搬运Gong】
前言
相信不少程序员在工作中都遇到过死锁这种场景,比如数据库死锁、应用程序死锁。那么大家都是如何来处理这种情况的呢?本文将通过一个简单的案例来进行梳理相关思路及解决方案,希望可以帮到遇到类似情况的童鞋。
1. 什么是死锁?
死锁是指两个或者两个以上的线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力干涉那它们都将无法推进下去,如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则就会因争夺有限的资源而陷入死锁。

产生死锁的主要原因:
① 系统资源不足;
② 进程运行推荐的顺序不合时候;
③ 资源分配不当;
2. 手写一个死锁的 case案例
上面我们知道了什么是死锁以及死锁产生的条件,那么来通过手写一个案例来体验一下死锁:
public static void main(String[] args) {
final Object objectA = new Object();
final Object objectB = new Object();
new Thread(() -> {
synchronized (objectA) {
System.out.println("线程 " + Thread.currentThread().getName() + " 启动,持有 A锁...");
try {
TimeUnit.SECONDS.sleep(1L);
synchronized (objectB) {
System.out.println("线程 " + Thread.currentThread().getName() + " 尝试获取 B 锁,成功!!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(() -> {
synchronized (objectB) {
System.out.println("线程 " + Thread.currentThread().getName() + " 启动,持有 B锁...");
try {
TimeUnit.SECONDS.sleep(1L);
synchronized (objectA) {
System.out.println("线程 " + Thread.currentThread().getName() + " 尝试获取 A 锁,成功!!");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
可以看到,两个线程互相争抢资源,导致死锁产生,程序处于一直运行阶段,无法结束。
3. 死锁排查
通过手写的死锁 case,知道了存在死锁,那么真实的生产环境是无法定位到是不是真的出现了死锁情况的,比如写了一个 while(true) {} 无限循环一段代码块。
3.1 命令行排查
如何定位产生了死锁呢?很简单,使用JDK 自带的命令- jstack 来查看 JVM 的堆栈信息:
① jps -查看正在运行的 Java 进程;
② jstack 进程 ID;


通过输出的信息可以看到,检查到了死锁,并且指出了具体产生死锁的代码行数。
3.2 图形界面排查
还有另外一种方式来排查死锁,也是 JDK 提供的一个图形化的命令 jconsole,通过这个命令可以连接到具体的Java 进程,在图形化中进行查看。


两种方式,都可以定位到死锁出现的位置。定位到位置后,优化程序规避死锁即可(通过死锁产生的原因进行规避)。
以上,是两种最简单也最实用的排查死锁的方法,在真实的生产环境遇到程序卡顿、无响应的时候,可通过这种方式来排查是否是因为产生了死锁占用大量资源不释放导致的。
边栏推荐
- [nvidia] CUDA_ VISIBLE_ DEVICES
- The problem of Chinese garbled code in the vscode output box can be solved once for life
- 达梦数据库全部
- ROS2——topic话题(八)
- postmessage通信
- Xavier CPU & GPU high load power consumption test
- Rehabilitation type force deduction brush question notes D1
- Xavier CPU & GPU 高负载功耗测试
- Getting started with typescript
- ROS2——node节点(七)
猜你喜欢

Configuration method and configuration file of SolidWorks GB profile library

Get class files and attributes by reflection

摄像头的MIPI接口、DVP接口和CSI接口
![LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation](/img/1f/30407bce35c324cc90f139b6f09fd1.jpg)
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation

NVM Downloading npm version 6.7.0... Error

Pycahrm reports an error: indentation error: unindent does not match any outer indentation

三体目标管理笔记

Page type

how to understand the “model independent.“

‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
随机推荐
H5 embedded app adapts to dark mode
ROS2——Service服务(九)
2022 winter vacation training game 5
Skywalking全部
解读最早的草图-图像翻译工作SketchyGAN
Ret2xx---- common CTF template proposition in PWN
The “mode“ argument must be integer. Received an instance of Object
ROS2——安装ROS2(三)
LSA Type Explanation - lsa-5 (type 5 LSA - autonomous system external LSA) and lsa-4 (type 4 LSA - ASBR summary LSA) explanation
ROS2——工作空间(五)
[MySQL 8.0 does not support capitalization of table names - corresponding scheme]
Log4qt usage of logbase in QT project
PR automatically moves forward after deleting clips
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
Initialization of global and static variables
Skywalking all
2. Addition and management of Oracle data files
vim
Getting started with typescript
Xavier CPU & GPU 高负载功耗测试