如何排查死锁问题
可以使用JDK自带的命令行工具排查:
基本就可以看到死锁的信息。
还可以利用图形化工具,比如JConsole(JConsole工具在JDK的bin目录中)。出现线程死锁以后,点击JConsole线程面板的检测到死锁按钮,将会看到线程的死锁信息。
演示样例如下:
package lock;
/**
* @author 百里
*/
public class BaiLiDeadLock {
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(string[] args) {
Thread thread1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread-1获取了锁1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("Thread-1尝试获取锁2");
}
}
});
Thread thread2 = new Thread(() -> {
synchronized (lock2) {
System.out.println("Thread-2获取了锁2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("Thread-2尝试获取锁1");
}
}
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
1.新建连接,找到相应的线程,点击连接
2.选择线程标签,点击检测死锁。查看死锁线程信息