public interface BlockingQueue 
 implements Queue<E>
| java.util.concurrent.BlockingQueue<E> | 
|    
          ArrayBlockingQueue<E>, 
          BlockingDeque<E>, 
          DelayQueue<E extends  
          Delayed>, 
          LinkedBlockingDeque<E>, 
          LinkedBlockingQueue<E>, 
          LinkedTransferQueue<E>, 
          PriorityBlockingQueue<E>, 
          SynchronousQueue<E>, 
          TransferQueue<E> 
           
          
         |  
      
Queue还支持在检索元素时等待队列变为非空的操作,并在存储元素时等待队列中的空间变为可用。
BlockingQueue方法有四种形式,处理操作的方式不能立即满足,但可能在未来某个时候满足:一个抛出异常,第二个返回特殊值( null或false ,取决于操作),第三个块无限期地阻塞当前线程,直到操作成功,第四个块在放弃之前只有给定的最大时间限制。 下表总结了这些方法:
| Throws exception | Special value | Blocks | Times out | |
| Insert | add(e) |  
       offer(e) |  
       put(e) |  
       offer(e, time, unit) |  
      
| Remove | remove() |  
       poll() |  
       take() |  
       poll(time, unit) |  
      
| Examine | element() |  
       peek() |  
       not applicable | not applicable | 
A BlockingQueue不接受null元素。 NullPointerException尝试执行put或offer和add , put实施投掷null 。 使用null作为null值来指示poll操作失败。
A BlockingQueue可能是有限的容量。 在任何给定时间可以有一个remainingCapacity超过该无额外元件可以是put无阻塞。 一个BlockingQueue没有任何内部容量约束始终报告的剩余容量Integer.MAX_VALUE 。
BlockingQueue实现被设计为主要用于生产者 - 消费者队列,但另外支持Collection接口。 因此,例如,可以使用remove(x)从队列中删除任意元素。 然而,这些操作通常不是非常有效地执行,并且仅用于偶尔使用,例如当排队的消息被取消时。
BlockingQueue实现是线程安全的。 所有排队方法都使用内部锁或其他形式的并发控制自动实现其效果。 然而, 大量的Collection操作addAll , containsAll , retainAll和removeAll 不一定原子除非在实现中另有规定执行。 因此,例如,添加c一些元素后, addAll(c)可能会失败(抛出异常)。
BlockingQueue本质上不支持任何种类的“关闭”或“关闭”操作,以指示不会再添加任何项目。 这些功能的需求和使用倾向于取决于实现。 例如,一种常见策略是生产者插入特殊的流结束或中毒对象,这些对象在消费者采取时会相应地进行解释。
使用示例,基于典型的生产者 - 消费者场景。 请注意, BlockingQueue可以安全地与多个生产者和多个消费者一起使用。
 class Producer implements Runnable {
   private final BlockingQueue queue;
   Producer(BlockingQueue q) { queue = q; }
   public void run() {
     try {
       while (true) { queue.put(produce()); }
     } catch (InterruptedException ex) { ... handle ...}
   }
   Object produce() { ... }
 }
 class Consumer implements Runnable {
   private final BlockingQueue queue;
   Consumer(BlockingQueue q) { queue = q; }
   public void run() {
     try {
       while (true) { consume(queue.take()); }
     } catch (InterruptedException ex) { ... handle ...}
   }
   void consume(Object x) { ... }
 }
 class Setup {
   void main() {
     BlockingQueue q = new SomeQueueImplementation();
     Producer p = new Producer(q);
     Consumer c1 = new Consumer(q);
     Consumer c2 = new Consumer(q);
     new Thread(p).start();
     new Thread(c1).start();
     new Thread(c2).start();
   }
 } 
    存储器一致性效果:当与其他并发集合,事先将物体放置成在一个线程动作 BlockingQueue happen-before到该元素的从访问或移除后续动作 BlockingQueue在另一个线程。
Public methods |  
      |
