生成天气数据和读取天气数据的程序运行结果(线程问题)有异常
//Weather类 package com.imooc; import java.util.Random; public class Weather { private int temperature; private int humidity; private Boolean flag = false; 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 getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } public synchronized void generate() { int temRandom = new Random().nextInt(41); int humRandom = new Random().nextInt(41); setTemperature(temRandom); setHumidity(humRandom); System.out.println("生成天气数据:" + this); setFlag(true); } public synchronized void read() { System.out.println("读取天气数据:" + this); setFlag(false); } @Override public String toString() { return "[温度:" + temperature + ", 湿度" + humidity + "]"; } } //GenerateWeather类 package com.imooc; public class GenerateWeather implements Runnable { private Weather weather; public GenerateWeather(Weather weather) { this.weather = weather; } @Override public void run() { int i = 0; while(i < 100) { // notifyAll(); //判断是否生成数据,如果生成了数据,就通知并等待读取数据,否则生成数据 if(weather.getFlag()) { try { notifyAll(); wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { weather.generate(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } //readWeather类 package com.imooc; public class ReadWeather implements Runnable { private Weather weather; public ReadWeather(Weather weather) { this.weather = weather; } @Override public void run() { for(int i = 0;i < 100;i++) { //判断是否读取数据,如果读取了数据,就通知并等待数据生成,否则读取数据 if(!weather.getFlag()) { try { notifyAll(); wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { weather.read(); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } //测试类 package com.imooc; public class ThreadTest { public static void main(String[] args) { Weather weather = new Weather(); GenerateWeather genWeather = new GenerateWeather(weather); ReadWeather readWeather = new ReadWeather(weather); Thread t1 = new Thread(genWeather); Thread t2 = new Thread(readWeather); t1.start(); t2.start(); } }
//这是运行结果
2
收起
正在回答
2回答
同学你好,修改后的代码如下:
package com.imooc; import java.util.Random; public class Weather { private int temperature; private int humidity; private Boolean flag = false; 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 getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } public synchronized void generate() { if (!flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("读取天气数据" + this); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } flag = false; notifyAll(); } public synchronized void read() { 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() * 100)); System.out.println("生成天气数据" + this); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } flag = true; notifyAll(); } @Override public String toString() { return "[温度:" + temperature + ", 湿度" + humidity + "]"; } }
package com.imooc; public class ReadWeather implements Runnable { private Weather weather; public ReadWeather(Weather weather) { this.weather = weather; } @Override public void run() { for(int i=0;i<100;i++) { weather.read(); } } }
package com.imooc; public class GenerateWeather implements Runnable { private Weather weather; public GenerateWeather(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0; i < 100; i++) { weather.generate(); } } }
//测试类 package com.imooc; public class ThreadTest { public static void main(String[] args) { Weather weather = new Weather(); GenerateWeather genWeather = new GenerateWeather(weather); ReadWeather readWeather = new ReadWeather(weather); Thread t1 = new Thread(genWeather); Thread t2 = new Thread(readWeather); t1.start(); t2.start(); } }
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
好帮手慕酷酷
2019-08-13 11:27:34
同学你好,报错的原因是natifyAll()方法需要放在wait()方法后进行的,也就是说wait阻塞当前线程后,才可以调用notifyAll通知其他线程运行哦~另外,同学得run()方法整体思路有点问题,建议同学可以参考其他同学的代码进行理解 http://class.imooc.com/course/qadetail/39309
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星