package com.prj;
public class DeadClock {
public static void main(String[] args) {
// TODO Auto-generated method stub
MakeUp g1=new MakeUp(0,"灰姑娘");
MakeUp g2=new MakeUp(0,"白雪公主");
g1.start();
g2.start();
}
}
class LipStick{
}
class Mirror{
}
class MakeUp extends Thread{
static LipStick lipstick=new LipStick();
static Mirror mirror=new Mirror();
int choice;
String name;
MakeUp(int choice,String name){
this.choice=choice;
this.name=name;
}
public void run() {
try {
makeup();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if(choice==0) {
synchronized (lipstick) {
System.out.println(this.name+"获得口红的锁");
Thread.sleep(1000);
// synchronized (mirror) {
// System.out.println(this.name+"获得镜子的锁");
// }
}
synchronized (mirror) {
System.out.println(this.name+"获得镜子的锁");
}
}else {
synchronized (mirror) {
System.out.println(this.name+"获得镜子的锁");
Thread.sleep(2000);
// synchronized (lipstick) {
// System.out.println(this.name+"获得口红的锁");
// }
}
synchronized (lipstick) {
System.out.println(this.name+"获得口红的锁");
}
}
}
}