死锁编码及定位分析?
作者:互联网
代码:
/** * @desc 死锁 * @Author xw * @Date 2019/8/23 */ public class DeadLockDemo { public static void main(String[] args) { String lockA = "lockA"; String lockB = "lockB"; new Thread(new HoldLockThread(lockA, lockB), "AAA").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(new HoldLockThread(lockB, lockA), "BBB").start(); } } class HoldLockThread implements Runnable { private String lockA; private String lockB; public HoldLockThread(String lockA, String lockB) { this.lockA = lockA; this.lockB = lockB; } @Override public void run() { synchronized (lockA) { // 锁A执有锁B System.out.println(Thread.currentThread().getName() + "\t 自己执有:" + lockA + "\t 尝试获取:" + lockB); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println(Thread.currentThread().getName() + "\t 自己执有:" + lockB + "\t 尝试获取:" + lockA); } } } }View Code
(1)jps命令定位进程号
(2)jstack死锁查看
标签:lockB,编码,lockA,HoldLockThread,String,定位,死锁,new,public 来源: https://www.cnblogs.com/ice-line/p/11423995.html