编程语言
首页 > 编程语言> > Java检查死锁的方式

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 

image

方式2:

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

image

# 查看具体栈信息
jstack 24341

标签:lockB,lockA,Java,String,HoldLock,检查,死锁,new,public
来源: https://www.cnblogs.com/mpyidudu/p/15628517.html