死锁是由于“同步块需要同时持有多个对象锁造成”的,要解决这个问题,思路很简单,就是:同一个代码块,不要同时持有两个对象锁。
/**
* 口红类
*/
class Lipstick{
}
/**
* 镜子类
*/
class Mirror{
}
/**
* 化妆线程类
*/
class Makeup extends Thread{
private int flag; //flag=0:拿着口红。flag!=0:拿着镜子
private String girlName;
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
public void setFlag(int flag){
this.flag = flag;
}
public void setGirlName(Set girlName){
this.girlName = girlName;
}
@Override
public void run(){
this.doMakeup();
}
/**
* 开始化妆
*/
public void doMakeup(){
if(flag == 0){
synchronized(lipstick){
System.out.println(this.girlName+" 拿着口红");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printstackTrace();
}
synchronized(mirror){
System.out.println(this.girlName+" 拿着镜子");
}
}
}else{
synchronized(mirror){
System.out.println(" 拿着镜子");
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized(lipstick){
System.out.println(this.getName+" 拿着口红");
}
}
}
}
}
public class DeadLockThread{
public static void main(String[] args){
Makeup makeup = new Makeup();
make.setFlag(0);
makeup.setGirlName("大丫");
Makeup makeup1 = new Makeup();
makeup1.setFlag(1);
makeup1.setGirlName("小丫");
makeup.start();
makeup1.start();
}
}