package usi.dbdp.SalesOrder.controller;
public class DeadLock {
public static void main(String[] args) {
Man m1=new Man(0, "你");
Man m2=new Man(1, "我");
m1.start();
m2.start();
}
}
class Kouhong{}
class Jingzi{}
class Man extends Thread{
private static Kouhong k = new Kouhong();
private static Jingzi j = new Jingzi();
int choice;
String name;
public Man(int choice, String name) {
super();
this.choice = choice;
this.name = name;
}
@Override
public void run() {
try {
makeup();
} catch (Exception e) {
e.printStackTrace();
}
}
private void makeup() throws Exception {
if (choice == 0) {
synchronized (k) {
System.out.println(this.name+"--拿到了口红的锁");
Thread.sleep(1000);
}
synchronized (j) {
System.out.println(this.name+"--拿到了镜子的锁");
}
}else {
synchronized (j) {
System.out.println(this.name+"--拿到了镜子的锁");
Thread.sleep(1000);
}
synchronized (k) {
System.out.println(this.name+"--拿到了口红的锁");
}
}
}
}
死锁解决方法
最新推荐文章于 2022-11-05 12:28:27 发布