zoukankan      html  css  js  c++  java
  • 线程-读取天气数据

    需求分析

    1、设计一个天气类Weather,用于温度和湿度数据的存放和读取。

    2、设计一个线程类GenerateWeather,用于生成天气数据。模拟生成100次天气数据,每次生成天气数据需要5秒的时间。

    3、设计一个线程类ReadWeather,用于读取天气数据。模拟读取100次天气数据,每次读取数据需要0.1秒的时间。

    4、设计一个测试类WeatherTest,模拟生成和读取数据的过程,要求实现生成一次,紧接着进行读取,不能出现不同步的情况。

    详细设计:

    1、 天气类Weather,包含int类型的温度(temperature)和湿度(humidity)属性,以及布尔类型的属性flag用于判断是生成还是读取天气信息。

    方法包括:

    (1)温度和湿度属性的getter和setter方法

    (2)生成天气数据的方法public void generate()

             使用随机数获取0-40度之间的温度,0-100之间的湿度

    (3)读取天气数据的方法public void read()

    (4)重写toString()方法

    2、 生成天气线程类GenerateWeather

    属性为Weather类的对象,包括构造方法和run方法。

    3、 读取天气线程类ReadWeather

    属性为Weather类的对象,包括构造方法和run方法。

    4、 测试类WeatherTest

    在主方法中模拟生成和读取数据的过程

    代码实现:

    /**
     * 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();
                }
            }
            System.out.print("生成天气数据");
            try {
                Thread.currentThread().sleep(5000);//生成数据需要5秒钟
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int m = (int) (Math.random()*40);//随机生成0-40的随机数
            int n = (int) (Math.random()*100);//随机生成0-100的随机数
            this.setTemperature(m);
            this.setHumidity(n);
            System.out.println(this);
            flag = true;
            notifyAll();
        }
        //读取天气数据
        public synchronized void read() {
            if(!flag) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.print("读取天气数据");
            try {
                Thread.currentThread().sleep(100);//读取数据需要0.1秒
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.getTemperature();
            this.getHumidity();
            System.out.println(this);
            flag = false;
            notifyAll();
        }
        @Override
        public String toString() {
            return "[温度:" + temperature + ", 湿度:" + humidity + "]";
        }
        
        
    }
    /**
     * 生产数据线程
     */
    public class GenerateWeather implements Runnable {
        Weather weather;
        public GenerateWeather(Weather weather) {
            this.weather = weather;
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            int i = 100;
            while(i > 0) {
                weather.generate();
                i--;
            }
            
        }
    
    }
    package com.weather;
    /**
     * 读取数据线程
     */
    public class ReadWeather implements Runnable {
        Weather weather;
        public ReadWeather(Weather weather){
            this.weather = weather;
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            int i = 100;
            while(i > 0) {
                weather.read();
                i--;
            }
        }
    
    }
    //测试类
    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(); } }
  • 相关阅读:
    [国嵌攻略][152][I2C总线介绍]
    [国嵌攻略][151][nandflash驱动程序设计]
    [国嵌攻略][150][实际嵌入式系统环境搭建]
    [国嵌攻略][149][Yaffs2文件系统应用]
    [国嵌攻略][148][MTD系统架构]
    [国嵌攻略][147][简单块设备驱动设计]
    [国嵌攻略][146][块设备驱动实例分析]
    PhpStorm Git 配置
    PHP 创建重用数据库连接函数 mysqli与PDO
    【转载】php程序员:从1.5K到18K 一个程序员的5年成长之路
  • 原文地址:https://www.cnblogs.com/conglingkaishi/p/9256960.html
Copyright © 2011-2022 走看看