死锁就是多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放。
锁的竞争:
是否是同一把锁
是否锁住同一资源
package com.lchtest.juc;
/**
* 两个或两个以上的线程,因为竞争同一个资源而造成相互等待的现象
*/
public class DeadLock {
static Object a = new Object();
static Object b = new Object();
public static void main(String[] args) {
new Thread(() ->{
synchronized (a){
System.out.println(Thread.currentThread().getName() + "持有锁a,试图获取锁B");
synchronized (b){
System.out.println(Thread.currentThread().getName() + "获取锁B");
}
}
},"线程a").start();
new Thread(() ->{
synchronized (b){
System.out.println(Thread.currentThread().getName() + "持有锁b,试图获取锁a");
synchronized (a){
System.out.println(Thread.currentThread().getName() + "获取锁a");
}
}
},"线程b").start();
}
}
运行结果:
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:D:\programsinstall\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=65193:D:\programsinstall\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\Baidudownload\JUC\out\production\JUC" com.lchtest.juc.DeadLock
线程a持有锁a,试图获取锁B
线程b持有锁b,试图获取锁a
- 如何检测死锁
通过jps获取Java 进程号,然后通过jstack命令来查看堆栈状态:
C:\Users\pc>jstack 9088
2021-08-01 21:43:30
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):
"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x00000000037a3800 nid=0x3a54 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"线程b" #13 prio=5 os_prio=0 tid=0x000000001be2a000 nid=0xa0c waiting for monitor entry [0x000000001c78f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.lchtest.juc.DeadLock.lambda$main$1(DeadLock.java:28)
- waiting to lock <0x00000000d6310e00> (a java.lang.Object)
- locked <0x00000000d6310e10> (a java.lang.Object)
at com.lchtest.juc.DeadLock$$Lambda$2/1831932724.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"线程a" #12 prio=5 os_prio=0 tid=0x000000001be24800 nid=0x316c waiting for monitor entry [0x000000001c68f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.lchtest.juc.DeadLock.lambda$main$0(DeadLock.java:18)
- waiting to lock <0x00000000d6310e10> (a java.lang.Object)
- locked <0x00000000d6310e00> (a java.lang.Object)
at com.lchtest.juc.DeadLock$$Lambda$1/990368553.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001a21f000 nid=0x1b3c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001a17e800 nid=0x200c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001a17a800 nid=0x1de0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001a17a000 nid=0x33d4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001a175000 nid=0x3c08 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001a164000 nid=0x3ed0 runnable [0x000000001b78e000]
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 <0x00000000d63ab678> (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 <0x00000000d63ab678> (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=0x000000001a0d6000 nid=0x3e40 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001a129800 nid=0x23e4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000000389b800 nid=0xfa0 in Object.wait() [0x000000001b42f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6188ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000d6188ed0> (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=0x000000000389a800 nid=0x18ec in Object.wait() [0x000000001b32f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d6186bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000d6186bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x00000000189a6800 nid=0x372c runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000037b9000 nid=0x408 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000037ba800 nid=0x2194 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000037bc000 nid=0x2a58 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000037bd800 nid=0x3f9c runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000037c1000 nid=0x313c runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000037c2000 nid=0x2264 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000037c5000 nid=0x23dc runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000037c6800 nid=0x1298 runnable
"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x00000000037c7800 nid=0xbf0 runnable
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x00000000037c8800 nid=0x3cc8 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001a26c800 nid=0x2708 waiting on condition
JNI global references: 317
Found one Java-level deadlock:
=============================
"线程b":
waiting to lock monitor 0x00000000189b0c88 (object 0x00000000d6310e00, a java.lang.Object),
which is held by "线程a"
"线程a":
waiting to lock monitor 0x00000000189b3728 (object 0x00000000d6310e10, a java.lang.Object),
which is held by "线程b"
Java stack information for the threads listed above:
===================================================
"线程b":
at com.lchtest.juc.DeadLock.lambda$main$1(DeadLock.java:28)
- waiting to lock <0x00000000d6310e00> (a java.lang.Object)
- locked <0x00000000d6310e10> (a java.lang.Object)
at com.lchtest.juc.DeadLock$$Lambda$2/1831932724.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"??程a":
at com.lchtest.juc.DeadLock.lambda$main$0(DeadLock.java:18)
- waiting to lock <0x00000000d6310e10> (a java.lang.Object)
- locked <0x00000000d6310e00> (a java.lang.Object)
at com.lchtest.juc.DeadLock$$Lambda$1/990368553.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.