package com.mz.xx.tb;
public class DeadLock implements Runnable {
private String o1 = "lock1";
private String o2 = "lock2";
private String waysName;
public DeadLock(String waysName) {
super();
this.waysName = waysName;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(waysName.equals("way1")){
synchronized(o1){
try {
System.out.println("Lock o1");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (o2) {
System.out.println("way1:Lock o1 o2");
}
}
}
else if(waysName.equals("way2")){
synchronized(o2){
try {
System.out.println("Lock o2");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (o1) {
System.out.println("way2:Lock o2 o1");
}
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new DeadLock("way1"));
Thread t2 = new Thread(new DeadLock("way2"));
t1.start();
t2.start();
}
}
简单的死锁案例
最新推荐文章于 2025-05-05 09:41:57 发布