阻塞队列------­­­BlockingQueue,以及常用的API

1.什么是阻塞队列
了解一下BlockingQueue的依赖关系

  • Collection接口是最上级的接口
  • Queue 和 Set List是同级关系
  • Queue继承者包括:
    Deque双端队列
    BlockingQueue阻塞队列
    AbstractQueue非阻塞队列

阻塞队列的概念:可以简单的理解成队列相当于一个管道:

  • 1.先进先出
  • 2.写入队列(当队列满了,再写入就必须阻塞等待)
  • 3.获取队列中的数据(如果队列是空的,只有等待写入后才能读取)

2.BlockingQueue主要的使用方法
在这里插入图片描述

主要的是一个添加方法移除方法,返回的都是布尔值

3.什么时候会用到阻塞队列BlockingQueue

  • 线程池,多线程并发处理时会用
    结合上面的情况,我自我理解觉得有点类似于线程的生产者和消费者,会有线程通信的情况,这个就必须考虑到线程的安全同步

BlockingQueue的使用特点:
1.添加元素,可以抛出异常,使用add方法
add方法的源码

    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

使用实例:

public class BlockQueueDemo {
    public static void main(String[] args) {
        testException();
    }

    /**
     * 抛出异常
     */
    public  static  void testException(){
        //这里的3指的是队列的容量
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
//        System.out.println(blockingQueue.add("d"));

        System.out.println();
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        //System.out.println(blockingQueue.remove());
    }

如果在上面代码中,在队列多增加了元素d,则会抛出一个队列已满的异常
在这里插入图片描述
同样的道理,如果队列中只添加了3个元素,如果要去取出第4个元素也会报错,同时也可以看出,队列的是先进先出
在这里插入图片描述

2.在队列中添加元素,不让抛出异常,使用offer方法,offer方法的源码:

@throws NullPointerException if the specified element is null如果添加的元素是null则会抛出空指针异常

  /**
     * Inserts the specified element at the tail of this queue if it is
     * possible to do so immediately without exceeding the queue's capacity,
     * returning {@code true} upon success and {@code false} if this queue
     * is full.  This method is generally preferable to method {@link #add},
     * which can fail to insert an element only by throwing an exception.
     *
     * @throws NullPointerException if the specified element is null
     */
    public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (count == items.length)
                return false;
            else {
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }

使用实例:
存入

    public static  void test2(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));

        System.out.println();
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }

队列容量同样是3个,调用方法,取出是使用poll方法,打印台输出的是
在这里插入图片描述
3.判断队列队首,一个队列中,三个元素都存放进去,结果是输出的"a"元素

    /**
     * 判断队首
     */
    public  static  void test3(){
        //这里的3指的是队列的容量
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));

        System.out.println();
        //element方法和peek方法都可以检测队首元素,不过element方法会抛出异常,peek不会
        System.out.println(blockingQueue.element());
        //  System.out.println(blockingQueue.peek());

    }

在这里插入图片描述
4.阻塞等待(一直等待)

    /**
     * 阻塞等待
     * @throws InterruptedException
     */
    public static void  test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");//存入
        blockingQueue.put("b");
        blockingQueue.put("c");
        blockingQueue.put("d");

    }

如果队列容量不够,会一直等待
在这里插入图片描述

        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
//        blockingQueue.put("d");
		//取出
        System.out.println(blockingQueue.take());

取出的结果是
在这里插入图片描述
5.超时等待,使用的是offer和poll的重载方法,给存入方法一个超时时间
存入
在这里插入图片描述
取出
在这里插入图片描述

    public static void test5() throws InterruptedException {
    ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
//        blockingQueue.offer("d",2, TimeUnit.SECONDS);//等待超过2秒钟如果队列还没有空间存放,就退出
        System.out.println();
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));//等待超过2秒钟如果队列还没有元素弹出,就退出
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值