老师帮忙看看哪里错了

正在回答 回答被采纳积分+1

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

2回答
提问者 慕斯9009508 2017-11-09 22:27:18
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
public class Weather {
    private int temperature;
    private int humidtiy;
    boolean flag=false;
    public int getTemperature() {
        return temperature;
    }
    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }
    public int getHumidtiy() {
        return humidtiy;
    }
    public void setHumidtiy(int humidtiy) {
        this.humidtiy = humidtiy;
    }
    public synchronized void generate(int humodity,int temperature){
        if(!flag){
            System.out.println("生成数据"+this.toString());
            this.setHumidtiy(humidtiy);
            this.setTemperature(temperature);
            flag=true;
        }
    }
    public synchronized void read(){
        if(flag){
            System.out.println("读取数据"+this.toString());    
            flag=false;
        }
    }
    @Override
    public String toString() {
        return "[温度=" + temperature + ", 湿度=" + humidtiy + "]";
    }
     
}
import java.util.Random;
 
public class GenerateWeather implements Runnable {
    Weather weather;
    public GenerateWeather(Weather weather){
        this.weather=weather;
    }
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            int s=(int)(Math.random())*30;
            int t=(int)(Math.random())*40;
            weather.generate(s,t);
            try {
                Thread.currentThread().sleep(5000);
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Thread.currentThread().notifyAll();
        }
         
    }
 
}
 
public class ReadWeather implements Runnable {
    Weather weather;
    public ReadWeather(Weather weather){
        this.weather=weather;
    }
    @Override
    public void run() {
        for(int i=0;i<100;i++){
            weather.read();
            try {
                Thread.currentThread().sleep(10);
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Thread.currentThread().notifyAll();
        }
         
    }
     
}
package com.java.weather;
 
public class Test {
 
    public static void main(String[] args) {
        Weather weather=new Weather();
        GenerateWeather gweather=new GenerateWeather(weather);
        ReadWeather rweather=new ReadWeather(weather);
        new Thread(gweather).start();
        new Thread(rweather).start();
    }
 
}


一叶知秋519 2017-11-09 19:04:48

你好,建议你把全部代码都贴出来,不要截图;另外把报错信息的截图截全一点,方便帮助你解决问题。祝学习愉快~

  • 提问者 慕斯9009508 #1
    老师已经发了,帮忙看一下!谢谢
    2017-11-09 22:27:58
  • 好帮手慕珊 回复 提问者 慕斯9009508 #2
    notify()或notifyAll()方法是和wait()方法成对使用的,也就是只能唤醒调用wait()方法进行等待的线程,而你的代码中没有wait()方法,所以会出错。wait()和notify()或notifyAll()方法的调用写在weather类里,可以参考一下线程间通信这一节的代码。
    2017-11-10 10:32:31
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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