下面这段代码,我转不过来弯,private boolean flag = false既然已经是假了,set这个函数里的 while(flag)是真还是假?为什么毕老师说判断是假,不需要等待?if语句不是满足条件就执行下面语句吗?为什么要跳过wait()呢?
private boolean flag = false;
public synchronized void set(String name)
{ if(flag)
try{wait();}catch(Exception e){}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
flag = true;
this.notify();
}
public synchronized void out()
{
if(!flag)
try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....消费者.............."+this.name);
flag = false;
this.notify();
}
|
|