使用 jstack或者jconsole查看死锁状态

1,有时候运行程序时会出现代码死锁,可以通过jstack进行查看,

 1.1 死锁代码如下:


import lombok.extern.slf4j.Slf4j;

import static cn.itcast.n2.util.Sleeper.sleep;

@Slf4j(topic = "c.TestDeadLock")
public class TestDeadLock {
    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (A) {
                log.debug("lock A");
                sleep(1);
                synchronized (B) {
                    log.debug("lock B");
                    log.debug("操作...");
                }
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            synchronized (B) {
                log.debug("lock B");
                sleep(0.5);
                synchronized (A) {
                    log.debug("lock A");
                    log.debug("操作...");
                }
            }
        }, "t2");
        t1.start();
        t2.start();
    }
}

1.2 ,通过jstack查看堆栈信息,通过deadlock关键字查询:


D:\bigdata\java_concurrent\juc_itcast>jstack 31540
2020-06-24 10:07:25
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):

"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x00000000031d3800 nid=0x796c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"t2" #13 prio=5 os_prio=0 tid=0x000000001ff24000 nid=0x8c1c waiting for monitor entry [0x000000002044f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at cn.itcast.n4.deadlock.TestDeadLock.lambda$test1$1(TestDeadLock.java:32)
        - waiting to lock <0x000000076c6001b8> (a java.lang.Object)
        - locked <0x000000076c6001c8> (a java.lang.Object)
        at cn.itcast.n4.deadlock.TestDeadLock$$Lambda$2/1321640594.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

"t1" #12 prio=5 os_prio=0 tid=0x000000001ff21000 nid=0x7318 waiting for monitor entry [0x000000002034f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at cn.itcast.n4.deadlock.TestDeadLock.lambda$test1$0(TestDeadLock.java:21)
        - waiting to lock <0x000000076c6001c8> (a java.lang.Object)
        - locked <0x000000076c6001b8> (a java.lang.Object)
        at cn.itcast.n4.deadlock.TestDeadLock$$Lambda$1/381707837.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

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

"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001ed13000 nid=0x7d4c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001ed00000 nid=0x6f58 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

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

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001ecba000 nid=0x8494 runnable [0x000000001f39e000]
   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 <0x000000076ba4b800> (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 <0x000000076ba4b800> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)

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

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

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001eb60800 nid=0x3d74 in Object.wait() [0x000000001f03f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076b788ec8> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x000000076b788ec8> (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=0x00000000032ca000 nid=0x67e8 in Object.wait() [0x000000001eb3e000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076b786b68> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x000000076b786b68> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x000000001cc79800 nid=0x1b0c runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000031e9000 nid=0x8828 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000031ea800 nid=0x598 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000031ec000 nid=0x5b24 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000031ee800 nid=0x59b4 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000031f1000 nid=0x6008 runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000031f2000 nid=0x898c runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000031f5800 nid=0x5e90 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000031f6800 nid=0x8380 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001ee0c800 nid=0x70c8 waiting on condition

JNI global references: 334


Found one Java-level deadlock:
=============================
"t2":
  waiting to lock monitor 0x000000001cc835c8 (object 0x000000076c6001b8, a java.lang.Object),
  which is held by "t1"
"t1":
  waiting to lock monitor 0x000000001cc81368 (object 0x000000076c6001c8, a java.lang.Object),
  which is held by "t2"

Java stack information for the threads listed above:
===================================================
"t2":
        at cn.itcast.n4.deadlock.TestDeadLock.lambda$test1$1(TestDeadLock.java:32)
        - waiting to lock <0x000000076c6001b8> (a java.lang.Object)
        - locked <0x000000076c6001c8> (a java.lang.Object)
        at cn.itcast.n4.deadlock.TestDeadLock$$Lambda$2/1321640594.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
"t1":
        at cn.itcast.n4.deadlock.TestDeadLock.lambda$test1$0(TestDeadLock.java:21)
        - waiting to lock <0x000000076c6001c8> (a java.lang.Object)
        - locked <0x000000076c6001b8> (a java.lang.Object)
        at cn.itcast.n4.deadlock.TestDeadLock$$Lambda$1/381707837.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

最后一行,可以很快速判断出来出现死锁的位置

使用 jstack或者jconsole查看死锁状态_第1张图片

 

2.使用jconsole查看死锁线程信息:

使用 jstack或者jconsole查看死锁状态_第2张图片

你可能感兴趣的:(java)