输出结果有时,生成天气数据在前面,但是获取到的值没有错,怎么回事?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package com.xww.weather; 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 synchronized void generate() { if (flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } temperature = ( int ) (Math.random() * 40 ); humidity = ( int ) (Math.random() * 100 ); this .setTemperature(temperature); this .setHumidity(humidity); flag= true ; notifyAll(); } public synchronized void read() { if (!flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } humidity = this .getHumidity(); temperature = this .getTemperature(); flag= false ; notifyAll(); } @Override public String toString() { return "[温度:" + temperature + ", 湿度:" + humidity + "]" ; } } |
正在回答
同学你好,修改代码如下图所示:如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.xww.weather; public class GenerateWeather implements Runnable { Weather weather; public GenerateWeather(Weather weather) { this .weather = weather; } @Override public void run() { for ( int i = 0 ; i < 100 ; i++) { weather.generate(); System.out.println( "生成天气数据" +weather); try { Thread.sleep( 5000 ); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.xww.weather; public class ReadWeather implements Runnable { Weather weather; public ReadWeather(Weather weather) { this .weather = weather; } @Override public void run() { for ( int i= 0 ;i< 100 ;i++) { weather.read(); System.out.println( "读取天气数据" +weather); try { Thread.sleep( 100 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 | package com.xww.weather; public class WeatherTest { public static void main(String[] args) { Weather one = new Weather(); new Thread( new GenerateWeather(one)).start(); new Thread( new ReadWeather(one)).start(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package com.xww.weather; 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 synchronized void generate() { if (flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } temperature = ( int ) (Math.random() * 40 ); humidity = ( int ) (Math.random() * 100 ); this .setTemperature(temperature); this .setHumidity(humidity); flag= true ; notifyAll(); } public synchronized void read() { if (!flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } humidity = this .getHumidity(); temperature = this .getTemperature(); flag= false ; notifyAll(); } @Override public String toString() { return "[温度:" + temperature + ", 湿度:" + humidity + "]" ; } } |
1 | <br> |
package com.xww.weather;
1 | <br> |
public class GenerateWeather implements Runnable {
Weather weather;
public GenerateWeather(Weather weather) {
this.weather = weather;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
weather.generate();
System.out.println("生成天气数据"+weather);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.xww.weather;
public class ReadWeather implements Runnable {
Weather weather;
public ReadWeather(Weather weather) {
this.weather = weather;
}
@Override
public void run() {
for(int i=0;i<100;i++) {
weather.read();
System.out.println("读取天气数据"+weather);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.xww.weather;
public class WeatherTest {
public static void main(String[] args) {
Weather one = new Weather();
new Thread(new GenerateWeather(one)).start();
new Thread(new ReadWeather(one)).start();
}
}
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