请老师帮忙解决一下,这样编码是否合理,谢谢:

请老师帮忙解决一下,这样编码是否合理,谢谢:

第一个类:----------------------------------------

package com.misi.imooc.tools.thread.work6;

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(int a, int b) {
        if (flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.setTemperature(a);
        this.setHumidity(b);
        System.out.println("生成:[温度:" + a + ",湿度:" + b + "]");
        flag = true;
        notifyAll();

    }

    public synchronized void read() {
        if (!flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("获取:[温度:" + getTemperature() + ",湿度:"
                + getHumidity() + "]");
        flag = false;
        notifyAll();
    }

    @Override
    public String toString() {
        return "[温度:" + getTemperature() + ",湿度:" + getHumidity() + "]";
    }

}

第二个类:---------------------------------

package com.misi.imooc.tools.thread.work6;

public class GenerateWeather implements Runnable {

    Weather weather;

    public GenerateWeather(Weather weather) {
        super();
        this.weather = weather;
    }

    public GenerateWeather() {
        super();
    }

    public void run() {

        while (true) {
            int a = (int) (Math.round(Math.random() * 40));
            int b = (int) (Math.round(Math.random() * 100));
            weather.generate(a, b);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

第三个类:----------------------------------------------

package com.misi.imooc.tools.thread.work6;

public class ReadWeather implements Runnable {

    Weather weather;

    public ReadWeather(Weather weather) {
        super();
        this.weather = weather;
    }

    public ReadWeather() {
        super();
    }

    public void run() {
        while (true) {
            weather.read();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

测试类:-----------------------------------

package com.misi.imooc.tools.thread.work6;

public class Test {
    
    public static void main(String[] args) {
        
        Weather we=new Weather();
        new Thread(new GenerateWeather(we)).start();
        new Thread(new ReadWeather(we)).start();
        
    }
    
}

正在回答 回答被采纳积分+1

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

2回答
Tender10 2018-06-22 16:47:08

代码中需要注意读取天气和生成天气的休眠时间点哦,每次生成天气数据需要5秒的时间,每次读取数据需要0.1秒的时间。

慕慕6012600 2018-06-22 16:29:58

代码没问题~

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

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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