boolean?flag?=?false;
//?生成天氣數(shù)據(jù)的方法
public?synchronized?void?generate()?{
if?(flag)?{
try?{
wait();
}?catch?(InterruptedException?e1)?{
//?TODO?自動生成的?catch?塊
e1.printStackTrace();
}
}
temperature?=?(int)?(Math.random()?*?40?+?1);
humidity?=?(int)?(Math.random()?*?100?+?1);
flag?=?true;
notifyAll();
}
//?讀取天氣數(shù)據(jù)的方法
public?synchronized?void?read()?{
if?(!flag)?{
try?{
wait();
}?catch?(InterruptedException?e1)?{
//?TODO?自動生成的?catch?塊
e1.printStackTrace();
}
}
temperature?=?this.getTemperature();
humidity?=?this.getHumidity();
flag?=false;
notifyAll();
}
大家好,小弟遇到難題了 ,希望得到幫助。問題:flag是如何出發(fā)wait()的?老師在視頻里講的沒聽懂。
qq_秦皇島崔哥_03852186
2017-07-11 10:41:33