|---|---|
 abstract boolean  |  
         add(E e)  将指定的元素插入此队列中,如果它是立即可行且不会违反容量限制,返回   |  
      
 abstract boolean  |  
         contains(Object o)  如果此队列包含指定的元素,则返回   |  
      
 abstract int  |  
         drainTo(Collection<? super E> c)  从该队列中移除所有可用的元素,并将它们添加到给定的集合中。  |  
      
 abstract int  |  
         drainTo(Collection<? super E> c, int maxElements)  最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。  |  
      
 abstract boolean  |  
         offer(E e)  将指定的元素插入此队列中,如果它是立即可行且不会违反容量限制,返回   |  
      
 abstract boolean  |  
         offer(E e, long timeout, TimeUnit unit)  将指定的元素插入此队列中,如有必要,等待指定的等待时间以使空间变为可用。  |  
      
 abstract E  |  
         poll(long timeout, TimeUnit unit)  检索并删除此队列的头部,如果元素变为可用,则等待达到指定的等待时间。  |  
      
 abstract void  |  
         put(E e)  将指定的元素插入此队列,等待空间变得可用时。  |  
      
 abstract int  |  
         remainingCapacity()  如果没有内部限制,则返回此队列理想情况下(在没有内存或资源约束的情况下)接受而没有阻塞的附加元素的数量,或者   |  
      
 abstract boolean  |  
         remove(Object o)  从该队列中移除指定元素的单个实例(如果存在)。  |  
      
 abstract E  |  
         take()  检索并删除此队列的头部,如果需要等待,直到元素变为可用。  |  
      
Inherited methods |  
      |
|---|---|
  java.util.Queue  
         |  
      |
  java.util.Collection  
         |  
      |
  java.lang.Iterable  
         |  
      |
boolean add (E e)
将指定的元素插入此队列中,如果它是立即可行且不会违反容量限制,返回true在成功和抛出IllegalStateException ,如果当前没有空间可用。 使用容量限制队列时,通常最好使用offer 。
| Parameters | |
|---|---|
e |  
         E: the element to add |  
       
| Returns | |
|---|---|
boolean |  
        true (as specified by add(E)) |  
       
| Throws | |
|---|---|
IllegalStateException |  
        if the element cannot be added at this time due to capacity restrictions | 
ClassCastException |  
        if the class of the specified element prevents it from being added to this queue | 
NullPointerException |  
        if the specified element is null | 
IllegalArgumentException |  
        if some property of the specified element prevents it from being added to this queue | 
boolean contains (Object o)
如果此队列包含指定的元素,则返回true 。 更正式地,返回true当且仅当该队列包含至少一个元素e例如o.equals(e) 。
| Parameters | |
|---|---|
o |  
         Object: object to be checked for containment in this queue |  
       
| Returns | |
|---|---|
boolean |  
        true if this queue contains the specified element |  
       
| Throws | |
|---|---|
ClassCastException |  
        if the class of the specified element is incompatible with this queue (optional) | 
NullPointerException |  
        if the specified element is null (optional) | 
int drainTo (Collection<? super E> c)
从该队列中移除所有可用的元素,并将它们添加到给定的集合中。 该操作可能比重复轮询该队列更有效。 尝试将元素添加到集合c遇到的故障可能导致元素不在任何集合中,或者在引发关联异常时集合中的任何一个或两个集合都不在元素中。 尝试将队列IllegalArgumentException自身导致IllegalArgumentException 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| Parameters | |
|---|---|
c |  
         Collection: the collection to transfer elements into |  
       
| Returns | |
|---|---|
int |  
        the number of elements transferred | 
| Throws | |
|---|---|
UnsupportedOperationException |  
        if addition of elements is not supported by the specified collection | 
ClassCastException |  
        if the class of an element of this queue prevents it from being added to the specified collection | 
NullPointerException |  
        if the specified collection is null | 
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)
最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。 尝试将元素添加到集合c遇到的故障可能导致元素不在任何集合中,或者在引发关联异常时集合中的任何一个集合或两个集合都不在元素中。 尝试排队到自己导致IllegalArgumentException 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| Parameters | |
|---|---|
c |  
         Collection: the collection to transfer elements into |  
       
maxElements |  
         int: the maximum number of elements to transfer |  
       
