线程池中用到的阻塞队列-BlockingQueue

接口源码:

public interface BlockingQueue<E> extends Queue<E> {
    /**
     * 向队列中添加元素 成功返回true,队列满了则抛出异常
     */
    boolean add(E e);

    /**
     * 向队列中添加元素,成功返回true,队列满了返回false
     */
    boolean offer(E e);

    /**
     * 向队列中添加元素,如果队列满了会一直阻塞直到插入成功
     */
    void put(E e) throws InterruptedException;

    /**
     * 向队列插入数据,成功则返回true,如果队列满了则阻塞指定时间,在阻塞时间内成功则返回true,否则返回false
     */
    boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException;

    /**
     * 移除并返回当前队列的第一个元素,如果没有数据则一直阻塞直到有数据
     */
    E take() throws InterruptedException;

    /**
     * 移除并返回队列的第一个元素,如果没有数据则阻塞至指定时间
     */
    E poll(long timeout, TimeUnit unit)
        throws InterruptedException;

    /**
     * 返回队列剩余空间
     */
    int remainingCapacity();

    /**
     * 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
     *         (optional)
     * @throws NullPointerException if the specified element is null
     *         (optional)
     */
    boolean remove(Object o);

    /**
     * Returns {@code true} if this queue contains the specified element.
     * More formally, returns {@code true} if and only if this queue contains
     * at least one element {@code e} such that {@code o.equals(e)}.
     *
     * @param o object to be checked for containment in this queue
     * @return {@code true} if this queue contains the specified element
     * @throws ClassCastException if the class of the specified element
     *         is incompatible with this queue
     *         (optional)
     * @throws NullPointerException if the specified element is null
     *         (optional)
     */
    public boolean contains(Object o);

    /**
     * Removes all available elements from this queue and adds them
     * to the given collection.  This operation may be more
     * efficient than repeatedly polling this queue.  A failure
     * encountered while attempting to add elements to
     * collection {@code c} may result in elements being in neither,
     * either or both collections when the associated exception is
     * thrown.  Attempts to drain a queue to itself result in
     * {@code IllegalArgumentException}. Further, the behavior of
     * this operation is undefined if the specified collection is
     * modified while the operation is in progress.
     *
     * @param c the collection to transfer elements into
     * @return the number of elements transferred
     * @throws UnsupportedOperationException if addition of elements
     *         is not supported by the specified collection
     * @throws ClassCastException if the class of an element of this queue
     *         prevents it from being added to the specified collection
     * @throws NullPointerException if the specified collection is null
     * @throws IllegalArgumentException if the specified collection is this
     *         queue, or some property of an element of this queue prevents
     *         it from being added to the specified collection
     */
    int drainTo(Collection<? super E> c);

    /**
     * Removes at most the given number of available elements from
     * this queue and adds them to the given collection.  A failure
     * encountered while attempting to add elements to
     * collection {@code c} may result in elements being in neither,
     * either or both collections when the associated exception is
     * thrown.  Attempts to drain a queue to itself result in
     * {@code IllegalArgumentException}. Further, the behavior of
     * this operation is undefined if the specified collection is
     * modified while the operation is in progress.
     *
     * @param c the collection to transfer elements into
     * @param maxElements the maximum number of elements to transfer
     * @return the number of elements transferred
     * @throws UnsupportedOperationException if addition of elements
     *         is not supported by the specified collection
     * @throws ClassCastException if the class of an element of this queue
     *         prevents it from being added to the specified collection
     * @throws NullPointerException if the specified collection is null
     * @throws IllegalArgumentException if the specified collection is this
     *         queue, or some property of an element of this queue prevents
     *         it from being added to the specified collection
     */
    int drainTo(Collection<? super E> c, int maxElements);
}

线程池中用到的阻塞队列-BlockingQueue_第1张图片

相关实现类:
线程池中用到的阻塞队列-BlockingQueue_第2张图片

你可能感兴趣的:(Java基础,Java多线程)