请老师帮我分析一下代码问题
import java.util.Random; public class Weather { private int temperature; private int humidity; boolean flag = false; public synchronized int getTemperature() { if (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } flag = true; notifyAll(); return temperature; } public synchronized int getHumidity() { if (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } flag = true; notifyAll(); return humidity; } public synchronized void setTemperature(int temperature) { if (flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.temperature = temperature; flag = false; notifyAll(); } public synchronized void setHumidity(int humidity) { if (flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.humidity = humidity; flag = false; notifyAll(); } public void generate() { Random r = new Random(); setTemperature(r.nextInt(40)); setHumidity(r.nextInt(100)); System.out.println("生成天气数据"+ this.toString()); } public void read() { System.out.println("读取天气数据" + this.toString()); } @Override public String toString() { return "[温度:" + temperature + ",湿度:" + humidity + "]"; } } public class GenerateWeather implements Runnable { Weather weather; GenerateWeather(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0; i < 100; i++) { weather.generate(); try { wait(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ReadWeather implements Runnable{ Weather weather; ReadWeather(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0; i < 100; i++) { weather.read(); try { wait(100); } catch (InterruptedException e) { 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(); } }
0
收起
正在回答 回答被采纳积分+1
3回答
好帮手慕阿满
2019-03-18 18:21:41
同学你好,同学的Weather类中的代码有些问题,应该将判断flag的代码写在generate()和read()方法中,参考如下代码:
import java.util.Random; public class Weather { private int temperature; private int humidity; boolean flag = false; public int getTemperature() { return temperature; } public synchronized void setTemperature(int temperature) { this.temperature = temperature; } public synchronized int getHumidity() { return humidity; } public synchronized void setHumidity(int humidity) { this.humidity = humidity; } public synchronized void generate() { if(flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Random r = new Random(); setTemperature(r.nextInt(41)); setHumidity(r.nextInt(101)); System.out.println("生成天气数据" + this.toString()); flag = true; notifyAll(); } public synchronized void read() { if(!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } temperature = getTemperature(); humidity = getHumidity(); flag = false; notifyAll(); System.out.println("读取天气数据" + this.toString()); } @Override public String toString() { return "[温度:" + temperature + ",湿度:" + humidity + "]"; } }
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
巴呆丶
2019-03-18 16:53:51
import java.util.Random; public class Weather { private int temperature; private int humidity; boolean flag = false; public synchronized int getTemperature() { if (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } flag = true; notifyAll(); return temperature; } public synchronized void setTemperature(int temperature) { if (flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.temperature = temperature; flag = false; notifyAll(); } public synchronized int getHumidity() { if (!flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } flag = true; notifyAll(); return humidity; } public synchronized void setHumidity(int humidity) { if (flag) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.humidity = humidity; flag = false; notifyAll(); } public void generate() { Random r = new Random(); setTemperature(r.nextInt(41)); setHumidity(r.nextInt(101)); System.out.println("生成天气数据"+ this.toString()); } public void read() { temperature = getTemperature(); humidity = getHumidity(); System.out.println("读取天气数据" + this.toString()); } @Override public String toString() { return "[温度:" + temperature + ",湿度:" + humidity + "]"; } } public class GenerateWeather implements Runnable { Weather weather; GenerateWeather(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0; i < 100; i++) { weather.generate(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ReadWeather implements Runnable{ Weather weather; ReadWeather(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0; i < 100; i++) { weather.read(); try { Thread.sleep(100); } catch (InterruptedException e) { 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(); } }
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星