手写一个死锁Demo

时间:2024-04-29 14:52:47

相互持有对方锁,导致死锁

 package jesse.test04;

 public class DeadLockSample extends Thread{

     private String first;
private String second;
public DeadLockSample(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()+" obtained: "+ first);
try {
Thread.sleep(1000L);
synchronized(second){
System.out.println(this.getName()+" obtained: "+ second);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public static void main(String[] args) throws InterruptedException {
String lockA = "lockA";
String lockB = "locakB";
DeadLockSample t1 = new DeadLockSample("thread1", lockA, lockB);
DeadLockSample t2 = new DeadLockSample("thread2", lockB, lockA);
t1.start();
t2.start();
t1.join();
t2.join();
} }