1.执行死锁代码
public class DeadLockTest { public static void main(String[] args) throws InterruptedException { //a线程持有a锁,并尝试获取b锁;b线程持有b锁,并尝试获取a锁 Object lockA = new Object(); Object lockB = new Object(); Thread threadA = new Thread(() -> { synchronized (lockA) { System.out.println("threadA获取到lockA,尝试获取lockB"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println("threadA获取到lockA,且获取到lockB"); } } }); Thread threadB = new Thread(() -> { synchronized (lockB) { System.out.println("threadB获取到lockB,尝试获取lockA"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockA) { System.out.println("threadB获取到lockB,且获取到lockA"); } } }); threadA.start(); threadB.start(); threadA.join(); threadB.join(); } }
2.查看java进程:jsp -l
3.查看线程堆栈:jstack -l 96354
最后明确说明发现了死锁
标签:lockB,threadA,lockA,threadB,Thread,获取,排查,死锁 From: https://www.cnblogs.com/katsu2017/p/17234857.html