老师,这个代码被我改了,跑不到20个呢?

老师,这个代码被我改了,跑不到20个呢?

package com.imooc.weather;

public class WeatherTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Weather w=new Weather();
		GenerateWeather g=new GenerateWeather(w);
		ReadWeather r=new ReadWeather(w);
		Thread t1=new Thread(g);
		Thread t2=new Thread(r);
		t1.start();
		t2.start();
		
	}

}


package com.imooc.weather;

public class Weather {
	private int temperature;
	private int humidity;
	private boolean flag=false;
	
	public Weather() {}
	
	public Weather(int temperature, int humidity,boolean flag) {
		super();
		this.temperature = temperature;
		this.humidity = humidity;
		this.flag = flag;
	}
	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 synchronized void generate() {
		int n=0,y=0;
		if(!flag) {
			n=(int)(Math.random()*40);
			setTemperature(n);
			y=(int)(Math.random()*100);
			setHumidity(y);
			System.out.println("生成"+n+"天气"+y+"温度");
			flag=true;
		}else {
			try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			}
		}
		notifyAll();
	}
	
	public synchronized void read() {
		if(flag) {
			System.out.println("读取"+getTemperature()+"天气"+getHumidity()+"温度");
			flag=false;
		}else {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		notifyAll();
		
		//System.out.println("读取"+getTemperature()+"天气"+getHumidity()+"温度");
		//getTemperature();
		//getHumidity();
	}
	
	
	@Override
	public String toString() {
		return "温度: " + temperature + "湿度:" + humidity;
	}
	
	
	
	
}




package com.imooc.weather;

public class ReadWeather implements Runnable{
	Weather weather;
	
	public ReadWeather(Weather weather) {
		super();
		this.weather = weather;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<=20;i++) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		weather.read();
		}
		
	}
	
}




package com.imooc.weather;

public class GenerateWeather implements Runnable{
	Weather weather;

	public GenerateWeather(Weather weather) {
		super();
		this.weather = weather;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
//		int n=0 ,y=0;
		for(int i=0;i<=20;i++) {
//			n=(int)(Math.random()*40);
//			weather.setTemperature(n);
//			y=(int)(Math.random()*100);
//			weather.setHumidity(y);
			
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			weather.generate();
		}
		
	}
	
	
}


正在回答

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

1回答

同学你好,老师这里测试同学的代码,有可能在最后一对数据输出之后,再输出一次生成的数据,但是没有对应的读取数据。

出现这个问题的原因是同学的flag改变代码写在了if结构中,这样当第一次执行时如果是read线程,那么这次是什么都没做然后进入循环的下一次,相当于消耗了一次读取的机会,就会导致无法成对出现。

祝学习愉快~


  • 热爱编程学习 提问者 #1

    老师我把代码改了,放uf外面了,为啥一直运行生成呢?



    package com.imooc.weather;


    public class Weather {

    private int temperature;

    private int humidity;

    private boolean flag=false;

    public Weather() {}

    public Weather(int temperature, int humidity,boolean flag) {

    super();

    this.temperature = temperature;

    this.humidity = humidity;

    this.flag = flag;

    }

    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 synchronized void generate() {

    int n=0,y=0;

    if(!flag) {

    n=(int)(Math.random()*40);

    setTemperature(n);

    y=(int)(Math.random()*100);

    setHumidity(y);

    System.out.println("生成"+n+"天气"+y+"温度");

    }else {

    try {

    wait();

    } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    flag=true;

    notifyAll();

    }

    public synchronized void read() {

    if(flag) {

    System.out.println("读取"+getTemperature()+"天气"+getHumidity()+"温度");

    }else {

    try {

    wait();

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    }

    flag=false;

    notifyAll();

    }

    @Override

    public String toString() {

    return "温度: " + temperature + "湿度:" + humidity;

    }

    }




    2022-03-14 21:35:46
  • 同学你好,生产者与消费者模型的逻辑是:

    1.如果有产品,则生产者不进行生产。生产者进行等待。

    2.如果有产品,则消费者进行消费。消费者唤醒生产者。

    3.如果没有产品,则生产者进行生产。生产者唤醒消费者。

    4.如果没有产品,则消费者不进行消费。消费者进行等待。

    上面的描述中,是否有产品被布尔类型变量表示,而是否应该等待则是if结构中的wait方法。此时可以发现,if结构中应当只有wait的代码,而在if之后才是生产与消费的代码。并且在生产或者消费之后,对另一方进行唤醒。

    建议同学复习一下前面的课程,并再次编码,老师这里将修改后的代码提供给同学。

    祝学习愉快~

    public class Weather {
    	private int temperature;
    	private int humidity;
    	private boolean flag = true;
    
    	public Weather() {
    	}
    
    	public Weather(int temperature, int humidity, boolean flag) {
    		super();
    		this.temperature = temperature;
    		this.humidity = humidity;
    		this.flag = flag;
    	}
    
    	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 synchronized void generate() {
    		int n = 0, y = 0;
    		if (!flag) {
    			try {
    				wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		n = (int) (Math.random() * 40);
    		setTemperature(n);
    		y = (int) (Math.random() * 100);
    		setHumidity(y);
    		System.out.println("生成" + n + "天气" + y + "温度");
    
    		flag = false;
    		notifyAll();
    	}
    
    	public synchronized void read() {
    		if (flag) {
    			try {
    				wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		System.out.println("读取" + getTemperature() + "天气" + getHumidity() + "温度");
    		flag = true;
    		notifyAll();
    	}
    
    	@Override
    	public String toString() {
    		return "温度: " + temperature + "湿度:" + humidity;
    	}
    
    }


    2022-03-15 09:47:43
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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