老师怎么用Boolean判断并同步输出

老师怎么用Boolean判断并同步输出

public class Weather {

//属性

private  int temperature;

private int hurmidity;

boolean flag=false;

//setter和getter方法

public int getTemperature() {

return temperature;

}

public void setTemperature(int temperature) {

this.temperature = temperature;

}

public int getHurmidity() {

return hurmidity;

}

public void setHurmidity(int hurmidity) {

this.hurmidity = hurmidity;

}

//生产天气数据的方法;

public synchronized void generate() {


this.setTemperature((int)(Math.random()*40));

this.setHurmidity((int)(Math.random()*100));

// System.out.println(this.getTemperature()+" "+this.getHurmidity());

System.out.println("生成天气数据"+this.toString());

}

//读取天气数据的方法:

public synchronized void read() {


System.out.println("读取天气数据" + this.toString());

this.generate();

this.getHurmidity();

this.getTemperature();

}

//重写toString方法

@Override

public String toString() {

return "[温度:" + this.getTemperature() + ", 湿度" + this.getHurmidity()+ "]";

}



}

public class GenerateWeather implements Runnable {

Weather weather;

//构造方法传入weather对象

GenerateWeather(Weather weather){

this.weather=weather;

}

@Override

public void run() {

// TODO Auto-generated method stub

for(int i=1;i<=100;i++) {

weather.generate();

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


}


public class ReadWeather implements Runnable {

Weather weather;


ReadWeather(Weather weather) {

this.weather = weather;

}


@Override

public void run() {

// TODO Auto-generated method stub

for (int i = 1; i <= 100; i++) {

weather.read();

try {

Thread.sleep(500);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


}


public class WeatherTest {

public static void main(String[] args) {

Weather weather=new Weather();

//测试:随机产生的天气数据

// for(int i=1;i<=100;i++) {

// weather.generate();

// System.out.println(i);

// }

//测试:生产天气线程对象

new Thread(new GenerateWeather(weather)).start();

//测试:读取天气线程对象

new Thread(new ReadWeather(weather)).start();

}


}


正在回答

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

3回答

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

读数据时,直接调用一次输出就可以了。你不需要去generate()生成数据也不要再次get数据了。方法的职责要单一。祝:学习愉快

  • 慕雪2495638 提问者 #1
    非常感谢,完美解决了
    2020-03-12 11:23:15
提问者 慕雪2495638 2020-03-11 18:19:34

public class Weather {

//属性

private  int temperature;

private int hurmidity;

boolean flag=false;

//setter和getter方法

public int getTemperature() {

return temperature;

}

public void setTemperature(int temperature) {

this.temperature = temperature;

}

public int getHurmidity() {

return hurmidity;

}

public void setHurmidity(int hurmidity) {

this.hurmidity = hurmidity;

}

//生产天气数据的方法;

public synchronized void generate() {

if(flag) {

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}else {

this.setTemperature((int)(Math.random()*40));

this.setHurmidity((int)(Math.random()*100));

// System.out.println(this.getTemperature()+" "+this.getHurmidity());

System.out.println("生成天气数据"+this.toString());

flag=true;

notifyAll();

}

}

//读取天气数据的方法:

public synchronized void read() {

if(!flag) {

try {

wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}else {

System.out.println("读取天气数据" + this.toString());

this.generate();

this.getHurmidity();

this.getTemperature();

flag=false;

notifyAll();

}

}

//重写toString方法

@Override

public String toString() {

return "[温度:" + this.getTemperature() + ", 湿度" + this.getHurmidity()+ "]";

}



}

是这样添加么,我这么只输出一条信息就卡死了http://img1.sycdn.imooc.com//climg/5e68bb2f09300ffa04940225.jpg

  • 不要加else。把else去掉。
    2020-03-11 18:40:23
  • 提问者 慕雪2495638 回复 好帮手慕雪 #2
    package com.imooc.weatherProject; public class Weather { //属性 private int temperature; private int hurmidity; boolean flag=false; //setter和getter方法 public int getTemperature() { return temperature; } public void setTemperature(int temperature) { this.temperature = temperature; } public int getHurmidity() { return hurmidity; } public void setHurmidity(int hurmidity) { this.hurmidity = hurmidity; } //生产天气数据的方法; public synchronized void generate() { if(flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } this.setTemperature((int)(Math.random()*40)); this.setHurmidity((int)(Math.random()*100)); // System.out.println(this.getTemperature()+" "+this.getHurmidity()); System.out.println("生成天气数据"+this.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("读取天气数据" + this.toString()); this.generate(); this.getHurmidity(); this.getTemperature(); flag=false; notifyAll(); } //重写toString方法 @Override public String toString() { return "[温度:" + this.getTemperature() + ", 湿度" + this.getHurmidity()+ "]"; } } 我取消了还是只输出了一条信息,就是停止显示了
    2020-03-12 06:32:44
好帮手慕雪 2020-03-11 18:12:19

generate()和read()中进来之后先判断flag的值。generate()flag为真时,先等待。否则进行生产数据,并更改flag为真并且唤醒其它线程。read()flag为假时,先等待。否则进行读数据,并更改flag为假并且唤醒其它线程。祝:学习愉快

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

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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