class deadTest implements Runnable{
private int num=100;
boolean fag=true;
Object obj=new Object();
public void run(){
if(fag){
while(true){
synchronized(obj){
show();
}
}
}else{
while(true){
show();
}
}
}
public synchronized void show(){
synchronized(obj){
if(num>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...."+num--);
}
}
}
}
public class 同步死锁 {
public static void main(String[] args){
deadTest d=new deadTest();
Thread t=new Thread(d);
Thread t1=new Thread(d);
t.start();
t1.start();
}
} |