|
发表于 2015-2-12 09:36:40
|
显示全部楼层
看我给你精简后的等效代码
class l implements Runnable{
int c;
boolean b,f;
public static void main(String[]args){
new l();
}
l(){
for(int i=0;i<6;i++)new Thread(this).start();
}
public void run(){
try{
if(b=!b)while(true){o();Thread.sleep(500);}
else while(true){i();Thread.sleep(500);}
}catch(Exception e){}
}
synchronized void i(){
while(f)try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"生产...烤鸭"+(++c));
f=!f;
notifyAll();
}
synchronized void o(){
while(!f)try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"消费......烤鸭"+c);
f=!f;
notifyAll();
}
}
|
|