生成天气数据和读取天气数据的程序运行结果(线程问题)有异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | //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回答
同学你好,修改后的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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 + "]" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //测试类 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积分~
来为老师/同学的回答评分吧