boolean flag = false;
// 生成天气数据的方法
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();
}
// 读取天气数据的方法
public synchronized void read() {
if (!flag) {
try {
wait();
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
temperature = this.getTemperature();
humidity = this.getHumidity();
flag =false;
notifyAll();
}为梦想努力_冬