输出结果有时,生成天气数据在前面,但是获取到的值没有错,怎么回事?

输出结果有时,生成天气数据在前面,但是获取到的值没有错,怎么回事?

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 + "]";
    }
 
}

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

正在回答

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

5回答

同学你好,修改代码如下图所示:http://img1.sycdn.imooc.com//climg/5cadca3400019fff07970617.jpghttp://img1.sycdn.imooc.com//climg/5cadca6900014d7005790433.jpghttp://img1.sycdn.imooc.com//climg/5cadca95000182a805880425.jpg如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 天天4244770 提问者 #1
    谢谢老师,我也是这么想的
    2019-04-10 18:54:51
吃吃吃鱼的猫 2019-04-10 15:18:52

同学你好,发生这种情况的原因是两个线程开启后,争夺资源交替运行。当天气的数据生成后,还未进行输出时先进行了读取操作并输出。http://img1.sycdn.imooc.com//climg/5cad91d60001683704490111.jpg如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 提问者 天天4244770 #1
    怎么解决这个问题呢?
    2019-04-10 15:24:42
提问者 天天4244770 2019-04-10 10:26:37
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 + "]";
    }
 
}


提问者 天天4244770 2019-04-10 10:22:14
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();

}


}


吃吃吃鱼的猫 2019-04-10 10:19:26

同学你好,建议你贴出复制后的完整代码,方便老师更好更快的解决问题。如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 提问者 天天4244770 #1
    已上传完整代码
    2019-04-10 10:27:39
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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