Java输入输出流练习5-3问题,烦请老师解答

来源:5-3 自由编程

weixin_慕后端4198415

2021-12-06 20:30:31

问题描述:

请问为什么我用注释的代码可以正常运行,但是调用weather里的方法就无法正常运行,只能产生一次数据,然后好像就死锁了,这是为什么?

相关代码:

package com.imooc.weather;

public class Weather {
	private int temperature;
	private int humidity;
	private boolean flag=false;
	
	public Weather() {
		
	}
	
	
	public synchronized void readWeather() {
		if(!flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("读取天气数据:" + this);
			flag=false;
			notifyAll();
		}
	}

	public synchronized void generateWeather() {
		if(flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.setTemperature((int)(Math.random()*40));
		this.setHumidity((int)(Math.random()*80));
		System.out.println("生成天气数据:" + this);
		flag=true;
		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;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	@Override
	public String toString() {
		return "[温度=" + temperature + ", 湿度=" + humidity + "]";
	}

}
package com.imooc.weather;

public class GenerateWeather implements Runnable {
	Weather w;

	public GenerateWeather(Weather w) {
		this.w = w;
	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
//			synchronized (w) {
//				if (w.isFlag()) {
//					try {
//						w.wait();
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}
//				w.setTemperature((int) (Math.random() * 40));
//				w.setHumidity((int) (Math.random() * 80));
//				System.out.println("生成天气数据:" + w);
//				w.setFlag(true);
//				w.notifyAll();
//			}
			w.generateWeather();
			
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}

	}
}
package com.imooc.weather;

public class GetWeather implements Runnable {
	Weather w;
	
	public GetWeather(Weather w) {
		this.w = w;
	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
//			synchronized (w) {
//				if (!(w.isFlag())) {
//					try {
//						w.wait();
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}
//				System.out.println("读取天气数据:" + w);
//				w.setFlag(false);
//				w.notifyAll();
//			}
			w.readWeather();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
package com.imooc.weather;

public class WeatherTest {

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

	}

}

请老师解答。


写回答

1回答

好帮手慕小蓝

2021-12-07

同学你好,同学的Weather类中readWeather方法体中,错误的将下图语句写在了if语句中,导致read方法无法修改flag和唤醒其他线程。

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

修改方式,将上图的语句移至if语句外即可。

祝学习愉快~


1

0 学习 · 9886 问题

查看课程