准备工具
Intellij IDEA
代码
package com.tony.test.practice;
import java.util.concurrent.TimeUnit;
/** * Created with IntelliJ IDEA. * * @Title: DeadLock * @Auther: 皮蛋布丁 * @Date: 2021/11/25/11:31 * @Description: */
public class DeadLock {
public static void main(String[] args) {
String lockA = "lockA";
String lockB = "lockB";
new Thread(new MyThread(lockA,lockB),"T1").start();
new Thread(new MyThread(lockB,lockA),"T2").start();
}
}
class MyThread implements Runnable{
private String lockA;
private String lockB;
public MyThread(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA){
System.out.println(Thread.currentThread().getName()+"lock:"+lockA+" =>get "+lockB);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB){
System.out.println(Thread.currentThread().getName()+"lock:"+lockB+" =>get "+lockA);
}
}
}
}
结果
出现死锁情况,程序始终在运行
问题排查
-
找到终端(Terminal)
-
输入 jps -l 查找进程号
-
输入jstack 进程号 找到死锁问题
注:能力有限,还请谅解,争取早日能够写出有质量的文章!
我是皮蛋布丁,一位爱吃皮蛋的热爱运动的废铁程序猿。
文章评论