p22 死锁
package kuang.thread;
public class DeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0,"泰罗");
Makeup g2 = new Makeup(1,"迪迦");
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 girlfriend;
Makeup(int choice,String girlfriend){
this.choice = choice;
this.girlfriend = girlfriend;
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException {
if (choice == 0) {
synchronized (lipstick) {
System.out.println(this.girlfriend + "获得口红的锁");
Thread.sleep(1000);
}
synchronized (mirror) {
System.out.println(this.girlfriend + "获得精子的锁");
}
} else {
synchronized (mirror) {
System.out.println(this.girlfriend + "获得镜子的锁");
Thread.sleep(1000);
}
}synchronized (lipstick) {
System.out.println(this.girlfriend + "获得口红的锁");
}
}
}