美文网首页
阻塞队列BlockingQueue

阻塞队列BlockingQueue

作者: Charein | 来源:发表于2019-01-29 14:58 被阅读0次

    BlockingQueue是一个接口继承于Queue

    public interface BlockingQueue<E> extends Queue<E> {
        /**
         * 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);
    
        /**
         * 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);
    
        /**
         * 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;
    
        /**
         * Inserts the specified element into this queue, waiting up to the
         * specified wait time if necessary for space to become available.
         *
         * @param e the element to add
         * @param timeout how long to wait before giving up, in units of
         *        {@code unit}
         * @param unit a {@code TimeUnit} determining how to interpret the
         *        {@code timeout} parameter
         * @return {@code true} if successful, or {@code false} if
         *         the specified waiting time elapses before space is available
         * @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
         */
        boolean offer(E e, long timeout, TimeUnit unit)
            throws InterruptedException;
    
        /**
         * Retrieves and removes the head of this queue, waiting if necessary
         * until an element becomes available.
         *
         * @return the head of this queue
         * @throws InterruptedException if interrupted while waiting
         */
        E take() throws InterruptedException;
    
        /**
         * Retrieves and removes the head of this queue, waiting up to the
         * specified wait time if necessary for an element to become available.
         *
         * @param timeout how long to wait before giving up, in units of
         *        {@code unit}
         * @param unit a {@code TimeUnit} determining how to interpret the
         *        {@code timeout} parameter
         * @return the head of this queue, or {@code null} if the
         *         specified waiting time elapses before an element is available
         * @throws InterruptedException if interrupted while waiting
         */
        E poll(long timeout, TimeUnit unit)
            throws InterruptedException;
    
        /**
         * Removes a single instance of the specified element from this queue,
         * if it is present.  More formally, removes an element {@code e} such
         * that {@code o.equals(e)}, if this queue contains one or more such
         * elements.
         * Returns {@code true} if this queue contained the specified element
         * (or equivalently, if this queue changed as a result of the call).
         *
         * @param o element to be removed from this queue, if present
         * @return {@code true} if this queue changed as a result of the call
         * @throws ClassCastException if the class of the specified element
         *         is incompatible with this queue
         *         (<a href="../Collection.html#optional-restrictions">optional</a>)
         * @throws NullPointerException if the specified element is null
         *         (<a href="../Collection.html#optional-restrictions">optional</a>)
         */
        boolean remove(Object o);
    }
    
    特殊值 超时 抛出异常 阻塞
    插入 offer offer(E e, long timeout, TimeUnit unit) add put
    移除 poll poll(long timeout, TimeUnit unit) remove take
    检查结果 peek 不可用 element 不可用

    相关文章

      网友评论

          本文标题:阻塞队列BlockingQueue

          本文链接:https://www.haomeiwen.com/subject/cusxsqtx.html