0%

java | 线程死锁 「栈」

java 的死锁检查。

代码如下

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
package com.redisc;

import lombok.extern.slf4j.Slf4j;

class A {
};

class B {
};

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

static A a = new A();
static B b = new B();

public static void main(String[] args) throws Exception {

new Thread(() -> {
synchronized (a) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (b) {

}
}
}).start();

Thread.sleep(1000);

new Thread(() -> {
synchronized (b) {
synchronized (a) {

}
}
}).start();


}

}

打包成 jar 包。然后运行。

使用 pa -aux | grep java 查看

34341   0.0  0.5  8139576  42724 s000  SN   11:04PM   0:00.41 /usr/bin/java -jar redisc-1.0-SNAPSHOT-jar-with-dependencies.jar

使用 jstack 34341 输出

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
2022-12-17 23:05:22
Full thread dump OpenJDK 64-Bit Server VM (14.0.1+7 mixed mode, sharing):

Threads class SMR info:
_java_thread_list=0x00007f958e5418c0, length=13, elements={
0x00007f958f866000, 0x00007f959080a000, 0x00007f958f868000, 0x00007f958f868800,
0x00007f958f86c000, 0x00007f958f86d000, 0x00007f958f009000, 0x00007f958e811800,
0x00007f958e812800, 0x00007f958e890000, 0x00007f958f00d800, 0x00007f958f010800,
0x00007f958f00e800
}

"Reference Handler" #2 daemon prio=10 os_prio=31 cpu=0.09ms elapsed=22.92s tid=0x00007f958f866000 nid=0x4603 waiting on condition [0x000070000c78f000]
java.lang.Thread.State: RUNNABLE
at java.lang.ref.Reference.waitForReferencePendingList(java.base@14.0.1/Native Method)
at java.lang.ref.Reference.processPendingReferences(java.base@14.0.1/Reference.java:241)
at java.lang.ref.Reference$ReferenceHandler.run(java.base@14.0.1/Reference.java:213)

"Finalizer" #3 daemon prio=8 os_prio=31 cpu=0.20ms elapsed=22.92s tid=0x00007f959080a000 nid=0x4503 in Object.wait() [0x000070000c892000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(java.base@14.0.1/Native Method)
- waiting on <0x0000000787f027f0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(java.base@14.0.1/ReferenceQueue.java:155)
- locked <0x0000000787f027f0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(java.base@14.0.1/ReferenceQueue.java:176)
at java.lang.ref.Finalizer$FinalizerThread.run(java.base@14.0.1/Finalizer.java:170)

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 cpu=0.31ms elapsed=22.91s tid=0x00007f958f868000 nid=0x5703 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Service Thread" #5 daemon prio=9 os_prio=31 cpu=0.05ms elapsed=22.91s tid=0x00007f958f868800 nid=0x5903 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 cpu=103.43ms elapsed=22.91s tid=0x00007f958f86c000 nid=0xa603 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
No compile task

"C1 CompilerThread0" #9 daemon prio=9 os_prio=31 cpu=79.09ms elapsed=22.91s tid=0x00007f958f86d000 nid=0xa403 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
No compile task

"Sweeper thread" #10 daemon prio=9 os_prio=31 cpu=1.93ms elapsed=22.91s tid=0x00007f958f009000 nid=0x5d03 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Notification Thread" #11 daemon prio=9 os_prio=31 cpu=0.13ms elapsed=22.89s tid=0x00007f958e811800 nid=0xa103 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Common-Cleaner" #12 daemon prio=8 os_prio=31 cpu=0.22ms elapsed=22.89s tid=0x00007f958e812800 nid=0x5e03 in Object.wait() [0x000070000d1b0000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(java.base@14.0.1/Native Method)
- waiting on <0x0000000787f45a60> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(java.base@14.0.1/ReferenceQueue.java:155)
- locked <0x0000000787f45a60> (a java.lang.ref.ReferenceQueue$Lock)
at jdk.internal.ref.CleanerImpl.run(java.base@14.0.1/CleanerImpl.java:148)
at java.lang.Thread.run(java.base@14.0.1/Thread.java:832)
at jdk.internal.misc.InnocuousThread.run(java.base@14.0.1/InnocuousThread.java:134)

