Java 阻塞队列与生产者消费者模型

一、阻塞队列

阻塞队列是⼀种特殊的队列,其也遵守队列 "先进先出" 的原则;

阻塞队列是⼀种线程安全的数据结构,并且具有以下特性:

当队列满的时候,继续入队列就会阻塞,直到有其他线程从队列中取走元素;

当队列空的时候,继续出队列也会阻塞,直到有其他线程往队列中插⼊元素;

阻塞队列的⼀个典型应用场景就是 "⽣产者消费者模型",这是⼀种非常典型的开发模型;

二、生产者消费者模型

生产者消费者模型就是通过⼀个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取;

使用阻塞队列实现生产者消费者模型的好处:

1. 阻塞队列就相当于⼀个缓冲区,平衡了生产者和消费者的处理能力;(削峰填谷)

2. 阻塞队列也能使生产者和消费者之间解耦合;

Java 标准库中的阻塞队列:

BlockingQueue 是⼀个接口,真正实现的类是 LinkedBlockingQueue,ArrayBlockingQueue,PriorityBlockingQueue 等;

put 方法用于阻塞式的入队列,take 用于阻塞式的出队列,BlockingQueue 也有 offer,poll,peek 等方法,但是这些方法并不带有阻塞特性;

下面就来实现一个阻塞队列,并基于该阻塞队列实现生产者消费者模型;

public class MyBlockingQueue {
    private int[] items = new int[10000];
    private volatile int size = 0;
    private volatile int head = 0;
    private volatile int tail = 0;

    /**
     * 实现进队列
     * @param value value
     * @throws InterruptedException e
     */
    public synchronized void put(int value) throws InterruptedException {
        while (size == items.length){
            this.wait();
        }
        items[tail] = value;
        tail = (tail + 1) % items.length;
        size++;
        notifyAll();
    }

    /**
     * 实现出队列
     * @return value
     * @throws InterruptedException e
     */
    public synchronized int take() throws InterruptedException {
        while (size == 0){
            this.wait();
        }
        int tmp = items[head];
        head = (head + 1) % items.length;
        size--;
        notifyAll();
        return tmp;
    }

    public static void main(String[] args) throws InterruptedException {
        MyBlockingQueue blockingQueue = new MyBlockingQueue();
        Thread customer = new Thread(() -> {
            while (true){
                try {
                    int value = blockingQueue.take();
                    System.out.println("消费者消费 " + value);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread producer = new Thread(() -> {
            while (true){
                try {
                    int value = new Random().nextInt(100);
                    blockingQueue.put(value);
                    System.out.println("生产者生产 " + value);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        customer.start();
        producer.start();
        customer.join();
        producer.join();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rcnhtin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值