為什麼沒有輸出結果?
public class Weather { //屬性 溫度和濕度 private int temperature; private int humidity; private boolean flag=false; //getter 和setter 方法 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 synchronized void generate(){ if(!flag){ try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setTemperature((int) (Math.random()*40)); this.setHumidity((int) (Math.random()*100)); } System.out.println("生成天氣數據[溫度 : " + this.getTemperature() + ", 濕度 : " + this.getHumidity() + "]"); flag=false; notifyAll(); } public synchronized void read(){ if(flag){ try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("讀取天氣數據[溫度 : " + this.getTemperature() + ", 濕度 : " + this.getHumidity() + "]"); flag=true; notifyAll(); } } @Override public String toString(){ return "[溫度 : " + temperature + ", 濕度 : " + humidity + "]"; } } 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 while(true){ weather.generate(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 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 while(true){ weather.read(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class WeatherTest { public static void main(String[] args){ Weather weather = new Weather(); new Thread(new GenerateWeather(weather)).start(); new Thread(new ReadWeather(weather)).start(); } }
39
收起
正在回答
3回答
首先,flag的初始值应该为true,如果为false,generate()这个方法一开始就wait()所以不会执行,就在那等待。
另外,下面的代码:
public synchronized void generate(){ if(!flag){ try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setTemperature((int) (Math.random()*40)); this.setHumidity((int) (Math.random()*100)); } System.out.println("生成天氣數據[溫度 : " + this.getTemperature() + ", 濕度 : " + this.getHumidity() + "]"); flag=false; notifyAll(); }
注意if语句的大括号写错了,应该改成这样,注意看注释,read()方法也是一样的
public synchronized void generate(){ if(!flag){ try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//大括号到这了 this.setTemperature((int) (Math.random()*40)); this.setHumidity((int) (Math.random()*100)); //此处的大括号没了 System.out.println("生成天氣數據[溫度 : " + this.getTemperature() + ", 濕度 : " + this.getHumidity() + "]"); flag=false; notifyAll(); }
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星