"Thread-0" #13 prio=5 os_prio=31 cpu=1.13ms elapsed=22.73s tid=0x00007f958e890000 nid=0x5f03 waiting for monitor entry [0x000070000d2b3000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.redisc.Run.lambda$main$0(Run.java:28)
- waiting to lock <0x0000000787b0f1d0> (a com.redisc.B)
- locked <0x0000000787b0de18> (a com.redisc.A)
at com.redisc.Run$$Lambda$21/0x0000000800b7b440.run(Unknown Source)
at java.lang.Thread.run(java.base@14.0.1/Thread.java:832)

"Thread-1" #14 prio=5 os_prio=31 cpu=1.46ms elapsed=21.72s tid=0x00007f958f00d800 nid=0x9903 waiting for monitor entry [0x000070000d3b6000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.redisc.Run.lambda$main$1(Run.java:38)
- waiting to lock <0x0000000787b0de18> (a com.redisc.A)
- locked <0x0000000787b0f1d0> (a com.redisc.B)
at com.redisc.Run$$Lambda$22/0x0000000800b7b840.run(Unknown Source)
at java.lang.Thread.run(java.base@14.0.1/Thread.java:832)

"DestroyJavaVM" #15 prio=5 os_prio=31 cpu=162.42ms elapsed=21.72s tid=0x00007f958f010800 nid=0x2703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Attach Listener" #16 daemon prio=9 os_prio=31 cpu=0.62ms elapsed=0.10s tid=0x00007f958f00e800 nid=0x9803 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"VM Thread" os_prio=31 cpu=2.58ms elapsed=22.93s tid=0x00007f9590876800 nid=0x4903 runnable

"GC Thread#0" os_prio=31 cpu=1.10ms elapsed=22.96s tid=0x00007f958f831800 nid=0x3303 runnable

"G1 Main Marker" os_prio=31 cpu=0.04ms elapsed=22.96s tid=0x00007f958f842800 nid=0x4f03 runnable

"G1 Conc#0" os_prio=31 cpu=0.04ms elapsed=22.96s tid=0x00007f958f843800 nid=0x3503 runnable

"G1 Refine#0" os_prio=31 cpu=0.04ms elapsed=22.96s tid=0x00007f959086c000 nid=0x3603 runnable

"G1 Young RemSet Sampling" os_prio=31 cpu=3.16ms elapsed=22.96s tid=0x00007f959086c800 nid=0x4a03 runnable
"VM Periodic Task Thread" os_prio=31 cpu=16.60ms elapsed=22.89s tid=0x00007f959087f800 nid=0x9f03 waiting on condition

JNI global refs: 9, weak refs: 0


Found one Java-level deadlock:
=============================
"Thread-0":
waiting to lock monitor 0x00007f9590139f00 (object 0x0000000787b0f1d0, a com.redisc.B),
which is held by "Thread-1"

"Thread-1":
waiting to lock monitor 0x00007f9590139e00 (object 0x0000000787b0de18, a com.redisc.A),
which is held by "Thread-0"

Java stack information for the threads listed above:
===================================================
"Thread-0":
at com.redisc.Run.lambda$main$0(Run.java:28)
- waiting to lock <0x0000000787b0f1d0> (a com.redisc.B)
- locked <0x0000000787b0de18> (a com.redisc.A)
at com.redisc.Run$$Lambda$21/0x0000000800b7b440.run(Unknown Source)
at java.lang.Thread.run(java.base@14.0.1/Thread.java:832)
"Thread-1":
at com.redisc.Run.lambda$main$1(Run.java:38)
- waiting to lock <0x0000000787b0de18> (a com.redisc.A)
- locked <0x0000000787b0f1d0> (a com.redisc.B)
at com.redisc.Run$$Lambda$22/0x0000000800b7b840.run(Unknown Source)
at java.lang.Thread.run(java.base@14.0.1/Thread.java:832)

Found 1 deadlock.

后面有个输出 Found one Java-level deadlock 有一个 deadlock 就证明死锁了。后面就是死锁的位置。

at com.redisc.Run.lambda$main$0(Run.java:28)
at com.redisc.Run.lambda$main$1(Run.java:38)
请我喝杯咖啡吧~