5-3作业问题
package com.imooc.xc;
public class Weather {
private int temperature;//温度
private int humidity;//湿度
boolean flag=false;
public synchronized void generate(){
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setTemperature((int)(Math.random()*40+1));
this.setHumidity((int)(Math.random()*100+1));
flag=true;
System.out.println("产生天气数据[温度:"+this.temperature+",湿度:"+this.humidity+"]");
notifyAll();
}
}
public synchronized void read(){
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.getTemperature();
this.getHumidity();
System.out.println("读取天气数据[温度:"+temperature+",湿度:"+humidity+"]");
flag=false;
notifyAll();
}
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;
}
}package com.imooc.xc;
public class GenerateWeather implements Runnable{
Weather weather;
GenerateWeather(Weather weather){
this.weather=weather;
}
@Override
public void run() {
int i=0;
while(i<100){
weather.generate();
i++;
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.imooc.xc;
public class ReadWeather implements Runnable{
Weather weather;
ReadWeather(Weather weather){
this.weather=weather;
}
@Override
public void run() {
int i=0;
while(i<100){
weather.read();
i++;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.imooc.xc;
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();
}
}
错在哪里,为啥输出不来啊?求解
正在回答 回答被采纳积分+1
public synchronized void generate() {
//flag为false的时候才生成,当有数据时为true时等待
if (flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 模拟获取温度,使用随机数获取0-40度的温度
int temperature1 = (int) (Math.random() * 40+1);
setTemperature(temperature1);
// 模拟获取湿度,使用随机数获取0-100的湿度
int humidity1 = (int) (Math.random() * 100+1);
setHumidity(humidity1);
// 输出得到的温度和湿度
System.out.println("生成天气数据 [温度:" + temperature1 + ", 湿度" + humidity1 + "]");
flag=true;
notifyAll();
}
// 计算机读取温湿度值得方法
public synchronized void read() {
//当flag为false,也就是没有生成新的天气数据时,则等待
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 读取温度
int temperature1 = getTemperature();
// 读取湿度
int humidity1 = getHumidity();
// 输出得到的温度和湿度
System.out.println("读取天气数据 [温度:" + temperature1 + ", 湿度" + humidity1 + "]");
flag=false;
notifyAll();
}
@Override
public String toString() {
return "天气 [温度:" + temperature + ", 湿度" + humidity + "]";
}Weather类生成天气和读取天气的代码参考如上,并且记得重写toString()方法,同时读取天气类和生成天气类的休眠代码的位置得调整一下。
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星