阻塞队列BlockingQueue的add offer put,

2021-05-16  本文已影响0人  一叶知秋yi

根据个人经验,做Android开发的,可能阻塞队列使用会相对较少,但是有时候看框架源码经常会碰到,所以有必要学习一下。阻塞队列里面的几个添加和删除的方法太容易记混了,所以这里专门总结记录一下,一个是可以加深自己的记忆,另一个也可以把我的理解分享给大家。

先直接放结论,有兴趣的可以再继续往后看具体的分析

添加方法 add() offer() put()
添加成功 return true return true 无返回
添加失败 抛异常 return false 阻塞等待直到可以插入
对应删除方法 remove() poll() take()

先讲一讲添加的几个方法,删除方案都是跟添加方法一一对应的。我们先看一下BlockingQueue中的添加方法说明

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions, returning
     * {@code true} upon success and throwing an
     * {@code IllegalStateException} if no space is currently available.
     * When using a capacity-restricted queue, it is generally preferable to
     * use {@link #offer(Object) offer}.
     *
     * @param e the element to add
     * @return {@code true} (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    boolean add(E e);

往队列中插入一个元素,插入成功则返回true,插入失败则抛出异常。

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions, returning
     * {@code true} upon success and {@code false} if no space is currently
     * available.  When using a capacity-restricted queue, this method is
     * generally preferable to {@link #add}, which can fail to insert an
     * element only by throwing an exception.
     *
     * @param e the element to add
     * @return {@code true} if the element was added to this queue, else
     *         {@code false}
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    boolean offer(E e);

往队列中插入一个元素,插入成功则返回true,插入失败则返回false。

    /**
     * Inserts the specified element into this queue, waiting if necessary
     * for space to become available.
     *
     * @param e the element to add
     * @throws InterruptedException if interrupted while waiting
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    void put(E e) throws InterruptedException;

往队列中插入一个元素,如果队列已满会阻塞,直到队列有空余空间。

上一篇下一篇

猜你喜欢

热点阅读