美文网首页
2018-12-25线程 生产者消费者模式(一)

2018-12-25线程 生产者消费者模式(一)

作者: 李春lu | 来源:发表于2018-12-25 20:06 被阅读0次

Container类(容器):

public class Container<T> {                   //容器
    private T[] arr;

    private int top = 0;

    public Container() {
        this(5);
    }

    public Container(int size) {
        this.arr = (T[]) new Object[size];
    }

    public synchronized void save(T o) {

        while (top == arr.length) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
            arr[top] = o;
            top++;
           // System.out.println("生产了第"+top+"个"+"包子");
            notify();
    }

    public synchronized T fatch() {
        while (top == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
       // System.out.println("消费了第"+top+"个包子");
        top--;
        T t=arr[top];
        notify();
        return t;
    }
    public int count(){
        return top;
    }
}

包子类:

public class BaoZi {
    private Integer id;
    private  String name;

    public BaoZi() {
    }

    public BaoZi(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "BaoZi{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

生产者(Producer):

public class Producer extends Thread{
    private Container<BaoZi> container;

    private Integer sum;

    public Producer(Container<BaoZi> container,Integer sum,String name){
        super(name);
        this.container=container;
        this.sum=sum;
    }

    @Override
    public void run() {
        String [] name={"鲜肉包","香菇包","豆沙包"};
        Random random=new Random();
        for (int i=0;i<sum;i++){
            BaoZi baoZi=new BaoZi(i+1,name[random.nextInt(3)]);
            System.out.println(Thread.currentThread().getName()+"做l一个包子"+baoZi);
            container.save(baoZi);
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

消费者(Customer):

public class Customer extends Thread {
    private Container<BaoZi> container;

    private Integer sum;

    public Customer(Container<BaoZi> container, Integer sum,String name){
        super(name);
        this.container=container;
        this.sum=sum;
    }

    @Override
    public void run() {
        Random random=new Random();
        for (int i=0;i<sum;i++){
            BaoZi baoZi=container.fatch();
            System.out.println(Thread.currentThread().getName()+"买了一个包子"+baoZi);
            try {
                Thread.sleep(2000+random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

测试类:


public class Test {
    public static void main(String[] args) {
        Container<BaoZi> container=new Container();

        Producer producer=new Producer(container,20,"我");

        Customer customer1=new Customer(container,3,"甲");
        Customer customer2=new Customer(container,5,"乙");
        Customer customer3=new Customer(container,8,"丙");
        Customer customer4=new Customer(container,4,"丁");

        producer.start();customer1.start();customer2.start();customer3.start();customer4.start();

    }
}

输出:

我做l一个包子BaoZi{id=1, name='香菇包'}
丁买了一个包子BaoZi{id=1, name='香菇包'}
我做l一个包子BaoZi{id=2, name='豆沙包'}
丙买了一个包子BaoZi{id=2, name='豆沙包'}
我做l一个包子BaoZi{id=3, name='豆沙包'}
甲买了一个包子BaoZi{id=3, name='豆沙包'}
我做l一个包子BaoZi{id=4, name='鲜肉包'}
乙买了一个包子BaoZi{id=4, name='鲜肉包'}
我做l一个包子BaoZi{id=5, name='豆沙包'}
丁买了一个包子BaoZi{id=5, name='豆沙包'}
我做l一个包子BaoZi{id=6, name='豆沙包'}
甲买了一个包子BaoZi{id=6, name='豆沙包'}
我做l一个包子BaoZi{id=7, name='豆沙包'}
丙买了一个包子BaoZi{id=7, name='豆沙包'}
我做l一个包子BaoZi{id=8, name='鲜肉包'}
乙买了一个包子BaoZi{id=8, name='鲜肉包'}
我做l一个包子BaoZi{id=9, name='香菇包'}
丁买了一个包子BaoZi{id=9, name='香菇包'}
我做l一个包子BaoZi{id=10, name='豆沙包'}
甲买了一个包子BaoZi{id=10, name='豆沙包'}
我做l一个包子BaoZi{id=11, name='鲜肉包'}
丙买了一个包子BaoZi{id=11, name='鲜肉包'}
我做l一个包子BaoZi{id=12, name='香菇包'}
我做l一个包子BaoZi{id=13, name='豆沙包'}
乙买了一个包子BaoZi{id=13, name='豆沙包'}
我做l一个包子BaoZi{id=14, name='豆沙包'}
我做l一个包子BaoZi{id=15, name='鲜肉包'}
丁买了一个包子BaoZi{id=15, name='鲜肉包'}
我做l一个包子BaoZi{id=16, name='香菇包'}
我做l一个包子BaoZi{id=17, name='豆沙包'}
丙买了一个包子BaoZi{id=17, name='豆沙包'}
乙买了一个包子BaoZi{id=16, name='香菇包'}
我做l一个包子BaoZi{id=18, name='鲜肉包'}
我做l一个包子BaoZi{id=19, name='香菇包'}
我做l一个包子BaoZi{id=20, name='香菇包'}
丙买了一个包子BaoZi{id=20, name='香菇包'}
乙买了一个包子BaoZi{id=19, name='香菇包'}
丙买了一个包子BaoZi{id=18, name='鲜肉包'}
丙买了一个包子BaoZi{id=14, name='豆沙包'}
丙买了一个包子BaoZi{id=12, name='香菇包'}

相关文章

网友评论

      本文标题:2018-12-25线程 生产者消费者模式(一)

      本文链接:https://www.haomeiwen.com/subject/uekqlqtx.html