目录
一、什么是死锁
死锁是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力干涉那它们都将无法推进下去,如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则就会因争夺有限的资源而陷入死锁。
二、代码实现一个死锁/**
* 死锁发生,两个以上线程因争夺资源互相等待
* 持有自己的锁,还妄图获得别人的锁
*/
public class DeadLockDemo {
public static void main(String[] args) {
String LockA = "lockA";
String LockB = "lockB";
new Thread(new HoldLockThread(LockA, LockB), "t1").start();
new Thread(new HoldLockThread(LockB, LockA), "t2").start();
}
}
class HoldLockThread implements Runnable{
private String lockA;
private String lockB;
public HoldLockThread(String lockA, String lockB){
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA){
System.out.println(Thread.currentThread().getName() + "得到"+lockA+"锁尝试获得"+lockB+"锁");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB){
System.out.println(Thread.currentThread().getName() + "得到" +lockB);
}
}
}
}
三、如何判断是不是死锁
1.jps -l命令定位进程号
2.jstack找到死锁查看
E:\eclipseWorkspace\MySpringboot>jstack 9500
2021-04-23 16:07:15
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode):
"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000002b93800 nid=0x32d8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"t2" #12 prio=5 os_prio=0 tid=0x000000001a12f800 nid=0x4474 waiting for monitor entry [0x000000001b2ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.boot.security.server.myUtils.jiagou2021.locks.HoldLockThread.run(DeadLockDemo.java:45)
- waiting to lock <0x00000000d6327610> (a java.lang.String)
- locked <0x00000000d6327648> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"t1" #11 prio=5 os_prio=0 tid=0x000000001a290800 nid=0x43ec waiting for monitor entry [0x000000001b1ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.boot.security.server.myUtils.jiagou2021.locks.HoldLockThread.run(DeadLockDemo.java:45)
- waiting to lock <0x00000000d6327648> (a java.lang.String)
- locked <0x00000000d6327610> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x00000000198ed000 nid=0xaa0 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x00000000198ec000 nid=0x7d4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001987f800 nid=0x255c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000000019831000 nid=0x17dc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x00000000198fa000 nid=0x219c runnable [0x0000000019c2e000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x00000000d629f910> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x00000000d629f910> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000018d73800 nid=0x41c4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001799a800 nid=0x2954 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000017971800 nid=0x2d04 in Object.wait() [0x0000000018ccf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5d08ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000d5d08ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000017949800 nid=0x14cc in Object.wait() [0x0000000018bcf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5d06b68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000d5d06b68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x0000000017947800 nid=0x2f70 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002bb1000 nid=0x4a64 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002bb2800 nid=0x33cc runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002bb4800 nid=0x3bd4 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002bb6000 nid=0x4e7c runnable
"VM Periodic Task Thread" os_prio=2 tid=0x00000000198ee000 nid=0x20e8 waiting on condition
JNI global references: 33
Found one Java-level deadlock:
=============================
"t2":
waiting to lock monitor 0x00000000179516b8 (object 0x00000000d6327610, a java.lang.String),
which is held by "t1"
"t1":
waiting to lock monitor 0x0000000017950218 (object 0x00000000d6327648, a java.lang.String),
which is held by "t2"
Java stack information for the threads listed above:
===================================================
"t2":
at com.boot.security.server.myUtils.jiagou2021.locks.HoldLockThread.run(DeadLockDemo.java:45)
- waiting to lock <0x00000000d6327610> (a java.lang.String)
- locked <0x00000000d6327648> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"t1":
at com.boot.security.server.myUtils.jiagou2021.locks.HoldLockThread.run(DeadLockDemo.java:45)
- waiting to lock <0x00000000d6327648> (a java.lang.String)
- locked <0x00000000d6327610> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.