死锁问题之查看排除死锁
程序员文章站
2022-03-10 18:23:02
public class DeadLock { public static void main(String[] args) { String lockA = "locka"; String lockB = "lockb"; new Thread(new deadlockTest(lockA, lockB), "t1").start(); new Thread(new deadlockTest(lockB, lockA), "t2")...
public class DeadLock {
public static void main(String[] args) {
String lockA = "locka";
String lockB = "lockb";
new Thread(new deadlockTest(lockA, lockB), "t1").start();
new Thread(new deadlockTest(lockB, lockA), "t2").start();
}
}
class deadlockTest implements Runnable {
private String lockA;
private String lockB;
public deadlockTest(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA) {
System.out.println(Thread.currentThread().getName() + "is " + lockA + " want " + lockB);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB) {
System.out.println(Thread.currentThread().getName() + "is " + lockB + " want " + lockA);
}
}
}
}
代码会造成死锁问题:
这个时候我们可以查看堆栈里面的情况:
使用jps-l
查看进程号
得到进程号为14100
使用jstack 进程号
查看堆栈的情况
往下面翻可以看见t1和t2两个线程各自都等着对面的,
所有互相交叉,形成死锁
本文地址:https://blog.csdn.net/csdnerM/article/details/112028043
下一篇: 一次binlog暴涨的分析