死锁是并发编程的难点问题。
大家好,我是李福春,我在准备面试,今天的问题是:
死锁是如何产生的?如何定位?如何修复和避免?
答:死锁是一种特定的程序状态,一般是多线程场景下两个以上的线程互相持有对方需要的锁而处于的永久阻塞状态。
定位方法:jstack分析线程的栈信息可以定位出来; 或者使用ThreadMXBean相关的api在程序中打印出相关的死锁信息;
修复和规避:死锁问题一般无法在线解决,一般紧急的先重启应用保证可用;然后在开发环境采用互相代码审查,使用预防性工具比如finBugs提前发现可能发生死锁的程序,修复程序本身的问题。
死锁的产生和定位
死锁的产生场景场景如下图:
死锁代码示例 package org.example.mianshi.deadlock; import java.util.concurrent.TimeUnit; /** * 作者: carter * 创建日期: 2020/3/30 12:09 * 描述: 死锁例子 */ public class DeadLockApp { public static void main(String[] args) { String first = "LockA"; String second = "LockB"; MyThread a = new MyThread("a", first, second); MyThread b = new MyThread("b", second, first); a.start(); b.start(); try { a.join(); b.join(); } catch (InterruptedException e) { e.printStackTrace(); } } public static class MyThread extends Thread { private final String first; private final String second; public MyThread(String name, String first, String second) { super(name); this.first = first; this.second = second; } @Override public void run() { synchronized (first) { System.out.println(this.getName() + " invoke first:" + first); try { TimeUnit.SECONDS.sleep(1); synchronized (second) { System.out.println(this.getName() + " invoke second:" + second); } } catch (InterruptedException e) { e.printStackTrace(); } } } } } jstack分析死锁
1, 找出运行程序的进程ID, ps -ef | grep java
2, 使用jstack pid 来分析线程的状态;
运行效果:
诊断结果:
使用api定位死锁信息 package org.example.mianshi.deadlock; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.Arrays; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * 作者: carter * 创建日期: 2020/3/30 12:09 * 描述: 死锁例子 */ public class DeadLockApp { private static final ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1); public static void main(String[] args) { checkDeadLockByThreadMXBean(); String first = "LockA"; String second = "LockB"; MyThread a = new MyThread("a", first, second); MyThread b = new MyThread("b", second, first); a.start(); b.start(); try { a.join(); b.join(); } catch (InterruptedException e) { e.printStackTrace(); } } private static void checkDeadLockByThreadMXBean() { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); threadPool.scheduleAtFixedRate(()->{ long[] deadlockedThreads = threadMXBean.findDeadlockedThreads(); if (deadlockedThreads!=null && deadlockedThreads.length>0){ Arrays.stream(deadlockedThreads) .mapToObj(id->threadMXBean.getThreadInfo(id)) .forEach(threadInfo->{ System.out.println("出现死锁的线程是:"+threadInfo); }); } }, 1, 5, TimeUnit.SECONDS); } public static class MyThread extends Thread { private final String first; private final String second; public MyThread(String name, String first, String second) { super(name); this.first = first; this.second = second; } @Override public void run() { synchronized (first) { System.out.println(this.getName() + " invoke first:" + first); try { TimeUnit.SECONDS.sleep(1); synchronized (second) { System.out.println(this.getName() + " invoke second:" + second); } } catch (InterruptedException e) { e.printStackTrace(); } } } } }
定位结果是:
程序中自动打出了产生死锁的线程,进一步的分析代码,然后解决。
如何避免死锁