代码出错,循环一百次一下就输出完了,还有生成天气信息的时候,天气对象输出了两次

代码出错,循环一百次一下就输出完了,还有生成天气信息的时候,天气对象输出了两次

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.imooc.task.one.bu3.a7xianchen.a5_3;
 
import java.util.concurrent.ThreadLocalRandom;
 
/**
 * 天气类Weather,用于温度和湿度数据的存放和读取
 
 * @author 15136
 *
 */
public class Weather {
 
    private int temperature; // 温度
    private int humidity; // 湿度
    private boolean flag; // 判断生成还是读取天气信息
 
    /**
     * 生成天气数据的方法
     */
    public synchronized void generate() {
        //进行判断当前是否可以生成天气信息:为假则允许,为真则不允许
        if (flag) {
            try {
                wait();
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //开始生成天气数据
        this.setTemperature(ThreadLocalRandom.current().nextInt(41));
        this.setHumidity(ThreadLocalRandom.current().nextInt(101));
        System.out.println("生成天气数据" this.toString());
        this.setFlag(true);
        notifyAll();
    }
 
    /**
     * 读取天气数据的方法
     */
    public synchronized void read() {
        //进行判断当前是否可以生成天气信息:为真则允许,为假则不允许
        if (!flag) {
            try {
                wait();
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(this);
        System.out.println("读取天气数据" this.toString());
        this.setFlag(false);
        notifyAll();
    }
 
    // 成员属性的封装:get/set方法
    public int getTemperature() {
        return temperature;
    }
 
    public int getHumidity() {
        return humidity;
    }
 
    public boolean isFlag() {
        return flag;
    }
 
    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }
 
    public void setHumidity(int humidity) {
        this.humidity = humidity;
    }
 
    public void setFlag(boolean flag) {
        this.flag = flag;
    }
 
    @Override
    public String toString() {
        return "[温度:" this.getTemperature() + ",湿度" this.getHumidity() + "]";
    }
 
}
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
package com.imooc.task.one.bu3.a7xianchen.a5_3;
 
/**
 * 设计一个线程类ReadWeather,用于读取天气数据。
 * 模拟读取100次天气数据,每次读取数据需要0.1秒的时间
 * @author 15136
 *
 */
public class GenerateWeather extends Thread {
 
    //天气对象
    private Weather weather;
     
    //无参构造方法
    public GenerateWeather() {
        super();
    }
    //有参构造方法
    public GenerateWeather(Weather weather) {
        super();
        this.weather = weather;
    }
 
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            this.getWeather().generate();
            try {
                Thread.sleep(100);
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
     
    //成员属性的get/set方法
    public Weather getWeather() {
        return weather;
    }
    public void setWeather(Weather weather) {
        this.weather = weather;
    }
     
}
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
package com.imooc.task.one.bu3.a7xianchen.a5_3;
 
public class ReadWeather extends Thread {
 
    //天气对象
    private Weather weather;
     
    //无参构造方法
    public ReadWeather() {
        super();
    }
    //有参构造方法
    public ReadWeather(Weather weather) {
        super();
        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();
            }
        }
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.imooc.task.one.bu3.a7xianchen.a5_3;
 
/**
 * 模拟生成和读取数据的过程,要求实现生成一次,紧接着进行读取,不能出现不同步的情况
 * @author 15136
 *
 */
public class WeatherTest {
     
    public static void main(String[] args) {
         
        Weather weather = new Weather();
        GenerateWeather generateWeather = new GenerateWeather(weather);
        ReadWeather readWeather = new ReadWeather(weather);
        Thread thread01 = new Thread(generateWeather);
        Thread thread02 = new Thread(readWeather);
        thread01.start();
        thread02.start();
         
    }
 
}


正在回答

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

3回答

同学你好,根据题目要求,生成的天气数据需要5秒,但是同学的生成天气的线程,却休眠时间却只有0.1秒哦

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

同学的生成天气的线程与读取天气的线程都是只休眠了100毫秒,也就是0.1秒哦!

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

所以建议同学将生成天气的线程修改为休眠5秒也就是5000毫秒,就即符合题目要求,也符合同学的计算了哦!例如

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

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

好帮手慕小班 2019-05-31 21:00:09

同学你好,1、生成和读取多输出一次是因为在read方法中,输出了两次,例如:

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

所以这里写下面的那个输出语句就可以了哦!

2、循环100次以下输出完,是因为sleep休眠时间太短了只有100毫秒,这里修改成1000,程序就会慢下来

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

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

  • 提问者 易表非凡 #1
    1、设计一个天气类Weather,用于温度和湿度数据的存放和读取。 2、设计一个线程类GenerateWeather,用于生成天气数据。模拟生成100次天气数据,每次生成天气数据需要5秒的时间。
    2019-06-01 07:33:38
  • 提问者 易表非凡 #2
    助教老师您好,按照题目要求:每次生成天气需要5秒,读取天气需要0.1秒,连续生成读取100次,程序需要运行(5+0.1)*100=510秒 但我的代码几十秒就运行完成了
    2019-06-01 07:40:48
提问者 易表非凡 2019-05-31 18:35:35

各位大神,求解答,这是运行效果图

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

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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