检查java线程死锁的几种方法

目录:Java线程死锁检测

1、使用jconsole

1.1 是什么?

1.2 怎么用?(win环境为例)

2、使用jstack

2.1 是什么?

2.2 怎么用?

3、使用jvisualvm

3.1 是什么?

3.2 怎么用?


死锁例子:

public class DeadLockTest implements Runnable {

    public String userName;
    public Object lockOne = new Object();
    public Object lockTwo = new Object();

    public void setFlag(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 (lockTwo) {
                    System.out.println("lockOne - > lockTwo");
                }
            }
        }

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

                synchronized (lockOne) {
                    System.out.println("lockTow -> lockOne");
                }
            }
        }
    }

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

1、使用jconsole

1.1 是什么?

jconsole是jdk自带的内置java性能分析器,用来监控Java应用程序的性能和跟踪Java中的代码.

1.2 怎么用?(win环境为例)

(1)cmd下输入 jconsole,回车。

C:\Users\10498>jconsole

显示出下图的JUI界面,这里可以看出,jconsole支持本地,也支持远程连接。

检查java线程死锁的几种方法_第1张图片

(2)选择本地进程,点击需要查看的进程,连接。

检查java线程死锁的几种方法_第2张图片

(3)查看线程死锁,选择”线程“,再点击“检测死锁”,选择线程,查看信息,可以看到死锁信息,类名及方法名。

检查java线程死锁的几种方法_第3张图片检查java线程死锁的几种方法_第4张图片

2、使用jstack

2.1 是什么?

jstack是java虚拟机自带的一种堆栈跟踪工具。

2.2 怎么用?

(1)先用jps查看进程pid信息。

C:\Users\10498>jps
13136 Launcher
13264 Jps
8736 RemoteMavenServer
9092 DeadLockTest
13464 JConsole
9372

(2)使用jstack -l [pid]查看其中的线程信息,可看到Found one Java-level deadlock:及具体的死锁信息展示。

C:\Users\10498>jstack -l 9092
2019-08-11 15:13:01
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):
//省略
Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x000000001d1535c8 (object 0x000000076b877570, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x000000001d150d38 (object 0x000000076b877580, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at com.wyq.hello2.DeadLockTest.run(DeadLockTest.java:40)
        - waiting to lock <0x000000076b877570> (a java.lang.Object)
        - locked <0x000000076b877580> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:748)
"Thread-0":
        at com.wyq.hello2.DeadLockTest.run(DeadLockTest.java:25)
        - waiting to lock <0x000000076b877580> (a java.lang.Object)
        - locked <0x000000076b877570> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

3、使用jvisualvm

3.1 是什么?

 JVM 监控工具,可以监控 CPU ,内存,类,线程等运行状况,实时监控服务器性能。

3.2 怎么用?

(1)cmd下输入 jvisualvm,显示GUI界面,选择相应进程查看

C:\Users\10498>jvisualvm

C:\Users\10498>

The launcher has determined that the parent process has a console and will reuse it for its own console output.
Closing the console will result in termination of the running program.
Use '--console suppress' to suppress console output.
Use '--console new' to create a separate console window.

检查java线程死锁的几种方法_第5张图片检查java线程死锁的几种方法_第6张图片

(2)点击“线程”,再点击“dump”可以查看详细信息

 

原文地址:https://my.oschina.net/JackieRiver/blog/1789476

你可能感兴趣的:(检查java线程死锁的几种方法)