如何去检测死锁

如何检测死锁

package threadStudy.deadLock;

/**
 * @description:
 * @author: brave.chen
 * @create: 2020-03-07 11:22
 **/
public class NormalDeadLock {
    private static Object first  = new Object();
    private static Object second  = new Object();

    private static void findToSecond() throws  InterruptedException{
        String str = Thread.currentThread().getName();
        synchronized (first){
            System.out.println("threadName = "+str + " get first");
            Thread.sleep(100);
            synchronized (second){
                System.out.println("threadName = "+str +" get second");
            }
        }
    }

    private static void secondToFirst() throws  InterruptedException{
        String str = Thread.currentThread().getName();
        synchronized (second){
            System.out.println("threadName = "+str + " get second");
            Thread.sleep(100);
            synchronized (first){
                System.out.println("threadName = "+str +" get first");
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                findToSecond();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                secondToFirst();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
    }
}

chenweideMacBook-Pro:study-2019 chenwei$ jps
49680 NormalDeadLock
49681 Launcher
1080 QuorumPeerMain
56987 Jps
6126 
chenweideMacBook-Pro:study-2019 chenwei$ jstack 49680
2020-03-07 11:29:35
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode):

"Attach Listener" #13 daemon prio=9 os_prio=31 tid=0x00007fcd21868000 nid=0x2f0b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

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

"Thread-1" #11 prio=5 os_prio=31 tid=0x00007fcd228b6800 nid=0x3d03 waiting for monitor entry [0x000070000c9a2000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at threadStudy.deadLock.NormalDeadLock.secondToFirst(NormalDeadLock.java:29)
        - waiting to lock <0x00000007956a9818> (a java.lang.Object)
        - locked <0x00000007956a9828> (a java.lang.Object)
        at threadStudy.deadLock.NormalDeadLock.lambda$main$1(NormalDeadLock.java:44)
        at threadStudy.deadLock.NormalDeadLock$$Lambda$2/2065951873.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

"Thread-0" #10 prio=5 os_prio=31 tid=0x00007fcd2313a000 nid=0x3c03 waiting for monitor entry [0x000070000c89f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at threadStudy.deadLock.NormalDeadLock.findToSecond(NormalDeadLock.java:18)
        - waiting to lock <0x00000007956a9828> (a java.lang.Object)
        - locked <0x00000007956a9818> (a java.lang.Object)
        at threadStudy.deadLock.NormalDeadLock.lambda$main$0(NormalDeadLock.java:37)
        at threadStudy.deadLock.NormalDeadLock$$Lambda$1/668386784.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

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

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

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

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

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

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

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007fcd23006000 nid=0x4e03 in Object.wait() [0x000070000c087000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000795588ed0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x0000000795588ed0> (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)

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

"VM Thread" os_prio=31 tid=0x00007fcd23005000 nid=0x5003 runnable 

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007fcd2200a800 nid=0x1d07 runnable 

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007fcd2200b000 nid=0x1e03 runnable 

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007fcd21808800 nid=0x5403 runnable 

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007fcd2280b800 nid=0x5203 runnable 

"VM Periodic Task Thread" os_prio=31 tid=0x00007fcd22077800 nid=0x4403 waiting on condition 

JNI global references: 320


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x00007fcd21834368 (object 0x00000007956a9818, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x00007fcd21836d58 (object 0x00000007956a9828, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at threadStudy.deadLock.NormalDeadLock.secondToFirst(NormalDeadLock.java:29)
        - waiting to lock <0x00000007956a9818> (a java.lang.Object)
        - locked <0x00000007956a9828> (a java.lang.Object)
        at threadStudy.deadLock.NormalDeadLock.lambda$main$1(NormalDeadLock.java:44)
        at threadStudy.deadLock.NormalDeadLock$$Lambda$2/2065951873.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
"Thread-0":
        at threadStudy.deadLock.NormalDeadLock.findToSecond(NormalDeadLock.java:18)
        - waiting to lock <0x00000007956a9828> (a java.lang.Object)
        - locked <0x00000007956a9818> (a java.lang.Object)
        at threadStudy.deadLock.NormalDeadLock.lambda$main$0(NormalDeadLock.java:37)
        at threadStudy.deadLock.NormalDeadLock$$Lambda$1/668386784.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

死锁预防

让线程获取锁的顺序一致

死锁检测

jps 查看java 进程信息 jstack +进程号 知道哪个死锁

你可能感兴趣的:(如何去检测死锁)