Several methods to check java thread deadlock

1. Use jconsole

    jconsole is a built-in java performance analyzer that comes with jdk, which is used to monitor the performance of Java applications and track the code in Java;

    It has a close relative, jvisualvm;

So how to use it?

    1. Very simple, enter in the idea command line:

D:\java\workspace\AS> jconsole

The GUI is displayed:

Here is a deadlock demo code:

package com.river.morethread;

public class DealLock implements Runnable{

    public String username;

    public Object lockOne = new Object();

    public Object lockSecond = new Object();

    public void serFlag(String username){
        this.username = username;
    }

    @Override
    public void run() {

        if ("a".equals(username)){
            synchronized (lockOne){
                try {
                    System.out.println("username -> " + username);
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockSecond){
                    System.out.println("lockOne -> lockSecond");
                }
            }
        }



        if ("b".equals(username)){
            synchronized (lockSecond){
                try {
                    System.out.println("username -> " + username);
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockOne){
                    System.out.println("lockSecond -> lockOne");
                }
            }
        }

    }


    public static void main(String[] args) throws InterruptedException {
        DealLock d1 = new DealLock();
        d1.serFlag("a");
        new Thread(d1).start();
        Thread.sleep(100);
        d1.serFlag("b");
        new Thread(d1).start();
    }
}

So the Gui selects the process to look at:

It can be seen here that jconsole supports both local and remote connections;

2. After clicking the link, enter the main page:

There are several tabs in the upper left corner, which monitor different information respectively.

3. View the thread deadlock, select the thread tab, at the bottom

There is a detection deadlock, click:

This picture can see the deadlock information, class name and method name;

 

2. Use jstack

1. First use jps to view the process pid information

2. Use jstack -l [pid] to view the thread information in it

There is a lot of information displayed here, not a complete screenshot...

This shows Found one Java-level deadlock: and the specific deadlock information display;

3. Using jvisualvm

        In (1), we introduced jvisualvm, a close relative of jconsole, which is introduced here;

        input the command

jvisualvm

 The following information will be displayed,

   

and show the GUI image:

The left side shows the current process information and pid. We click on DealLock (pid: 122168) of the process we are demonstrating deadlock.

Here are some current process information displays, which are similar to those of jconsole. Not much else to say, just click on the thread:

You can see the prompt detected deadlock, click the "Thread Dump" button on the right side of the box to display all thread information, when you see the bottom:

Does this look familiar, no need to say anymore...  

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325441606&siteId=291194637