来谈谈 java.util.Queue 和 java.util.concurrent.BlockingQueue 接口规范(JDK1.8 源码分析)

Queue / BlockingQueue 接口规范


前言

普通队列接口和阻塞队列接口简单介绍


提示:以下是本篇文章正文内容,下面案例可供参考

一、Queue 源码部分

public interface Queue<E> extends Collection<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.
     *
     * @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 and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this 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.
     * 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 and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(E e);

    /**
     * Retrieves and removes the head of this queue.  This method differs
     * from {@link #poll poll} only in that it throws an exception if this
     * queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E remove();

    /**
     * Retrieves and removes the head of this queue,
     * or returns {@code null} if this queue is empty.
     *
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E poll();

    /**
     * Retrieves, but does not remove, the head of this queue.  This method
     * differs from {@link #peek peek} only in that it throws an exception
     * if this queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E element();

    /**
     * Retrieves, but does not remove, the head of this queue,
     * or returns {@code null} if this queue is empty.
     *
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E peek();
}
  1. 插入方法:
    boolean add(E e); 插入一个元素到队列中,失败时返回IllegalStateException (队列容量不够)
    boolean offer(E e);添加队列成功返回true,否则返回false

    有容量限制时尽量使用offer因为添加队列失败时直接返回false,而add则抛给异常处理,比较耗时。

  2. 删除方法:
    E remove();//删除队列头部的元素,队列为空时抛出异常
    E poll(); //删除队列头部的元素队列为空时返回null

  3. 查找方法:
    E element(); //返回队列头部元素,为空时抛出异常
    E peek(); //返回队列头部元素,为空时返回null

队列接口主要申明了一些入队和出队的方法,这些方法的区别在于入队和出队操作失败时返回值的不同。

二、BlockingQueue 源码部分

public interface BlockingQueue<E> extends Queue<E> {
    
    
     //与父接口中的add方法类似
     boolean add(E e);

    //与父接口中的offer方法类似
    boolean offer(E e);

    /**
     *将指定的元素插入到队列中,如果需要等待
     *使空间可用。
     *
     * @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;

    //如果队列满了,一直阻塞,直到队列不满了或者线程被中断或者超时
    boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException;

    //如果队列空了,一直阻塞,直到队列不为空或者线程被中断
    E take() throws InterruptedException;

    //如果队列空了,一直阻塞,直到队列不为空或者线程被中断或者超时
    E poll(long timeout, TimeUnit unit)
        throws InterruptedException;

    //返回剩余容量
    int remainingCapacity();

    //删除队列中指定元素
    boolean remove(Object o);

    //队列中是否含有某个元素
    public boolean contains(Object o);

    //将队列中所有元素删除并添加到指定集合中去
    int drainTo(Collection<? super E> c);

    //将队列中最多maxElements个元素删除并添加到指定集合中去
    int drainTo(Collection<? super E> c, int maxElements);
}

  1. 入队操作:
    void put(E e) throws InterruptedException;
    boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;

    区别在于offer入队超时后会立即返回false。

  2. 出队操作:
    E take() throws InterruptedException;
    E poll(long timeout, TimeUnit unit)

    区别在于poll出队超时后会返回null。

猜你喜欢

转载自blog.csdn.net/weixin_41237676/article/details/109036237
今日推荐