5-3的一些问题 为什么在 实现runnable类种方法的语句打印输出也会是随机的,而非确定的
老师,您好,我在天气类的作业中,添加了 一句输出语句,打印输出这是第几次读天气/生成天气。 这些输出语句也是位置不确定的。想知道是为什么吖。
下面是我的作业的完整代码和输出
Weather 类
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 | package com.imooc.thread; import java.util.Random; public class Weather { private int temperature; private int humidity; private boolean flag = false ; // True to read // Constructor public Weather() { // TODO Auto-generated constructor stub } public Weather( int temp, int humidity) { this .setHumidity(humidity); this .setTemperature(temp); } // Getters and Setters 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 "Weather data [temperature=" + temperature + ", humidity=" + humidity + "]" ; } // Methods (read and generate methods must be synchronized) // They can not be interrupted by other when one thread calls them public synchronized void read() { if ( this .flag) { //if it is the time to read the weather read them System.out.println( "read Weather feature : " + this .toString()); //update the flag and notify other thread. flag = false ; this .notifyAll(); } else { try { this .wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void generate() { synchronized ( this ) { if (! this .flag) { Random random = new Random(); this .setHumidity(random.nextInt( 41 )); this .setTemperature(random.nextInt( 101 )); System.out.println( "Generate Weather feature : " + this .toString()); //After generate weather, modify the flag and notify other threads this .setFlag( true ); this .notifyAll(); } else { //if this time is not to generate weather, releases the lock on the object // so that another thread can jump in and acquire a lock. try { this .wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
两个实现runnable 接口的类
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 | package com.imooc.thread; public class GenerateWeather implements Runnable { private Weather weather; public GenerateWeather() { // TODO Auto-generated constructor stub } public GenerateWeather(Weather weather) { super (); this .setWeather(weather); } public Weather getWeather() { return weather; } public void setWeather(Weather weather) { this .weather = weather; } @Override public void run() { // TODO Auto-generated method stub for ( int i = 0 ; i< 100 ; i++) { weather.generate(); System.out.println( "第" +i+ "次写" ); try { Thread.sleep( 100 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
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 | package com.imooc.thread; public class ReadWeather implements Runnable { private Weather weather; public ReadWeather() { // TODO Auto-generated constructor stub } public ReadWeather(Weather weather) { super (); this .weather = weather; } public Weather getWeather() { return weather; } public void setWeather(Weather weather) { this .weather = weather; } @Override public void run() { // TODO Auto-generated method stub for ( int i = 0 ; i< 100 ; i++) { weather.read(); System.out.println( "第" +i+ "次读" ); try { Thread.sleep( 100 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
测试代码及输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.imooc.thread; public class WeatherTest { public static void main(String[] args) { // TODO Auto-generated method stub Weather weather = new Weather(); Thread readWeather = new Thread( new ReadWeather(weather)); Thread geneWeather = new Thread( new GenerateWeather(weather)); readWeather.start(); geneWeather.start(); } } |
输出:
Generate Weather feature : Weather data [temperature=56, humidity=2]
第0次读
第0次写
read Weather feature : Weather data [temperature=56, humidity=2]
第1次写
第1次读
Generate Weather feature : Weather data [temperature=32, humidity=34]
第2次写
第2次读
read Weather feature : Weather data [temperature=32, humidity=34]
第3次读
Generate Weather feature : Weather data [temperature=59, humidity=19]
第3次写
read Weather feature : Weather data [temperature=59, humidity=19]
Generate Weather feature : Weather data [temperature=86, humidity=35]
第4次写
第4次读
read Weather feature : Weather data [temperature=86, humidity=35]
第5次读
Generate Weather feature : Weather data [temperature=70, humidity=19]
第5次写
read Weather feature : Weather data [temperature=70, humidity=19]
第6次写
第6次读
Generate Weather feature : Weather data [temperature=32, humidity=29]
第7次读
第7次写
read Weather feature : Weather data [temperature=32, humidity=29]
第8次写
第8次读
Generate Weather feature : Weather data [temperature=4, humidity=5]
第9次读
正在回答
两个线程的执行顺序就是随机的。但作业要求是,你增加逻辑代码后,不要让它随机,让它固定下来。先产生天气数据再读取天气数据。所以你看你加了flag后Generate Weather feature :与read Weather feature顺序是固定的。你无法固定线程顺序,但你可以固定线程中某代码的执行顺序呀,这不就达到目的了嘛。祝:学习愉快
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