老师怎么用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();
}
}
正在回答
读数据时,直接调用一次输出就可以了。你不需要去generate()生成数据也不要再次get数据了。方法的职责要单一。祝:学习愉快
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()+ "]";
}
}
是这样添加么,我这么只输出一条信息就卡死了
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星