package com.codeiteasy.config;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Parameter;
import java.util.concurrent.TimeUnit;
public class ThreadTest2 {
private static Thread t1, t2;
private static final Object obj1 = new Object();
private static final Object obj2 = new Object();
public static void read() {
synchronized (obj1) {
System.out.println("获取到读锁" + Thread.currentThread().getName());
synchronized (obj2) {
System.out.println("获取到写锁" + Thread.currentThread().getName());
System.out.println("success read;");
}
System.out.println("释放写锁" + Thread.currentThread().getName());
}
System.out.println("释放读锁" + Thread.currentThread().getName());
}
public static void write() {
synchronized (obj2) {
System.out.println("获取到写锁" + Thread.currentThread().getName());
synchronized (obj1) {
System.out.println("获取到读锁" + Thread.currentThread().getName());
System.out.println("success write.");
}
System.out.println("释放读锁" + Thread.currentThread().getName());
}
System.out.println("释放写锁" + Thread.currentThread().getName());
}
public static void main(String[] args) {
System.out.println("进程号:" + getPid());
Thread t1 = new Thread(() -> {
while (true) {
read();
}
});
t1.setName("[t1]");
Thread t2 = new Thread(() -> {
while (true) {
write();
}
});
t2.setName("[t2]");
t1.start();
t2.start();
}
private static String getPid() {
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
String name = mxBean.getName();
name = name.split("@")[0];
return name;
}
}
t1线程和t2线程互相持有锁,互相等待对方释放锁,出现了死锁问题。
可以用jstack
命令去检测
jstack [进程号]
输出结果:
Microsoft Windows [版本 10.0.22621.2283]
(c) Microsoft Corporation。保留所有权利。
E:\Coding\java\gradeSys>jstack 35932
2023-09-18 11:21:21
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.291-b10 mixed mode):
"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x000001973488d000 nid=0x721c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"[t2]" #13 prio=5 os_prio=0 tid=0x000001970b709800 nid=0x7b90 waiting for monitor entry [0x00000007789ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.codeiteasy.config.ThreadTest2.write(ThreadTest2.java:32)
- waiting to lock <0x00000197d97af500> (a java.lang.Object)
- locked <0x00000197d97af510> (a java.lang.Object)
at com.codeiteasy.config.ThreadTest2.lambda$main$1(ThreadTest2.java:50)
at com.codeiteasy.config.ThreadTest2$$Lambda$2/772777427.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"[t1]" #12 prio=5 os_prio=0 tid=0x000001970b707000 nid=0x83e4 waiting for monitor entry [0x00000007788ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.codeiteasy.config.ThreadTest2.read(ThreadTest2.java:20)
- waiting to lock <0x00000197d97af510> (a java.lang.Object)
- locked <0x00000197d97af500> (a java.lang.Object)
at com.codeiteasy.config.ThreadTest2.lambda$main$0(ThreadTest2.java:44)
at com.codeiteasy.config.ThreadTest2$$Lambda$1/492228202.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000001970b395000 nid=0x7b6c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000001970b36f000 nid=0x6e34 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000001970b36d800 nid=0x8b2c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000001970b369800 nid=0x2034 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000001970b369000 nid=0x507c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000001970b365000 nid=0x8b28 runnable [0x00000007781ff000]
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 <0x00000197d9808258> (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 <0x00000197d9808258> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000001970b0bb000 nid=0x8fe8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000001970b0ba000 nid=0x7eb8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000019708c64000 nid=0x8bf8 in Object.wait() [0x0000000777eff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000197d9588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000197d9588ee0> (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=0x0000019708c5d800 nid=0x8cf8 in Object.wait() [0x0000000777dfe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000197d9586c00> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000197d9586c00> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x0000019708c30800 nid=0x8218 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000197348a5800 nid=0x8b70 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000197348a7000 nid=0x8d68 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000197348a8800 nid=0x8814 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000197348a9800 nid=0x89cc runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000197348ac800 nid=0x8f18 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000197348ad800 nid=0x8374 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000197348b0800 nid=0x71c8 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000197348b1800 nid=0x8ba4 runnable
"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x00000197348b2800 nid=0x8dac runnable
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x00000197348b3800 nid=0x84f8 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000001970b4a1800 nid=0x8128 waiting on condition
JNI global references: 317
Found one Java-level deadlock:
=============================
"[t2]":
waiting to lock monitor 0x0000019708c612d8 (object 0x00000197d97af500, a java.lang.Object),
which is held by "[t1]"
"[t1]":
waiting to lock monitor 0x0000019708c62828 (object 0x00000197d97af510, a java.lang.Object),
which is held by "[t2]"
Java stack information for the threads listed above:
===================================================
"[t2]":
at com.codeiteasy.config.ThreadTest2.write(ThreadTest2.java:32)
- waiting to lock <0x00000197d97af500> (a java.lang.Object)
- locked <0x00000197d97af510> (a java.lang.Object)
at com.codeiteasy.config.ThreadTest2.lambda$main$1(ThreadTest2.java:50)
at com.codeiteasy.config.ThreadTest2$$Lambda$2/772777427.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"[t1]":
at com.codeiteasy.config.ThreadTest2.read(ThreadTest2.java:20)
- waiting to lock <0x00000197d97af510> (a java.lang.Object)
- locked <0x00000197d97af500> (a java.lang.Object)
at com.codeiteasy.config.ThreadTest2.lambda$main$0(ThreadTest2.java:44)
at com.codeiteasy.config.ThreadTest2$$Lambda$1/492228202.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.