Java检查死锁的方式


模式死锁

public class DeadLockDemo {

    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new HoldLock(lockA,lockB), "AA").start();
        new Thread(new HoldLock(lockB,lockA), "BB").start();
    }

}

class HoldLock implements Runnable{

    private String lockA;
    private String lockB;

    public HoldLock(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "\t 自己持有:" + lockA + "\t 尝试获取:" + lockB);
            TimeUnit.SECONDS.sleep(2);
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "\t 自己持有:" + lockB + "\t 尝试获取:" + lockA);
            }
        }
    }
}

方式1: 使用Jcosole查看

jconsole 

方式2:

# 查看运行的Java进程 pid
jps -l

# 查看具体栈信息
jstack 24341