Java多线程—生产者与消费者设计

生产者:
作用是生产产品
生产逻辑:通过一个生产标记,判断是否需要生产产品
如果需要生产:生产产品,并通知消费者
如果不需要: 等待

消费者:
作用是消费产品
消费逻辑:判断是否有足够的产品可以消费
如果可以消费:获取产品,进行消费
如果不可以:等待

假设有这样一个场景:餐厅当中客人将自己所选的菜单交给服务员,服务员再把客人的需求交给厨房,此时就是一个生产者消费者的问题,客人进行消费,厨房进行生产,下面就来模拟一下这个场景:

把厨房看作是一个生产工程,厨房一直在生产(有生产的最大限制),达到最大等待,然后客人进行消费, 当生产的量为0时,客人进行等待,然后一直进行下去。

厨房工场:

public class ProductPool {

    /*存贮所有的产品的集合,生产者生产产品,往这个集合中添加元素,
     * 消费者消费产品,从这个集合中取元素*/
    private List<Produce> produceList;

    //产品池中产品最大的数量
    private int maxSize;

    public ProductPool(int maxSize) {
        this.produceList = new LinkedList<>();
        this.maxSize = maxSize;
    }

    /*生产者将生产好的商品放入商品池*/
    public synchronized void push(Produce produce) {

        //判断是否还需要在生产产品
        if (this.produceList.size() > maxSize) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //将产品添加到集合当中
        this.produceList.add(produce);
        //通知其他人,有产品可以消费了
        this.notifyAll();
    }

    /*消费者从商品池中取出一件商品消费*/
    public synchronized Produce pop() {

        //判断是否还有商品再去消费
        if (this.produceList.size() == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //从商品池中取出一件商品
        Produce remove = this.produceList.remove(0);
        //通知其他人,我取出了一件商品
        this.notifyAll();
        return remove;
    }
}

产品:

package org.youyuan.thread.produceAndConsumer;

public class Produce {

    private String name;

    public String getName() {
        return name;
    }

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

    public Produce(String name) {
        this.name = name;
    }

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

生产者:

package org.youyuan.thread.produceAndConsumer;

public class Productors extends Thread {

    private ProductPool pool;

    public Productors(ProductPool productPool){
        this.pool = productPool;
    }

    @Override
    public void run() {
        while (true){
            String name = (int)(Math.random()*100)+"号产品";
            System.out.println("生产了一件产品:"+name);
            Produce produce = new Produce(name);
            this.pool.push(produce);
        }
    }
} 

消费者:

package org.youyuan.thread.produceAndConsumer;

public class Customer extends Thread {

    private ProductPool productPool;

    public Customer(ProductPool ProductPool){
        this.productPool = ProductPool;
    } 
    @Override
    public void run() {
        while (true){
            Produce pop = this.productPool.pop();
            System.out.println("消费者消费了一件产品:"+pop.getName());
        }
    }
} 

运行程序:

package org.youyuan.thread.produceAndConsumer;

public class Program {

    /*生产者
    *作用是生产产品
    * 生产逻辑:通过一个生产标记,判断是否需要生产产品
    * 如果需要生产:生产产品,并通知消费者
    * 如果不需要: 等待
    *
    **/

    /*消费者
    * 作用是消费产品
    * 消费逻辑:判断是否有足够的产品可以消费
    * 如果可以消费:获取产品,进行消费
    * 如果不可以:等待
    * */

    public static void main(String[] args) {
        ProductPool productPool = new ProductPool(15);
        new Productors(productPool).start();
        new Customer(productPool).start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值