5-1線程間通信課後練習疑問
package weather; public class Weather { private double hot; private double wet; public Weather() { } public Weather(double hot, double wet) { setWet(wet); setHot(hot); } public double getHot() { return hot; } public void setHot(double hot) { this.hot = hot; } public double getWet() { return wet; } public void setWet(double wet) { this.wet = wet; } public void create() { System.out.println("天氣數據" + '\n' + "溫度:" + this.getHot() + '\n' + "溼度:" + this.getWet() + "%"); } }
package weather; public class Read implements Runnable { Weather weather; public Read(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0; i < 100; i++) { weather.create(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package weather; public class Create implements Runnable { Weather weather; public Create(Weather weather) { this.weather = weather; } @Override public void run() { for (int i = 0, n = 90; i < 50; i++, n -= 5) { weather.setHot(i); weather.setWet(n); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package weather; public class Test { public static void main(String[] args) { Weather weather = new Weather(); new Thread(new Create(weather)).start(); new Thread(new Read(weather)).start(); } }
並未正常終止程序
26
收起
正在回答 回答被采纳积分+1
1回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星