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

根據(jù)個(gè)人經(jīng)驗(yàn),做Android開發(fā)的,可能阻塞隊(duì)列使用會(huì)相對(duì)較少,但是有時(shí)候看框架源碼經(jīng)常會(huì)碰到,所以有必要學(xué)習(xí)一下。阻塞隊(duì)列里面的幾個(gè)添加和刪除的方法太容易記混了,所以這里專門總結(jié)記錄一下,一個(gè)是可以加深自己的記憶,另一個(gè)也可以把我的理解分享給大家。

先直接放結(jié)論,有興趣的可以再繼續(xù)往后看具體的分析

添加方法 add() offer() put()
添加成功 return true return true 無返回
添加失敗 拋異常 return false 阻塞等待直到可以插入
對(duì)應(yīng)刪除方法 remove() poll() take()

先講一講添加的幾個(gè)方法,刪除方案都是跟添加方法一一對(duì)應(yīng)的。我們先看一下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);

往隊(duì)列中插入一個(gè)元素,插入成功則返回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);

往隊(duì)列中插入一個(gè)元素,插入成功則返回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;

往隊(duì)列中插入一個(gè)元素,如果隊(duì)列已滿會(huì)阻塞,直到隊(duì)列有空余空間。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容