j.u.c 阻塞队列

296 阅读3分钟

blockingQueue

  • 只能检索并删除此队列的头
  • 只能在队列尾插入
  • 遵循 FIFO
public interface BlockingQueue<E> extends Queue<E> {
  
//如果可以在不违反容量限制的情况下立即将指定元素插入此队列,则成功时返回true,如果当前没有可用空间,则抛出IllegalStateException。当使用容量受限队列时,通常最好使用offer。
    boolean add(E e);

 //如果可以在不违反容量限制的情况下立即将指定元素插入此队列,则成功时返回true,如果当前没有可用空间,则返回false。当使用容量受限队列时,此方法通常比add(E)更可取,后者抛出异常。
    boolean offer(E e);

   //将指定的元素插入此队列,必要时等待空间可用。
    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();

  //从队列中删除指定元素的单个实例(如果存在)。更正式地说,如果此队列包含一个或多个这样的元素,则移除元素e,使o.equals(e)。如果此队列包含指定元素,则返回true(如果此队列因调用而更改,则返回等效值)。
    boolean remove(Object o);

  
    public boolean contains(Object o);

 
//从该队列中删除所有可用元素并将它们添加到给定集合中此操作可能比重复轮询此队列更有效。尝试将元素添加到集合c时遇到错误,可能导致在引发关联的异常时,元素既不在集合中,也不在两个集合中。尝试将队列自身排出会导致IllegalArgumentException。此外,如果在操作进行时修改了指定的集合,则此操作的行为未定义。
    int drainTo(Collection<? super E> c);

  
    int drainTo(Collection<? super E> c, int maxElements);
}

ArrayBlockingQueue

  • 构造方法, 指定队列容量 确定数组大小, 之后数组大小不再改变。

  • 数组

  • 支持并发

    final ReentrantLock lock;

    /** Condition for waiting takes */
    private final Condition notEmpty;

    /** Condition for waiting puts */
    private final Condition notFull;


LinkedBlockingQueue

  • 构造方法, 指定队列大小, 如果不指定,则Integer.MAX_VALUE

  • 链表结构

  • 支持并发

BlockingDeque

  • 双端队列

  • 支持队列的头插入,也支持队列的尾插入

  • 支持队列的头检索和删除, 也支持队列的尾检索删除

  • 支持FIFO的队列属性

  • 也支持LIFO的栈结构属性


public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {

 
    void addFirst(E e);

  
    void addLast(E e);


    boolean offerFirst(E e);

  
    boolean offerLast(E e);

   
    void putFirst(E e) throws InterruptedException;

   
    void putLast(E e) throws InterruptedException;

   
    boolean offerFirst(E e, long timeout, TimeUnit unit)
        throws InterruptedException;

    
    boolean offerLast(E e, long timeout, TimeUnit unit)
        throws InterruptedException;

  
    E takeFirst() throws InterruptedException;

   
    E takeLast() throws InterruptedException;

 
    E pollFirst(long timeout, TimeUnit unit)
        throws InterruptedException;

  
    E pollLast(long timeout, TimeUnit unit)
        throws InterruptedException;


    boolean removeFirstOccurrence(Object o);

 
    boolean removeLastOccurrence(Object o);

    // *** BlockingQueue methods ***

   
    boolean add(E e);

  
    boolean offer(E e);

  
    void put(E e) throws InterruptedException;

  
    boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException;

  
    E remove();


    E poll();


    E take() throws InterruptedException;


    E poll(long timeout, TimeUnit unit)
        throws InterruptedException;

  
    E element();

 
    E peek();

   
    boolean remove(Object o);

  
    public boolean contains(Object o);


    public int size();


    Iterator<E> iterator();

    // *** Stack methods ***
    void push(E e);
}


LinkedBlockingDeque

  • 双端队列不支持数组结构的。