多线程之死锁

1.背景

面试官问:什么情况下回产生死锁?如何定位?

2.代码

package com.ldp.demo01;

import com.common.MyThreadUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 02/01 6:09
 * @description <p>
 * 监控工具的使用:
 * https://www.cnblogs.com/newAndHui/p/11105956.html
 * </p>
 */
@Slf4j
public class Test09DeadLock {
    /**
     * 死锁测试与定位
     *
     * @param args
     */
    public static void main(String[] args) {
        Object lockA = new Object();
        Object lockB = new Object();
        Thread tA = new Thread(() -> {
            synchronized (lockA) {
                log.info("获得锁 lockA");
                // 1秒后取获取锁 lockB
                MyThreadUtil.sleep(1);
                synchronized (lockB) {
                    log.info("获得锁 lockB");
                }
            }
        }, "tA");

        Thread tB = new Thread(() -> {
            synchronized (lockB) {
                log.info("获得锁 lockB");
                // 2秒后取获取锁 lockA
                MyThreadUtil.sleep(2);
                synchronized (lockA) {
                    log.info("获得锁 lockA");
                }
            }
        }, "tB");

        // 启动线程
        tA.start();
        tB.start();
    }
}

3.死锁定位

3.1.控制台命令定位

1. jps 查看进程号

E:\workroom4\thread-demo> jps
117044 Jps
96804 Launcher
106872 Test09DeadLock
111560

2.jstack 进程号 查看明细

E:\workroom4\thread-demo>jstack 106872
2022-02-01 18:16:38
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.152-b16 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x00000000033d3800 nid=0x1a300 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"tB" #12 prio=5 os_prio=0 tid=0x000000000b2a5000 nid=0xf4d8 waiting for monitor entry [0x000000000ba4f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.ldp.demo01.Test09DeadLock.lambda$main$1(Test09DeadLock.java:40)
        - waiting to lock <0x00000000d6a346e0> (a java.lang.Object)
        - locked <0x00000000d6a346f0> (a java.lang.Object)
        at com.ldp.demo01.Test09DeadLock$$Lambda$2/2030562336.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

"tA" #11 prio=5 os_prio=0 tid=0x000000000b2a4000 nid=0x187f0 waiting for monitor entry [0x000000000b14e000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.ldp.demo01.Test09DeadLock.lambda$main$0(Test09DeadLock.java:29)
        - waiting to lock <0x00000000d6a346f0> (a java.lang.Object)
        - locked <0x00000000d6a346e0> (a java.lang.Object)
        at com.ldp.demo01.Test09DeadLock$$Lambda$1/984849465.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

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

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

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

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

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000000a410800 nid=0x1b3d0 runnable [0x000000000aa9e000]
   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 <0x00000000d5f354c0> (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 <0x00000000d5f354c0> (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=0x0000000009040000 nid=0x18fe4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000034ce000 nid=0x1ceec in Object.wait() [0x000000000a39f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5d08ec8> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x00000000d5d08ec8> (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=0x00000000034c3000 nid=0x14214 in Object.wait() [0x000000000a29f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d5d06b68> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x00000000d5d06b68> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x0000000008ff7000 nid=0x88e0 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000033e9000 nid=0x1c0c runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000033ea800 nid=0x162fc runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000033ec000 nid=0x18df4 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000033ed800 nid=0x8fcc runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000000a54a000 nid=0x1bf58 waiting on condition

JNI global references: 327


Found one Java-level deadlock:
=============================
"tB":
  waiting to lock monitor 0x00000000034cb938 (object 0x00000000d6a346e0, a java.lang.Object),
  which is held by "tA"
"tA":
  waiting to lock monitor 0x00000000034c9af8 (object 0x00000000d6a346f0, a java.lang.Object),
  which is held by "tB"

Java stack information for the threads listed above:
===================================================
"tB":
        at com.ldp.demo01.Test09DeadLock.lambda$main$1(Test09DeadLock.java:40)
        - waiting to lock <0x00000000d6a346e0> (a java.lang.Object)
        - locked <0x00000000d6a346f0> (a java.lang.Object)
        at com.ldp.demo01.Test09DeadLock$$Lambda$2/2030562336.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
"tA":
        at com.ldp.demo01.Test09DeadLock.lambda$main$0(Test09DeadLock.java:29)
        - waiting to lock <0x00000000d6a346f0> (a java.lang.Object)
        - locked <0x00000000d6a346e0> (a java.lang.Object)
        at com.ldp.demo01.Test09DeadLock$$Lambda$1/984849465.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

 

重点是这里:

Found one Java-level deadlock:
=============================
"tB":
  waiting to lock monitor 0x00000000034cb938 (object 0x00000000d6a346e0, a java.lang.Object),
  which is held by "tA"
"tA":
  waiting to lock monitor 0x00000000034c9af8 (object 0x00000000d6a346f0, a java.lang.Object),
  which is held by "tB"

3.2.jconsole工具定位

多线程之死锁

 

 下一步:

多线程之死锁

 

 jconsole工具的使用之前我们在将jvm的时候详细讲过,不会使用的可以看这里

https://www.cnblogs.com/newAndHui/p/11105956.html

完美

上一篇:Java之volatile关键字


下一篇:线程Thread和线程池