死锁问题排查

代码示例:

public class Test002 {


    private static Object lock1 = new Object();
    private static Object lock2 = new Object();


    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock1) {
                    try {
                        System.out.println("Thread 1 start ");
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock2) {
                        System.out.println("Thread 1 end");
                    }
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock2) {
                    try {
                        System.out.println("Thread 2 start ");
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock1) {
                        System.out.println("Thread 2 end");
                    }
                }
            }
        }).start();


    }

}

我们使用jvisualvm进行查看,可以直接到死信息,如下图

 

 查看dump信息,很明确的提示了在那里等锁信息。如下:

 

 

posted @ 2019-11-27 15:27  leoBlog  阅读(284)  评论(0编辑  收藏  举报