阻塞队列实现生产者消费者模型

1.什么是阻塞队列?

阻塞队列是一种特殊的队列,首先它是线程安全的,其次它带有阻塞特性。阻塞特性就是当你的队列为空的时候,这时候如果要出队列,就会阻塞,阻塞到有元素添加为止;那如果队列为满的时候,这时候如果要入队列就会阻塞,阻塞到有元素出队列为止。它最大的意义是实现生产者消费者模型。

2.什么是生产者消费者模型?

生产者将生产的内容放到阻塞队列中,而消费者就从阻塞队列中获取到内容,这就是生产者消费者模型。其实就类似于包饺子一样,包饺子涉及到和面,擀面皮,和包饺子等一系列操作。那其中我们可以把擀面皮看作成生产者,而包饺子可以看作消费者,那阻塞队列就相当于放饺子皮的地方。

3.为什么要使用生产者消费者模型?

(1)解耦合

两个模块联系约紧密,耦合度越高。

如上图所示如果没有阻塞队列A和B直接交互,那么任何一个服务器挂了都会对对方产生影响。如果以后再引进服务器C那么服务器A和服务器B的代码改动也会很大。但是引入阻塞队列之后他们只和阻塞队列交互,彼此之间的耦合度降低了,任何一台服务器都不会对其他服务器产生影响。

(2)削峰填谷

两个服务器直接交互,如果服务器A处理请求的速度快,服务器B处理的请求慢,就有可能会把服务器弄崩溃。那削峰的意思就是当服务器A处理请求多的时候,放入到阻塞队列中,服务器B还是安装原来的速度处理请求;通常请求很多的情况不会一直出现,等到请求没有那么多的时候,服务器B就会把阻塞队列的请求给处理掉,这就是填谷。

4.java自带的阻塞队列

public class Test7 {
    
    //java自己提供的阻塞队列
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<>();
        queue.put("111");
        queue.put("222");
        queue.put("333");
        queue.put("444");
        queue.put("555");
        for (int i = 0; i < 5; i++) {
            System.out.println(queue.take());
        }

    }

}

5.自己实现一个阻塞队列

(1)先实现一个循环队列

public class MyBlockQueue {


    private int head = 0;//队头的下标
    private int tail = 0;//队尾的下标
    private int size = 0;//队列的大小
    private String[] data = new String[1000];

    public void put(String elem) {
        if (size == data.length) {//判断队列是不是为空
            return;
        }
        data[tail] = elem;//将传入的值赋值给队尾
        tail++;
        if (tail == data.length) {//因为是循环队列,所以要判断tail是不是和队列的大小一样,一样赋值为0,再一次循环
            tail = 0;
        }
        size++;
    }

    public String take() {
        if (size == 0) {
            return null;
        }
        String ret = data[head];//将队头的元素记录下来
        head++;
        if (head == data.length) {//因为是循环队列,所以要判断head是不是和队列的大小一样,一样赋值为0,再一次循环
            head = 0;
        }
        size--;
        return ret;
    }

    public static void main(String[] args) throws InterruptedException {
        MyBlockQueue queue = new MyBlockQueue();
        queue.put("111");
        queue.put("222");
        queue.put("333");
        queue.put("444");
        queue.put("555");
        for (int i = 0; i < 5; i++) {
            System.out.println(queue.take());
        }
    }
    

(2)再实现一个阻塞队列


    private volatile int head = 0;//队头的下标
    private volatile int tail = 0;//队尾的下标
    private volatile int size = 0;//队列的大小
    private final String[] data = new String[1000];

    public void put(String elem) throws InterruptedException {
        synchronized (this) {
            while (size == data.length) {//判断队列是不是为空
                this.wait();//队列满了就阻塞,等待有元素被take时唤醒
            }
            data[tail] = elem;//将传入的值赋值给队尾
            tail++;
            if (tail == data.length) {//因为是循环队列,所以要判断tail是不是和队列的大小一样,一样赋值为0,再一次循环
                tail = 0;
            }
            size++;
            this.notify();//此时有元素被put,唤醒take方法去删除元素
        }
    }

    public String take() throws InterruptedException {
        synchronized (this){
            while (size == 0) {
                this.wait();//当队列为空时,不能take元素,需要阻塞到有元素被put
            }
            String ret = data[head];//将队头的元素记录下来
            head++;
            if (head == data.length) {//因为是循环队列,所以要判断head是不是和队列的大小一样,一样赋值为0,再一次循环
                head = 0;
            }
            size--;
            this.notify();//此时有元素被take,唤醒放元素的方法
            return ret;
        }
    }

这里判断队列为空和队列为满用的时while而不是if的原因时,wait的唤醒不只是被notify唤醒,还有可能被interrupt唤醒,所以我们要持续判断。同时涉及到修改参数,利用volatile来修饰,保证内存可见性。

(3)最后实现生产者消费者模型

public class MyBlockQueue {


    private volatile int head = 0;//队头的下标
    private volatile int tail = 0;//队尾的下标
    private volatile int size = 0;//队列的大小
    private final String[] data = new String[1000];

    public void put(String elem) throws InterruptedException {
        synchronized (this) {
            while (size == data.length) {//判断队列是不是为空
                this.wait();//队列满了就阻塞,等待有元素被take时唤醒
            }
            data[tail] = elem;//将传入的值赋值给队尾
            tail++;
            if (tail == data.length) {//因为是循环队列,所以要判断tail是不是和队列的大小一样,一样赋值为0,再一次循环
                tail = 0;
            }
            size++;
            this.notify();//此时有元素被put,唤醒take方法去删除元素
        }
    }

    public String take() throws InterruptedException {
        synchronized (this){
            while (size == 0) {
                this.wait();//当队列为空时,不能take元素,需要阻塞到有元素被put
            }
            String ret = data[head];//将队头的元素记录下来
            head++;
            if (head == data.length) {//因为是循环队列,所以要判断head是不是和队列的大小一样,一样赋值为0,再一次循环
                head = 0;
            }
            size--;
            this.notify();//此时有元素被take,唤醒放元素的方法
            return ret;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyBlockQueue queue = new MyBlockQueue();
        Thread t1 = new Thread(()-> {
            int num = 1;
            while (true) {
                try {
                    queue.put(num+"");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("生成元素:" + num);
                num++;
            }
        });
        Thread t2 = new Thread(()-> {
            while (true) {
                try {
                    System.out.println("消费元素:" + queue.take());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值