阻塞隊列BlockingQueue的add offer put,你分得清了嗎?

根據個人經驗,做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;

往隊列中插入一個元素,如果隊列已滿會阻塞,直到隊列有空餘空間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章