标签:bit unit system tools orm monit sig hand native
测试:
package t1;
import java.util.concurrent.TimeUnit;
class MyThread implements Runnable {
private String lockA;
private String lockB;
public MyThread(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA) {
System.out.println(Thread.currentThread().getName() + ":持有" + lockA);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":抢夺" + lockB);
synchronized (lockB) {
}
}
}
}
public class DiedLock {
public static void main(String[] args) {
String lockA = "lockA";
String lockB = "lockB";
new Thread(new MyThread(lockA, lockB), "AA").start();
new Thread(new MyThread(lockB, lockA), "BB").start();
}
}
输出结果:
AA:持有lockA
BB:持有lockB
AA:抢夺lockB
BB:抢夺lockA
排查:
在所在工程中启cmd中jps -l 命令获取java 进程
D:\ms\workspace\ztest>jps -l
3764
9208 sun.tools.jps.Jps
9352 t1.DiedLock
在所在工程中启cmd中jstack 进程号 命令获取java 堆栈信息
D:\ms\workspace\ztest>jstack 9352
2020-04-04 06:22:01
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode):
"DestroyJavaVM" #11 prio=5 os_prio=0 tid=0x00000000009fe800 nid=0x2914 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"BB" #10 prio=5 os_prio=0 tid=0x000000001626e000 nid=0x26f4 waiting for monitor entry [0x0000000016abf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at t1.MyThread.run(DiedLock.java:24)
- waiting to lock <0x00000000eb64ce50> (a java.lang.String)
- locked <0x00000000eb64ce88> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"AA" #9 prio=5 os_prio=0 tid=0x000000001626d000 nid=0x1f24 waiting for monitor entry [0x00000000169bf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at t1.MyThread.run(DiedLock.java:24)
- waiting to lock <0x00000000eb64ce88> (a java.lang.String)
- locked <0x00000000eb64ce50> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00000000161d9800 nid=0x2870 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x00000000161d5800 nid=0xb14 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x0000000015119000 nid=0x2dd4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000015118000 nid=0x12d8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001516e800 nid=0x2654 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000150f8800 nid=0x438 in Object.wait() [0x0000000015e5e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000eb588ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000eb588ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:212)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000002d9e000 nid=0x191c in Object.wait() [0x0000000015d5f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000eb586bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000eb586bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x00000000150d5800 nid=0xee8 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002cc8000 nid=0x590 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002cc9800 nid=0x2fc0 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x0000000016208800 nid=0x2ea4 waiting on condition
JNI global references: 5
Found one Java-level deadlock:
=============================
"BB":
waiting to lock monitor 0x0000000002da5d78 (object 0x00000000eb64ce50, a java.lang.String),
which is held by "AA"
"AA":
waiting to lock monitor 0x0000000002da48d8 (object 0x00000000eb64ce88, a java.lang.String),
which is held by "BB"
Java stack information for the threads listed above:
===================================================
"BB":
at t1.MyThread.run(DiedLock.java:24)
- waiting to lock <0x00000000eb64ce50> (a java.lang.String)
- locked <0x00000000eb64ce88> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"AA":
at t1.MyThread.run(DiedLock.java:24)
- waiting to lock <0x00000000eb64ce88> (a java.lang.String)
- locked <0x00000000eb64ce50> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
标签:bit unit system tools orm monit sig hand native
原文地址:https://www.cnblogs.com/dengw125792/p/12630201.html