0%

java | 问题检测 定位死锁

检查死锁可以使用 jconsole 工具,或者使用 jps 定位进程 id,再用 jstack 定位死锁。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.redisc;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Run")
public class Run {


public static void main(String[] args) throws Exception {
Object A = new Object();
Object B = new Object();

new Thread(() -> {
synchronized (A) {
log.debug("lock A");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (B) {
log.debug("lock B");
}
}
}).start();

new Thread(() -> {
synchronized (B) {
log.debug("lock B");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (A) {
log.debug("lock A");
}
}
}).start();
}

}

使用 jstack 来输出栈信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
2022-12-19 22:56:35
Full thread dump OpenJDK 64-Bit Server VM (25.242-b08 mixed mode):

"Attach Listener" #14 daemon prio=9 os_prio=31 tid=0x00007f9f8e9d5000 nid=0x5903 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"DestroyJavaVM" #13 prio=5 os_prio=31 tid=0x00007f9f93837000 nid=0xd03 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Thread-1" #12 prio=5 os_prio=31 tid=0x00007f9f93833800 nid=0xa803 waiting for monitor entry [0x0000700007c4b000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.redisc.Run.lambda$main$1(Run.java:36)
- waiting to lock <0x0000000795bf3de8> (a java.lang.Object)
- locked <0x0000000795bf3df8> (a java.lang.Object)
at com.redisc.Run$$Lambda$2/580024961.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

"Thread-0" #11 prio=5 os_prio=31 tid=0x00007f9f91066000 nid=0xa903 waiting for monitor entry [0x0000700007b48000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.redisc.Run.lambda$main$0(Run.java:22)
- waiting to lock <0x0000000795bf3df8> (a java.lang.Object)
- locked <0x0000000795bf3de8> (a java.lang.Object)
at com.redisc.Run$$Lambda$1/1567581361.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

"Service Thread" #10 daemon prio=9 os_prio=31 tid=0x00007f9f90020800 nid=0x3f03 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #9 daemon prio=9 os_prio=31 tid=0x00007f9f94826000 nid=0x4103 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #8 daemon prio=9 os_prio=31 tid=0x00007f9f94825800 nid=0x3d03 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #7 daemon prio=9 os_prio=31 tid=0x00007f9f91044000 nid=0x4303 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 tid=0x00007f9f94824800 nid=0x3b03 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #5 daemon prio=5 os_prio=31 tid=0x00007f9f9001e000 nid=0x4503 runnable [0x0000700007433000]
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 <0x0000000795719f78> (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 <0x0000000795719f78> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:61)

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007f9f8e81f800 nid=0x4703 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007f9f9380a800 nid=0x3003 in Object.wait() [0x0000700007127000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795588ee8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x0000000795588ee8> (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=31 tid=0x00007f9f8f026000 nid=0x4d03 in Object.wait() [0x0000700007024000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795586c08> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000795586c08> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=31 tid=0x00007f9f8f01f000 nid=0x2e03 runnable

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007f9f8f016000 nid=0x1f07 runnable

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007f9f8f016800 nid=0x1b03 runnable

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007f9f8f017000 nid=0x1c03 runnable

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007f9f8f017800 nid=0x2a03 runnable

"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007f9f8f018800 nid=0x2c03 runnable

"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007f9f8f019000 nid=0x5303 runnable

"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007f9f8f019800 nid=0x5203 runnable

"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007f9f8f01a000 nid=0x5003 runnable

"VM Periodic Task Thread" os_prio=31 tid=0x00007f9f93815800 nid=0x5503 waiting on condition

JNI global references: 319


Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x00007f9f8e8100d8 (object 0x0000000795bf3de8, a java.lang.Object),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x00007f9f9109ca88 (object 0x0000000795bf3df8, a java.lang.Object),
which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.redisc.Run.lambda$main$1(Run.java:36)
- waiting to lock <0x0000000795bf3de8> (a java.lang.Object)
- locked <0x0000000795bf3df8> (a java.lang.Object)
at com.redisc.Run$$Lambda$2/580024961.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at com.redisc.Run.lambda$main$0(Run.java:22)
- waiting to lock <0x0000000795bf3df8> (a java.lang.Object)
- locked <0x0000000795bf3de8> (a java.lang.Object)
at com.redisc.Run$$Lambda$1/1567581361.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

发现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"Thread-1" #12 prio=5 os_prio=31 tid=0x00007f9f93833800 nid=0xa803 waiting for monitor entry [0x0000700007c4b000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.redisc.Run.lambda$main$1(Run.java:36)
- waiting to lock <0x0000000795bf3de8> (a java.lang.Object)
- locked <0x0000000795bf3df8> (a java.lang.Object)
at com.redisc.Run$$Lambda$2/580024961.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

"Thread-0" #11 prio=5 os_prio=31 tid=0x00007f9f91066000 nid=0xa903 waiting for monitor entry [0x0000700007b48000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.redisc.Run.lambda$main$0(Run.java:22)
- waiting to lock <0x0000000795bf3df8> (a java.lang.Object)
- locked <0x0000000795bf3de8> (a java.lang.Object)
at com.redisc.Run$$Lambda$1/1567581361.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

得以定位到相关位置。

请我喝杯咖啡吧~