5-3编程练习

来源:5-3 自由编程

慕神0457710

2020-08-22 04:41:26

public class WeatherTest {

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

	}

}
public class Weather {
	private int temperatrue;
	private int humidity;
	private boolean flag = false;
	
	public int getTemperatrue() {
		return temperatrue;
	}
	public void setTemperatrue(int temperatrue) {
		this.temperatrue = temperatrue;
	}
	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();
			}
		}
		setTemperatrue((int)(Math.random()*40));
		setHumidity((int)(Math.random()*100));
		System.out.println("生成天气数据"+toString());
		flag = true;
		notifyAll();
	}
	
	public synchronized void read () {
		if (!flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("读取天气数据"+toString());
		flag = false;
		notifyAll();
	}
	
	@Override
	public String toString() {
		return "[温度:" + temperatrue + ", 湿度:" + humidity+ "]";
	}
	
}
public class ReadWeather implements Runnable{
	Weather wea;
	
	public ReadWeather(Weather wea) {
		this.wea = wea;
	}

	@Override
	public void run() {
		for (int i=0;i<100;i++) {
			wea.read();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
public class GenerateWeather implements Runnable{
	Weather wea;
	
	public GenerateWeather(Weather wea) {
		this.wea = wea;
	}

	@Override
	public void run() {
		for (int i=0;i<100;i++) {
			wea.generate();
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}


写回答

2回答

好帮手慕阿园

2020-08-22

同学你好,测试代码是正确的,完成的很棒!继续加油!

祝学习愉快~

0

小乌兹cxy

2022-01-28

{
    ([] args) {
        = Weather();
        Thread(GenerateWeather()).start();
        Thread(ReadWeather()).start();
    }
}
{
    ;
    ;
    = ;

    () {
        ;
    }

    (temperature) {
        .= temperature;
    }

    () {
        ;
    }

    (humidity) {
        .= humidity;
    }
    (){
        (){
            {
                wait();
            } (e) {
                e.printStackTrace();
            }
        }
        .setTemperature(Random().nextInt());
        .setHumidity(Random().nextInt());
        ..println(+ .toString());
        = ;
    }
    (){
        (!){
            {
                wait();
            } (e) {
                e.printStackTrace();
            }
        }
        ..println(+ .toString());
        = ;
        notifyAll();
    }
    (){
        + .getTemperature() + + .getHumidity() + ;
    }
}
{
    ;
    (weather){
        .= weather;
    }
    (){
        (i = ; i < ; i++)
            {
            .read();
            {
                .();
            } (e) {
                e.printStackTrace();
            }
            i++;
        }
    }
}
{
    ;
    (weather){
        .= weather;
    }
    (){
        (i = ; i < ; i++) {
            .generate();
            {
                .();
            } (e) {
                e.printStackTrace();
            }
            i++;
        }
    }
}

老师我的代码总是运行出来是一个两个就不往下运行了

https://img.mukewang.com/climg/61f3f084096cf5e907360176.jpg


0

0 学习 · 16556 问题

查看课程