生成天气数据和读取天气数据的程序运行结果(线程问题)有异常

生成天气数据和读取天气数据的程序运行结果(线程问题)有异常

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();
    }
}

//这是运行结果

http://img1.sycdn.imooc.com//climg/5d51659d00015a0e07140230.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

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
    老师,您好! 将wait()方法和notifyAll()方法调换下位置,还是会出现相同的问题,下面是运行结果。 生成天气数据:[温度:24, 湿度35] Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:502) at com.imooc.ReadWeather.run(ReadWeather.java:17) at java.lang.Thread.run(Thread.java:748) Exception in thread "Thread-0" java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:502) at com.imooc.GenerateWeather.run(GenerateWeather.java:19) at java.lang.Thread.run(Thread.java:748)
    2019-08-13 12:01:20
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码