1 package com.atguigu.boot.com.atguigu; 2 /** 3 * 死锁是两个或者两个以上线程争抢资源而产生的问题 4 * 导致死锁的原因:线程相互争抢被其他线程锁定的资源,系统资源不足 进行运行推进的顺序不当 资源分配不当 5 */ 6 class HoldLocalThread implements Runnable 7 { 8 private String lockA; 9 private String lockB; 10 11 public HoldLocalThread(String lockA, String lockB) { 12 this.lockA = lockA; 13 this.lockB = lockB; 14 } 15 @Override 16 public void run() { 17 synchronized (lockA) 18 { 19 System.out.println(Thread.currentThread().getName()+"线程持有锁"+lockA+"\t 尝试获取锁"+lockB); 20 synchronized (lockB) 21 { 22 System.out.println(Thread.currentThread().getName()+"线程持有锁"+lockB+"\t 尝试获取锁"+lockA); 23 } 24 } 25 } 26 } 27 28 public class DeadLockDemo_55 { 29 public static void main(String[] args) { 30 String lockA="lockA"; 31 String lockB="lockB"; 32 new Thread(new HoldLocalThread(lockA,lockB),"ThreadAAA").start(); 33 new Thread(new HoldLocalThread(lockB,lockA),"ThreadBBB").start(); 34 } 35 }