死锁-写一个死锁的例子
- 什么是死锁
- 死锁产生的条件
- 如何避免死锁
- 死锁预防
- 死锁避免
- 死锁检测
- 死锁解除
- 鸵鸟策略
- 手写一个死锁的例子
https://blog.51cto.com/u_16213642/8352155
什么是死锁
在两个或者多个并发进程中,如果每个进程持有某种资源而又等待其它进程释放它或它们现在保持着的资源,在未改变这种状态之前都不能向前推进,称这一组进程产生了死锁。通俗的讲就是两个或多个进程无限期的阻塞、相互等待的一种状态。
死锁产生的条件
死锁产生的四个必要条件:(有一个条件不成立,则不会产生死锁)
- 互斥条件:一个资源一次只能被一个进程使用
- 请求与保持条件:一个进程因请求资源而阻塞时,对已获得资源保持不放
- 不剥夺条件:进程获得的资源,在未完全使用完之前,不能强行剥夺
- 循环等待条件:若干进程之间形成一种头尾相接的环形等待资源关系
如何避免死锁
常用的处理死锁的方法有:死锁预防、死锁避免、死锁检测、死锁解除、鸵鸟策略。
死锁预防
基本思想就是确保死锁发生的四个必要条件中至少有一个不成立:
① 破除资源互斥条件
② 破除“请求与保持”条件:实行资源预分配策略,进程在运行之前,必须一次性获取所有的资源。缺点:在很多情况下,无法预知进程执行前所需的全部资源,因为进程是动态执行的,同时也会降低资源利用率,导致降低了进程的并发性。
③ 破除“不可剥夺”条件:允许进程强行从占有者那里夺取某些资源。当一个已经保持了某些不可被抢占资源的进程,提出新的资源请求而不能得到满足时,它必须释放已经保持的所有资源,待以后需要时再重新申请。这意味着进程已经占有的资源会被暂时被释放,或者说被抢占了。
④ 破除“循环等待”条件:实行资源有序分配策略,对所有资源排序编号,按照顺序获取资源,将紧缺的,稀少的采用较大的编号,在申请资源时必须按照编号的顺序进行,一个进程只有获得较小编号的进程才能申请较大编号的进程。
死锁避免
死锁预防通过约束资源请求,防止4个必要条件中至少一个的发生,可以通过直接或间接预防方法,但是都会导致低效的资源使用和低效的进程执行。而死锁避免则允许前三个必要条件,但是通过动态地检测资源分配状态,以确保循环等待条件不成立,从而确保系统处于安全状态。所谓安全状态是指:如果系统能按某个顺序为每个进程分配资源(不超过其最大值),那么系统状态是安全的,换句话说就是,如果存在一个安全序列,那么系统处于安全状态。银行家算法是经典的死锁避免的算法。
死锁检测
死锁解除
死锁解除的常用方法就是终止进程和资源抢占,回滚。所谓进程终止就是简单地终止一个或多个进程以打破循环等待,包括两种方式:终止所有死锁进程和一次只终止一个进程直到取消死锁循环为止;所谓资源抢占就是从一个或者多个死锁进程那里抢占一个或多个资源。
鸵鸟策略
把头埋在沙子里,假装根本没发生问题。因为解决死锁问题的代价很高,因此鸵鸟策略这种不采取任何措施的方案会获得更高的性能。当发生死锁时不会对用户造成多大影响,或发生死锁的概率很低,可以采用鸵鸟策略。大多数操作系统,包括 Unix,Linux 和 Windows,处理死锁问题的办法仅仅是忽略它。
手写一个死锁的例子
必须每一个字母都能手敲出来
public class ThreadLock {
public static void main(String[] args) {
String l1 = "lock1";
String l2 = "lock2";
Thread t1 = new Thread(() -> {
synchronized (l1) {
System.out.println("thread1 get lock1");
try{
Thread.sleep(1000); // 使用到注意处理异常
} catch (InterruptedException ex) {
ex.printStackTrace();
}
synchronized (l2) {
System.out.println("thread1 get lock2");
}
}
});
Thread t2 = new Thread(() -> {
synchronized (l2) {
System.out.println("thread2 get lock2");
try{
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
synchronized (l1) {
System.out.println("thread2 get lock1");
}
}
});
t1.start();
t2.start();
}
}
运行结果:
手动编译此代码
javac ThreadLock.java
生成编译后的class文件
使用命令 java 执行class文件
java com.example.demo.ThreadLock
注意:
这一步可能出现问题
- 不能带后缀
- 可能报错(找不到或无法加载主类)
参考 https://blog.csdn.net/gao_zhennan/article/details/112749742 解决
可以看到程序无法结束,无法继续执行,产生了死锁。
首先使用jps查看线程 ID
打印线程堆栈
jstack 线程ID
PS D:\Users\fwang23> jstack 3624
2024-03-26 14:18:25
Full thread dump OpenJDK 64-Bit Server VM (25.265-b01 mixed mode):
"DestroyJavaVM" #21 prio=5 os_prio=0 tid=0x0000017170568800 nid=0x4978 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-1" #20 prio=5 os_prio=0 tid=0x0000017179461800 nid=0x6df0 waiting for monitor entry [0x0000000a8f7ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.example.demo.ThreadLock.lambda$main$1(ThreadLock.java:36)
- waiting to lock <0x0000000700ddf470> (a java.lang.String)
- locked <0x0000000700ddf4a8> (a java.lang.String)
at com.example.demo.ThreadLock$$Lambda$2/303563356.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Thread-0" #19 prio=5 os_prio=0 tid=0x0000017179460000 nid=0x616c waiting for monitor entry [0x0000000a8f6ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.example.demo.ThreadLock.lambda$main$0(ThreadLock.java:21)
- waiting to lock <0x0000000700ddf4a8> (a java.lang.String)
- locked <0x0000000700ddf470> (a java.lang.String)
at com.example.demo.ThreadLock$$Lambda$1/531885035.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #18 daemon prio=9 os_prio=0 tid=0x0000017178f5a000 nid=0x4680 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread11" #17 daemon prio=9 os_prio=2 tid=0x0000017178ed1000 nid=0x3be8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread10" #16 daemon prio=9 os_prio=2 tid=0x0000017178ed4000 nid=0x73e8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread9" #15 daemon prio=9 os_prio=2 tid=0x0000017178ed3000 nid=0x6e24 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread8" #14 daemon prio=9 os_prio=2 tid=0x0000017178ed2800 nid=0x6a2c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread7" #13 daemon prio=9 os_prio=2 tid=0x0000017178ecd800 nid=0xa94 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread6" #12 daemon prio=9 os_prio=2 tid=0x0000017178ed5000 nid=0x732c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread5" #11 daemon prio=9 os_prio=2 tid=0x0000017178ed1800 nid=0x6f68 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread4" #10 daemon prio=9 os_prio=2 tid=0x0000017178e9e800 nid=0x69c0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread3" #9 daemon prio=9 os_prio=2 tid=0x0000017178e8c000 nid=0x67c0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x0000017178e87000 nid=0x65d4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x0000017178e20800 nid=0x71f8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x0000017178e85000 nid=0x2170 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000017178e71800 nid=0x723c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000017178e70800 nid=0x8c8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000017176101000 nid=0x7100 in Object.wait() [0x0000000a8e5ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000700d08ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x0000000700d08ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000017178e00800 nid=0x281c in Object.wait() [0x0000000a8e4ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000700d06c00> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000700d06c00> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x00000171760e8800 nid=0xf48 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000001717057e000 nid=0x3c78 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000001717057f800 nid=0x1528 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000017170581000 nid=0x2dac runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000017170583000 nid=0x6d64 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000017170585000 nid=0x4a80 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000017170586800 nid=0x5bf8 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x000001717058a000 nid=0x66f4 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x000001717058b000 nid=0xaa0 runnable
"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x000001717058c800 nid=0x7244 runnable
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x000001717058d800 nid=0x6018 runnable
"GC task thread#10 (ParallelGC)" os_prio=0 tid=0x000001717058f000 nid=0x2c4c runnable
"GC task thread#11 (ParallelGC)" os_prio=0 tid=0x0000017170593000 nid=0x265c runnable
"GC task thread#12 (ParallelGC)" os_prio=0 tid=0x0000017170594800 nid=0x7220 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x0000017178f5e000 nid=0x6ccc waiting on condition
JNI global references: 309
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x00000171760fe128 (object 0x0000000700ddf470, a java.lang.String),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x0000017176100a68 (object 0x0000000700ddf4a8, a java.lang.String),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.example.demo.ThreadLock.lambda$main$1(ThreadLock.java:36)
- waiting to lock <0x0000000700ddf470> (a java.lang.String)
- locked <0x0000000700ddf4a8> (a java.lang.String)
at com.example.demo.ThreadLock$$Lambda$2/303563356.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at com.example.demo.ThreadLock.lambda$main$0(ThreadLock.java:21)
- waiting to lock <0x0000000700ddf4a8> (a java.lang.String)
- locked <0x0000000700ddf470> (a java.lang.String)
at com.example.demo.ThreadLock$$Lambda$1/531885035.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
在线程堆栈中,可以看到发生了死锁。
- Thread-1 想获取 0x00000171760fe128 的对象监视器锁,但这个锁被 Thread-0 占用;
- Thread-0 想获取 0x0000017176100a68 的对象监视器锁,但这个锁被 Thread-1 占用;