| Returns | |
|---|---|
int |  
        the number of elements transferred | 
| Throws | |
|---|---|
UnsupportedOperationException |  
        if addition of elements is not supported by the specified collection | 
ClassCastException |  
        if the class of an element of this queue prevents it from being added to the specified collection | 
NullPointerException |  
        if the specified collection is null | 
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 | 
boolean offer (E e)
将指定的元素插入此队列中,如果它是立即可行且不会违反容量限制,返回true在成功和false如果当前没有空间可用。 在使用容量受限的队列时,通常最好使用这种方法,因为add(E)只能通过抛出异常来插入元素。
| Parameters | |
|---|---|
e |  
         E: the element to add |  
       
| Returns | |
|---|---|
boolean |  
        true if the element was added to this queue, else false |  
       
| Throws | |
|---|---|
ClassCastException |  
        if the class of the specified element prevents it from being added to this queue | 
NullPointerException |  
        if the specified element is null | 
IllegalArgumentException |  
        if some property of the specified element prevents it from being added to this queue | 
boolean offer (E e, 
                long timeout, 
                TimeUnit unit) 
     将指定的元素插入此队列中,如有必要,等待指定的等待时间以使空间变为可用。
| Parameters | |
|---|---|
e |  
         E: the element to add |  
       
timeout |  
         long: how long to wait before giving up, in units of unit |  
       
unit |  
         TimeUnit: a TimeUnit determining how to interpret the timeout parameter |  
       
| Returns | |
|---|---|
boolean |  
        true if successful, or false if the specified waiting time elapses before space is available |  
       
| Throws | |
|---|---|
InterruptedException |  
        if interrupted while waiting | 
ClassCastException |  
        if the class of the specified element prevents it from being added to this queue | 
NullPointerException |  
        if the specified element is null | 
IllegalArgumentException |  
        if some property of the specified element prevents it from being added to this queue | 
E poll (long timeout, 
                TimeUnit unit) 
     检索并删除此队列的头部,如果元素变为可用,则等待达到指定的等待时间。
| Parameters | |
|---|---|
timeout |  
         long: how long to wait before giving up, in units of unit |  
       
unit |  
         TimeUnit: a TimeUnit determining how to interpret the timeout parameter |  
       
| Returns | |
|---|---|
E |  
        the head of this queue, or null if the specified waiting time elapses before an element is available |  
       
| Throws | |
|---|---|
InterruptedException |  
        if interrupted while waiting | 
void put (E e)
将指定的元素插入此队列,等待空间变得可用时。
| Parameters | |
|---|---|
e |  
         E: the element to add |  
       
| Throws | |
|---|---|
InterruptedException |  
        if interrupted while waiting | 
ClassCastException |  
        if the class of the specified element prevents it from being added to this queue | 
NullPointerException |  
        if the specified element is null | 
IllegalArgumentException |  
        if some property of the specified element prevents it from being added to this queue | 
int remainingCapacity ()
如果没有内部限制,则返回此队列理想情况下(在没有内存或资源约束的情况下)接受而没有阻塞的附加元素的数量,或 Integer.MAX_VALUE 。
请注意,您 不能总是通过检查 remainingCapacity来判断尝试插入元素是否成功,因为可能是另一个线程即将插入或删除元素。
| Returns | |
|---|---|
int |  
        the remaining capacity | 
boolean remove (Object o)
从该队列中移除指定元素的单个实例(如果存在)。 更正式地说,删除一个元素e ,使得o.equals(e) ,如果这个队列包含一个或多个这样的元素。 如果此队列包含指定的元素(或者等价地,如果此队列因呼叫而改变),则返回true 。
| Parameters | |
|---|---|
o |  
         Object: element to be removed from this queue, if present |  
       
| Returns | |
|---|---|
boolean |  
        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) | 
NullPointerException |  
        if the specified element is null (optional) | 
E take ()
检索并删除此队列的头部,如果需要等待,直到元素变为可用。
| Returns | |
|---|---|
E |  
        the head of this queue | 
| Throws | |
|---|---|
InterruptedException |  
        if interrupted while waiting |