Java并发编程(1)—— 实现一个生产者消费者队列(三种方式)

生产者消费者队列,顾名思义,就是一个队列,不停地有生产者在里面生产对象并通知阻塞的消费者可以消费了,如果队列满了,生产者就阻塞不能再生产;消费者来消费(也就是读取并拿走队列里的对象)并通知阻塞的生产者,直到把队列消费空,就阻塞不能再消费。wait/notify机制wait/notify机制原理wait/notify机制是与Monitor监视器锁关联在一起的。一个线程在持有某个对象的监视器...
摘要由CSDN通过智能技术生成

生产者消费者队列,顾名思义,就是一个队列,不停地有生产者在里面生产对象并通知阻塞的消费者可以消费了,如果队列满了,生产者就阻塞不能再生产;消费者来消费(也就是读取并拿走队列里的对象)并通知阻塞的生产者,直到把队列消费空,就阻塞不能再消费。

2020.3.2更新:
原来的代码不是特别简练,修改了一下,参考的是youtube上一个博主的实现,实现的还是挺简练的。

BlockingQueue版本的实现

public class ProducerConsumerQueueBlockingQueue {
   

    /** BlockingQueue实现的生产者消费者模式 **/

    public static void main(String[] args) {
   

        BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(10);

        final Runnable producer = () -> {
   
            while(true){
   
                try {
   
                    //blockingQueue.put(ItemFactory.createItem());
                    blockingQueue.put(new Object());
                }catch (InterruptedException e){
   
                    e.printStackTrace();
                }
            }
        };

        for (int i=0; i<5; ++i)
            new Thread(producer).start();

        final Runnable consumer = () -> {
   
            while(true){
   
                try {
   
                    Object item = blockingQueue.take();
                    // process(item);
                }catch (InterruptedException e){
   
                    e.printStackTrace();
                }
            }
        };

        for (int i=0; i<5; ++i)
            new Thread(consumer).start();
    }
}

Condition的await/signal版本实现

public class ProducerConsumerQueue<E> {
   

    private Queue<E> queue;
    private int max = 16;
    private Object notFull;
    private Object notEmpty;

    public ProducerConsumerQueue(int size){
   
        queue = new LinkedList<>();
        this.max = size;
        notFull = new Object();
        notEmpty = new Object();
    }

    public synchronized void put(E element){
   
        while(queue.size() == max){
   
            try {
   
                notFull.wait();
            }catch (InterruptedException e){
   
                e.printStackTrace();
            }
        }
        queue.add(element);
        notEmpty.notifyAll();
    }

    public synchronized E take(){
   
        while(queue.size()==0){
   
            try {
   
                notEmpty.wait();
            }catch (InterruptedException e){
   
                e.printStackTrace();
            }
        }
        E element = queue.remove();
        notFull.notifyAll();
        return element;
    }


    public static void main(String[] args) {
   
        ProducerConsumerQueue<Object> pc = new ProducerConsumerQueue(10);

        final Runnable producer = ()->{
   
            while(true)
                pc.put(new Object());
        };

        final Runnable consumer = ()->{
   
            while(true){
   
                Object obj = pc.take
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值