死锁编码及定位分析
程序员文章站
2022-04-17 22:38:41
...
产生死锁的原因
死锁是指两个或两个以上的线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力干涉那它们都无法推进下去,如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则就会因争夺有限的资源而陷入自锁。
代码示例
public class DeadLockDemo {
public static void main(String[] args) {
new Thread(new HoldLockThread("lockA","lockB"),"AAA").start();
new Thread(new HoldLockThread("lockB","lockA"),"BBB").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() + "\t 自己持有:"+lockA+"\t 尝试获取:"+lockB);
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB){
System.out.println(Thread.currentThread().getName() + "\t 自己持有:"+lockB+"\t 尝试获取:"+lockA);
}
}
}
}
运行结果
问题定位
jdk bin目录下有jps.jar、jstack.jar
linux下查看 进程命令 ps -ef | grep xxx
windows 下的java运行程序也有类似ps的查看进程的命令 jps
- jps -l 定位进程号
(c) 2018 Microsoft Corporation。保留所有权利。
H:\interview_code>jps -l
2976 org.jetbrains.jps.cmdline.Launcher
8432 sun.tools.jps.Jps
13228
18604 com.wu.pratice.lockDemo.DeadLockDemo
2956
H:\interview_code>
- jstack xxx(进程号) Java的异常堆栈信息
H:\interview_code>jstack 18604
2019-06-06 13:58:37
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):
"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x000000000293e000 nid=0x1f0c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"BBB" #12 prio=5 os_prio=0 tid=0x000000001db6d800 nid=0x18d4 waiting for monitor entry [0x000000001e83f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.wu.pratice.lockDemo.HoldLockThread.run(DeadLockDemo.java:36)
- waiting to lock <0x000000076b319338> (a java.lang.String)
- locked <0x000000076b319370> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
"AAA" #11 prio=5 os_prio=0 tid=0x000000001db6a800 nid=0x34e4 waiting for monitor entry [0x000000001e73f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.wu.pratice.lockDemo.HoldLockThread.run(DeadLockDemo.java:36)
- waiting to lock <0x000000076b319370> (a java.lang.String)
- locked <0x000000076b319338> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001daed000 nid=0x2b48 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001dae7000 nid=0x234c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001da85000 nid=0x16a4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001da80800 nid=0x30c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001da64800 nid=0x4570 runnable [0x000000001e12e000]
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 <0x000000076b452250> (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 <0x000000076b452250> (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=0x000000001c67f000 nid=0x26cc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001da38800 nid=0x1708 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c65a800 nid=0x4730 in Object.wait() [0x000000001d9cf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b188ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x000000076b188ec8> (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=0x0000000003050800 nid=0x34c8 in Object.wait() [0x000000001d8cf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b186b68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x000000076b186b68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x000000001c638800 nid=0xbdc runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002f78000 nid=0x13c4 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002f79800 nid=0x3e80 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002f7b000 nid=0x1e6c runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002f7c800 nid=0xea0 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001db40800 nid=0x40a0 waiting on condition
JNI global references: 33
Found one Java-level deadlock:
=============================
"BBB":
waiting to lock monitor 0x000000000305b428 (object 0x000000076b319338, a java.lang.String),
which is held by "AAA"
"AAA":
waiting to lock monitor 0x0000000003058ae8 (object 0x000000076b319370, a java.lang.String),
which is held by "BBB"
Java stack information for the threads listed above:
===================================================
"BBB":
at com.wu.pratice.lockDemo.HoldLockThread.run(DeadLockDemo.java:36)
- waiting to lock <0x000000076b319338> (a java.lang.String)
- locked <0x000000076b319370> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
"AAA":
at com.wu.pratice.lockDemo.HoldLockThread.run(DeadLockDemo.java:36)
- waiting to lock <0x000000076b319370> (a java.lang.String)
- locked <0x000000076b319338> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
上一篇: 历史揭秘:古代驸马能不能纳妾?
下一篇: 毕諴的人物生平简介,毕諴是什么官?