生产者消费者模式

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();
        }
    }
}
全部评论

相关推荐

01-16 18:34
四川大学 Java
欢迎加入AI:没有啥稳定不稳定,一切都源于业务快速发展还是收缩。我当年一开始去的央企,业务不赚钱,也贼卷,慢慢就开始优化了。。。
点赞 评论 收藏
分享
02-17 20:43
西北大学 Java
在做测评的猫头鹰很紧张:他问你,你问deep seek
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务