工作中避免死锁的方法分享
转自:
http://www.java265.com/JavaJingYan/202205/16536185653533.html
下文笔者讲述工作中避免死锁的方法分享,如下所示:
1.设置超时时间
Lock的tryLock(Long timeout,TimeUnit unit) 设置获取锁的时间,如果达到这个时间还没有获取到锁,就回退 synchronized 不具备尝试锁的能力 获取不到锁的,也就是获取失败,我们进行日志记录,邮件提醒,重启服务器
/** * 用tryLock来避免死锁 */ public class TryLockDeadlock implements Runnable{ int flag = 1; static Lock lock1 = new ReentrantLock(); static Lock lock2 = new ReentrantLock(); public static void main(String[] args) { TryLockDeadlock deadlock1 = new TryLockDeadlock(); TryLockDeadlock deadlock2 = new TryLockDeadlock(); deadlock1.flag = 1; deadlock1.flag = 0; new Thread(deadlock1).start(); new Thread(deadlock2).start(); } @Override public void run() { for (int i = 0; i < 100; i++) { if(flag == 1){ try { if(lock1.tryLock(300, TimeUnit.MILLISECONDS)){ System.out.println("线程1获取到了锁1"); Thread.sleep(new Random().nextInt(10)); if(lock2.tryLock(300, TimeUnit.MILLISECONDS)){ System.out.println("线程1获取到了锁2"); lock1.unlock(); lock2.unlock(); break; }else{ System.out.println("线程1获取第二把锁的时候,失败,已经重试"); lock1.unlock(); Thread.sleep(new Random().nextInt(10)); } }else{ System.out.println("线程1获取锁1失败,已经重试"); } } catch (InterruptedException e) { e.printStackTrace(); } } if(flag == 0){ try { if(lock2.tryLock(3000, TimeUnit.MILLISECONDS)){ System.out.println("线程2获取到了锁2"); Thread.sleep(new Random().nextInt(1000)); if(lock1.tryLock(3000, TimeUnit.MILLISECONDS)){ System.out.println("线程2获取到了锁1"); lock2.unlock(); lock1.unlock(); break; }else{ System.out.println("线程2获取第1把锁的时候,失败,已经重试"); lock2.unlock(); Thread.sleep(new Random().nextInt(1000)); } }else{ System.out.println("线程2获取锁2失败,已经重试"); } } catch (InterruptedException e) { e.printStackTrace(); } } } } }