线程dump分析并发死锁

死锁代码:

/**
 * 死锁例子
 *
 */
public class DeadLockDemo {

    /**
     * A锁
     */
    private static String A = "A";
    /**
     * B锁
     */
    private static String B = "B";

    public static void main(String[] args) {
        new DeadLockDemo().deadLock();
    }

    private void deadLock() {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (A) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (B) {
                        System.out.println("1");
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (B) {
                    synchronized (A) {
                        System.out.println("2");
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }

}

运行上述代码,然后在cmd窗口dump线程
先查看进程对应的pid,通过 ### jps -l

C:\Users\Lanxumit>jps -l
11576 com.example.opencv.lib.designpattern.DeadLockDemo
12184 sun.tools.jps.Jps
10124 org.gradle.launcher.daemon.bootstrap.GradleDaemon
2572

可以看到11576 com.example.opencv.lib.designpattern.DeadLockDemo
接着dump对应的进程

C:\Users\Lanxumit>jstack 11576 > dump11576

接着查看dump11576的文件内容:

2020-07-01 14:39:32
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode):

"DestroyJavaVM" #12 prio=5 os_prio=0 tid=0x00000000023f8000 nid=0x1804 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" #11 prio=5 os_prio=0 tid=0x000000001b333000 nid=0x2dfc waiting for monitor entry [0x000000001c43e000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.example.opencv.lib.designpattern.DeadLockDemo$2.run(DeadLockDemo.java:46)
    - waiting to lock <0x00000007818399c0> (a java.lang.String)
    - locked <0x00000007818399f0> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:745)

"Thread-0" #10 prio=5 os_prio=0 tid=0x000000001b332000 nid=0x2f68 waiting for monitor entry [0x000000001c0ff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.example.opencv.lib.designpattern.DeadLockDemo$1.run(DeadLockDemo.java:35)
    - waiting to lock <0x00000007818399f0> (a java.lang.String)
    - locked <0x00000007818399c0> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:745)

"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x000000001b309800 nid=0x2538 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000001b285000 nid=0x2ca4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

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

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001b269000 nid=0xf74 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000019ffd800 nid=0x2c8c in Object.wait() [0x000000001b10e000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000781788e98> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
    - locked <0x0000000781788e98> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000019fb6800 nid=0x2b7c in Object.wait() [0x000000001316e000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000781786b40> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:502)
    at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
    - locked <0x0000000781786b40> (a java.lang.ref.Reference$Lock)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x0000000019fab000 nid=0x1a38 runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000000000240d000 nid=0x2c94 runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000240e800 nid=0x26dc runnable 

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002410000 nid=0x2e20 runnable 

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002411800 nid=0x2c44 runnable 

"VM Periodic Task Thread" os_prio=2 tid=0x000000001b2b3000 nid=0x1f24 waiting on condition 

JNI global references: 6


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x0000000019fbba28 (object 0x00000007818399c0, a java.lang.String),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x0000000019fbce18 (object 0x00000007818399f0, a java.lang.String),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
    at com.example.opencv.lib.designpattern.DeadLockDemo$2.run(DeadLockDemo.java:46)
    - waiting to lock <0x00000007818399c0> (a java.lang.String)
    - locked <0x00000007818399f0> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:745)
"Thread-0":
    at com.example.opencv.lib.designpattern.DeadLockDemo$1.run(DeadLockDemo.java:35)
    - waiting to lock <0x00000007818399f0> (a java.lang.String)
    - locked <0x00000007818399c0> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

你可能感兴趣的:(线程dump分析并发死锁)