程序无法运行的毛病在哪里

程序无法运行的毛病在哪里

package com.imooc.name;


public class Weather {

private int temperature;

private int humidity;

boolean flag = false;


public int getTemperature() {

return temperature;

}


public void setTemperature(int temperature) {

this.temperature = temperature;

}


public int getHumidity() {

return humidity;

}


public void setHumidity(int humidity) {

this.humidity = humidity;

}


public void generate() {

if (flag) {

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

setTemperature((int) (Math.random() * 40));

setHumidity((int) (Math.random() * 100));

System.out.println("生成天气数据[温度:" + temperature + ",湿度" + humidity + "]");

flag = true;

notifyAll();

}

}


public void read() {

if (flag) {

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

getTemperature();

getHumidity();

System.out.println("生成天气数据[温度:" + temperature + ",湿度" + humidity + "]");

flag = false;

notifyAll();

}

}


public String toString() {

return "生成天气数据[温度:" + temperature + ",湿度" + humidity + "]";

}

}

package com.imooc.name;


public class GenerateWeather implements Runnable {

Weather weather;


GenerateWeather(Weather weather) {

this.weather = weather;

}


public void run() {

for (int i = 1; i <= 100; i++) {

weather.generate();

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

package com.imooc.name;


public class ReadWeather implements Runnable{

     Weather weather;

ReadWeather(Weather weather){

     this.weather=weather;

      }

      public void run() {

     for(int i=1;i<=100;i++) {

     weather.getHumidity();

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

     }

      }

}

package com.imooc.name;


public class Test {


public static void main(String[] args) {

     Weather weather=new Weather();

     new Thread(new GenerateWeather(weather)).start();

     new Thread(new ReadWeather(weather)).start();


}


}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿慧 2020-03-19 10:23:59

同学你好,

1、生成和读取温度中应该先判断flag,如果条件不满足,进行等待。如果条件满足进行生成或读取温度。所以应该把生成或读取温度放在if判断外面。

另外,read和generate方法应该加上synchronized,实现方法同步。read方法输出语句错误。

修改如下:

http://img1.sycdn.imooc.com//climg/5e72d75c09fb9c4107670274.jpg

http://img1.sycdn.imooc.com//climg/5e72d7540930845a07280283.jpg

2、ReadWeather类中温度读取应该调用read()方法,不是getHumidity()方法。

修改如下:

http://img1.sycdn.imooc.com//climg/5e72d76a09f5051c04780231.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕函数4736799 #1
    修改后只能进行一次生成天气预报,读取都没有进行,也未进行第二次生成
    2020-03-19 13:16:21
  • 好帮手慕阿慧 回复 提问者 慕函数4736799 #2
    同学你好,Weather类read方法中的if判断应该是!flag。
    2020-03-19 13:56:20
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师