生产者消费者模式

1

概念

生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题。

生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。这个阻塞队列就是用来给生产者和消费者解耦的。

2

基于BlockingQueue的生产者消费者模型

BlockingQueue 在多线程编程中阻塞队列(Blocking Queue)是一种常用于实现生产者和消费者模型的数据结构。其与普通的队列区别在于,当队列为空时,从队列获取元素的操作将会被阻塞,直到队列中被放入了元素;当队列满时,往队列里存放元素的操作也会被阻塞,直到有元素被从队列中取出(以上的操作都是基于不同的线程来说的,线程在对阻塞队列进程操作时会被阻塞)

阻塞队列采用ReentrantLock

详情

产品

public class Goods {
    private int id;
    private String name;

    public Goods(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

中间容器

public class MidContainer {

    public static final int MAX_POOL=10;
    public static final int MAX_PRODUCER=5;
    public static final int MAX_CONSUMER=4;
    public static Queue<Goods> queue=new ArrayBlockingQueue<>(MAX_POOL);


}

生产者

public class Producer implements Runnable {
    private Goods goods;
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {

        while (true){
            try{
                Thread.sleep(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }

            synchronized (MidContainer.queue){
                goods=new Goods(0001,"ipad2020");
                if(MidContainer.queue.size()<MidContainer.MAX_POOL){
                    MidContainer.queue.add(goods);
                    System.out.println(Thread.currentThread().getName()+"生产了商品");
                }else {
                    try{
                        MidContainer.queue.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }




        }
    }
}

消费者

public class Consumer implements Runnable {

    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {

        while (true){
            try {
                Thread.sleep(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }

            synchronized (MidContainer.queue){
                if(!MidContainer.queue.isEmpty()){
                    MidContainer.queue.poll();
                    System.out.println(Thread.currentThread().getName()+"买了这个东西");
                }else {
                    MidContainer.queue.notify();
                }
            }
        }
    }
}

测试

public class Test {
    public static void main(String[] args) {
        Producer producer = new Producer();
        Consumer consumer = new Consumer();
        for (int i = 0; i < MidContainer.MAX_PRODUCER; i++) {
            Thread thread_pro = new Thread(producer, "生产者线程" + "-----------" + i);
            thread_pro.start();
        }
        for (int j = 0; j < MidContainer.MAX_CONSUMER; j++) {
            Thread thread_con = new Thread(consumer, "消费者线程" + "-----------" + j);
            thread_con.start();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值