关闭

生产者与消费者模型 wait() · notify() 方法的使用

标签: notifywait
179人阅读 评论(0) 收藏 举报
分类:

模型说明:

Cooker生产食物, 把食物放到盘子中,如果盘子中没有食物就继续生产。
客户消费食物,如果盘子中有食物就把食物吃掉。

Object的wait() notify() 方法使用说明:
其中wait方法必须在 Object的 synchronized块中执行
这里写图片描述

public void make(){
        if(d.isEmpty()){
            int index = ran.nextInt(foods.length);
            String f = foods[index];
            System.out.println(name + "制作了"+f);
            d.setFood(f);
            d.setEmpty(false);
            //不能依赖于sleep来控制线程
            /*try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/
        }
    }

Cooker类

package produce;

import java.util.Random;

public class Cooker implements Runnable{
    private String name;
    private String[] foods;
    private Disk d;
    private static Random ran = new Random();

    public Cooker(String name, Disk d) {
        super();
        this.name = name;
        this.foods = new String[]{"大米","馒头","包子","豆沙包","鱼","牛肉"};
        this.d = d;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getFoods() {
        return foods;
    }
    public void setFoods(String[] foods) {
        this.foods = foods;
    }
    public Disk getD() {
        return d;
    }
    public void setD(Disk d) {
        this.d = d;
    }

    public void make(){
        synchronized (d) {
            if(d.isEmpty()){
                int index = ran.nextInt(foods.length);
                String f = foods[index];
                System.out.println(name + "制作了"+f);
                d.setFood(f);
                d.setEmpty(false);
                d.notify();
                try {
                    d.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                try {
                    d.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void run() {
        for(int i=0; i<20; i++){
            make();
        }
        d.setEnd(true);
    }

}

Customer 类

package produce;

public class Customer implements Runnable{
    private String name;
    private Disk d;
    public Customer(String name, Disk d) {
        super();
        this.name = name;
        this.d = d;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Disk getD() {
        return d;
    }
    public void setD(Disk d) {
        this.d = d;
    }

    public void eat(){
        synchronized (d) {
            if(!d.isEmpty()){
                String food = d.getFood();
                System.out.println(name + "正在享用"+food);
                d.setEmpty(true);
                d.notify();
                try {
                    d.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                try {
                    d.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
    @Override
    public void run() {
        while(true){
            if(d.isEnd()){
                break;
            }
            eat();
        }
    }
}

Disk 类

package produce;

public class Disk {
    private String food;
    private boolean empty;
    private boolean end;


    public Disk(boolean end) {
        super();
        this.end = end;
    }
    public String getFood() {
        return food;
    }
    public boolean isEnd() {
        return end;
    }
    public void setEnd(boolean end) {
        this.end = end;
    }
    public void setFood(String food) {
        this.food = food;
    }
    public boolean isEmpty() {
        return empty;
    }
    public void setEmpty(boolean empty) {
        this.empty = empty;
    }

}

Test 类

package produce;

public class Test {
    public static void main(String[] args) {
        Disk d = new Disk(false);
        d.setEmpty(true);
        Cooker co = new Cooker("刘大厨",d);
        Customer cu = new Customer("王小胖",d);

        new Thread(co).start();
        new Thread(cu).start();
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    最新评论