使用jstack检查死锁
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class MyDeadThread { public static Object o1 = new Object(); public static Object o2 = new Object(); public static void main(String[] args) { CyclicBarrier barrier = new CyclicBarrier(2); new Thread(()->{ synchronized (o1){ try { barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } // latch.countDown(); // try { // latch.await();//此处不能这样用,latch是阻塞主线程的。应该用阻塞子线程的。 // } catch (InterruptedException e) { // e.printStackTrace(); // } synchronized (o2){ System.out.println(Thread.currentThread().getName()); } } }).start(); new Thread(()->{ synchronized (o2){ try { barrier.await(); int numberWaiting = barrier.getNumberWaiting(); int parties = barrier.getParties(); System.out.println("numberWaiting="+numberWaiting+"如果num为0说明barrier设计生效了"); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } synchronized (o1){ System.out.println(Thread.currentThread().getName()); } } }).start(); } }
jstack查看
[root@master javatest]# jstack -l 2019 2020-10-24 11:50:03 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.221-b11 mixed mode): "Attach Listener" #11 daemon prio=9 os_prio=0 tid=0x00007fd3f0001000 nid=0x809 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "DestroyJavaVM" #10 prio=5 os_prio=0 tid=0x00007fd418009000 nid=0x7e4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Thread-1" #9 prio=5 os_prio=0 tid=0x00007fd4180f7800 nid=0x7ee waiting for monitor entry [0x00007fd3f59bb000] java.lang.Thread.State: BLOCKED (on object monitor) at MyDeadLock.lambda$main$1(MyDeadLock.java:43) - waiting to lock <0x00000000f0a5baa8> (a java.lang.Object) - locked <0x00000000f0a5bab8> (a java.lang.Object) at MyDeadLock$$Lambda$2/135721597.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) Locked ownable synchronizers: - None "Thread-0" #8 prio=5 os_prio=0 tid=0x00007fd4180f5800 nid=0x7ed waiting for monitor entry [0x00007fd3f5abc000] java.lang.Thread.State: BLOCKED (on object monitor) at MyDeadLock.lambda$main$0(MyDeadLock.java:26) - waiting to lock <0x00000000f0a5bab8> (a java.lang.Object) - locked <0x00000000f0a5baa8> (a java.lang.Object) at MyDeadLock$$Lambda$1/1418481495.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) Locked ownable synchronizers: - None "Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007fd4180b4000 nid=0x7eb runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fd4180b1000 nid=0x7ea waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fd4180af000 nid=0x7e9 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fd4180ad000 nid=0x7e8 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fd41807c000 nid=0x7e7 in Object.wait() [0x00007fd4086fd000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000f0a08ed8> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x00000000f0a08ed8> (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) Locked ownable synchronizers: - None "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fd418077800 nid=0x7e6 in Object.wait() [0x00007fd4087fe000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000000f0a06c00> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x00000000f0a06c00> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) Locked ownable synchronizers: - None "VM Thread" os_prio=0 tid=0x00007fd41806e000 nid=0x7e5 runnable "VM Periodic Task Thread" os_prio=0 tid=0x00007fd4180b7000 nid=0x7ec waiting on condition JNI global references: 309 Found one Java-level deadlock: ============================= "Thread-1": waiting to lock monitor 0x00007fd3fc004e28 (object 0x00000000f0a5baa8, a java.lang.Object), which is held by "Thread-0" "Thread-0": waiting to lock monitor 0x00007fd3fc0062c8 (object 0x00000000f0a5bab8, a java.lang.Object), which is held by "Thread-1" Java stack information for the threads listed above: =================================================== "Thread-1": at MyDeadLock.lambda$main$1(MyDeadLock.java:43) - waiting to lock <0x00000000f0a5baa8> (a java.lang.Object) - locked <0x00000000f0a5bab8> (a java.lang.Object) at MyDeadLock$$Lambda$2/135721597.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) "Thread-0": at MyDeadLock.lambda$main$0(MyDeadLock.java:26) - waiting to lock <0x00000000f0a5bab8> (a java.lang.Object) - locked <0x00000000f0a5baa8> (a java.lang.Object) at MyDeadLock$$Lambda$1/1418481495.run(Unknown Source) at java.lang.Thread.run(Thread.java:748) Found 1 deadlock.
参考:
https://docs.oracle.com/javase/8/docs/api/