一般在Java项目里用到锁的场景不多,有朋友调侃说用到锁的次数还没有面试被问到的次数多,哈哈!
说句难听话,锁一般都很少用到,何况死锁呢?想产生死锁还是有点难的,需要满足2个条件:
共享资源同时只能被一个线程使用,如果已经有一个线程占用了资源,其余线程只能等待,直到资源被释放。
死锁情况肯定存在多个资源被多个线程争抢的情况。
比如线程1持有了资源A,然后去等待获取资源B,线程2持有了资源B,然后等待获取资源A,这样就会形成死锁。
一般有2种方式避免死锁:
/** * 避免死锁,我觉得有2种方式: * 1、线程直接一把头获取所需要的全部锁,不要分步 * 2、线程获取A之后,再去获取B,超时仍未获取到B,则释放A */public class AvoidDeadLock01 { private static Lock lock1 = new ReentrantLock(); private static Lock lock2 = new ReentrantLock(); public static void acquireLocks(Lock lock1, Lock lock2) { boolean isLock1Acquired = false; boolean isLock2Acquired = false; while (true) { try { isLock1Acquired = lock1.tryLock(); isLock2Acquired = lock2.tryLock(); } finally { if (isLock1Acquired && isLock2Acquired) { return; } if (isLock1Acquired) { lock1.unlock(); } if (isLock2Acquired) { lock2.unlock(); } } try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public static void main(String[] args) { Thread thread1 = new Thread(() -> { acquireLocks(lock1, lock2); System.out.println("=====线程1 获取到了2把锁====="); lock1.unlock(); lock2.unlock(); }); Thread thread2 = new Thread(() -> { acquireLocks(lock1, lock2); System.out.println("=====线程2 获取到了2把锁====="); lock1.unlock(); lock2.unlock(); }); thread1.start(); thread2.start(); }}
public class AvoidDeadLock02 { private static Lock lock1 = new ReentrantLock(); private static Lock lock2 = new ReentrantLock(); public static void acquireLocks(Lock lock1, Lock lock2) { boolean isLock1Acquired = false; boolean isLock2Acquired = false; try { while (true) { isLock1Acquired = lock1.tryLock(200, TimeUnit.MILLISECONDS); if (isLock1Acquired) { isLock2Acquired = lock2.tryLock(200, TimeUnit.MILLISECONDS); if (isLock2Acquired) { break; } else { lock1.unlock(); } } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { if (!isLock1Acquired || !isLock2Acquired) { if (isLock1Acquired) { lock1.unlock(); } if (isLock2Acquired) { lock2.unlock(); } } } } public static void main(String[] args) { Thread thread1 = new Thread(() -> { acquireLocks(lock1, lock2); System.out.println("=====线程1 获取到了2把锁====="); lock1.unlock(); lock2.unlock(); }); Thread thread2 = new Thread(() -> { acquireLocks(lock1, lock2); System.out.println("=====线程2 获取到了2把锁====="); lock1.unlock(); lock2.unlock(); }); thread1.start(); thread2.start(); }}
一般出现死锁时,可能会导致CPU、内存等资源消耗过高,导致系统性能下降。也可能导致应用无响应或者假死等等,所以要从多角度进行死锁的排查。
首先是用top、df、free等命令查看操作系统的基本情况。然后可以使用jmap、jstack等命令查看JVM线程栈和堆内存的情况。一般出现死锁时,会在线程栈的信息里出现deadlock字样。
还可以采用VisualVM、JConsole等工具进行排查。
本文链接:http://www.28at.com/showinfo-26-80818-0.htmlJava并发:如何避免死锁
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com