5-3自由编程,请老师看看我的问题

5-3自由编程,请老师看看我的问题

package thread;
//Weather类
public class Weather {
 private int temperature;
 private int humidity;
  boolean flag=false;
 public Weather() {
 }
 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;
 }
 
 @Override
 public String toString() {
  return "天气数据 [温度:" + temperature + ", 湿度:" + humidity + "]";
 }
 public synchronized void generate() {
        for(int i=0;i<100;i++) {
         int n=(int)(Math.random()*40);
      int m=(int)(Math.random()*100);
  setTemperature(n);
  setHumidity(m);
  if(flag) {
   
   try {
    wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  notifyAll();
  System.out.println("生成"+toString());
  flag=true;
        }
 }
 public synchronized void read() {
    for(int i=0;i<100;i++) {
     
  this.temperature=getTemperature();
  this.humidity=getHumidity();
  if(!flag) {
   try {
    wait();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  System.out.println("读取"+toString());
  flag=false;
    }
 }
 
}
//GenerateWeather类
package thread;
public class GenerateWeather implements Runnable {
 Weather weather;
    public GenerateWeather(Weather weather) {
  // TODO Auto-generated constructor stub
     this.weather=weather;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  weather.generate();
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}
// ReadWeather类
package thread;
public class ReadWeather implements Runnable{
Weather weather;
public ReadWeather(Weather weather) {
 // TODO Auto-generated constructor stub
 this.weather=weather;
}
@Override
public void run() {
 // TODO Auto-generated method stub
 
 weather.read();
 try {
  Thread.sleep(1000);
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}
//主类
package thread;
public class WeatherTest {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Weather weather=new Weather();
  try {
   new Thread(new GenerateWeather(weather)).join();
   new Thread(new ReadWeather(weather)).join();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  new Thread(new GenerateWeather(weather)).start();
  new Thread(new ReadWeather(weather)).start();
  
  
 }
}


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

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

3回答
提问者 慕前端6228120 2018-09-28 21:05:35
提问者 慕前端6228120 2018-09-28 18:50:28

生成天气数据 [温度:15, 湿度:38]
读取天气数据 [温度:15, 湿度:38]
生成天气数据 [温度:4, 湿度:52]
读取天气数据 [温度:34, 湿度:79]
生成天气数据 [温度:34, 湿度:79]
读取天气数据 [温度:25, 湿度:75]

  • 同学还是在Weather类中写的循环吗?将循环写在ReadWeather和GenerateWeather类中,就不会出现先读取再生成的结果。如果有问题,可以继续提问。祝:学习愉快。
    2018-09-28 18:55:14
  • 提问者 慕前端6228120 回复 好帮手慕阿满 #2
    不是了,是在两个run()中写的循环语句
    2018-09-28 18:58:11
  • 好帮手慕阿满 回复 提问者 慕前端6228120 #3
    按照思路改过之后,这边运行程序没有再出现这种情况,建议同学将代码和运行结果截图发上来,我们帮你再调试一下程序。祝:学习愉快!
    2018-09-28 19:13:50
好帮手慕阿满 2018-09-28 18:18:06

运行同学的程序,结果只生成和读取一遍天气数据,就不再运行,而且生成的数据和读取的数据不一样。修改意见:Weather类中,read()方法中缺少了notifyAll();需要添加上。另外建议将循环写在ReadWeather和GenerateWeather类中,定义int型变量n初始化为1,run方法中添加while循环,循环中n++,如果n==100,break退出循环。

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

  • 提问者 慕前端6228120 #1
    按照老师你的方法改了以后,运行过程中依然有小问题,是什么情况啊,截图在回答里了
    2018-09-28 18:49:49
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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