`
andy54321
  • 浏览: 434575 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

接口 BlockingQueue<E>

    博客分类:
  • J2EE
阅读更多

java.util.concurrent
接口 BlockingQueue<E>

类型参数:
E - 在此 collection 中保持的元素类型
所有超级接口:
Collection <E>, Iterable <E>, Queue <E>
所有已知实现类:
ArrayBlockingQueue , DelayQueue , LinkedBlockingQueue , PriorityBlockingQueue , SynchronousQueue

public interface BlockingQueue<E>
extends Queue <E>

支持两个附加操作的 Queue ,这两个操作是:检索元素时等待队列变为非空,以及存储元素时等待空间变得可用。

BlockingQueue 不接受 null 元素。试图 addputoffer 一个 null 元素时,某些实现会抛出 NullPointerExceptionnull 被用作指示 poll 操作失败的警戒值。

BlockingQueue 可以是限定容量的。它在任意给定时间都可以有一个 remainingCapacity ,超出此容量,便无法无阻塞地 put 额外的元素。没有任何内部容量约束的 BlockingQueue 总是报告 Integer.MAX_VALUE 的剩余容量。

BlockingQueue 实现主要用于生产者-使用者队列,但它另外还支持 Collection 接口。因此,举例来说,使用 remove(x) 从队列中移除任意一个元素是有可能的。然而,这种操作通常 会有效执行,只能有计划地偶尔使用,比如在取消排队信息时。

BlockingQueue 实现是线程安全的。所有排队方法都可以使用内部锁定或其他形式的并发控制来自动达到它们的目的。然而,大量的 Collection 操作(addAllcontainsAllretainAllremoveAll没有 必要自动执行,除非在实现中特别说明。因此,举例来说,在只添加了 c 中的一些元素后,addAll(c) 有可能失败(抛出一个异常)。

BlockingQueue 实质上 支持使用任何一种“close”或“shutdown”操作来指示不再添加任何项。这种功能的需求和使用有依赖于实现的倾向。例如,一种常用的策略是:对于生产者,插入特殊的 end-of-streampoison 对象,并根据使用者获取这些对象的时间来对它们进行解释。

以下是基于典型的生产者-使用者场景的一个用例。注意,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();
   }
 }
 

此接口是 Java Collections Framework 的成员。

 

 

从以下版本开始:
1.5

<!-- ========== METHOD SUMMARY =========== --> <!-- -->

方法摘要
 boolean add (E  o)
          将指定的元素添加到此队列中(如果立即可行),在成功时返回 true ,其他情况则抛出 IllegalStateException。
 int drainTo (Collection <? super E > c)
          移除此队列中所有可用的元素,并将它们添加到给定 collection 中。
 int drainTo (Collection <? super E > c, int maxElements)
          最多从此队列中移除给定数量的可用元素,并将这些元素添加到给定 collection 中。
 boolean offer (E  o)
          如果可能的话,将指定元素插入此队列中。
 boolean offer (E  o, long timeout, TimeUnit  unit)
          将指定的元素插入此队列中,如果没有可用空间,将等待指定的等待时间(如果有必要)。
 E poll (long timeout, TimeUnit  unit)
          检索并移除此队列的头部,如果此队列中没有任何元素,则等待指定等待的时间(如果有必要)。
 void put (E  o)
          将指定元素添加到此队列中,如果没有可用空间,将一直等待(如果有必要)。
 int remainingCapacity ()
          返回在无阻塞的理想情况下(不存在内存或资源约束)此队列能接受的元素数量;如果没有内部限制,则返回 Integer.MAX_VALUE
 E take ()
          检索并移除此队列的头部,如果此队列不存在任何元素,则一直等待。

 <!-- -->  

分享到:
评论

相关推荐

    LinkedBlockingQueue 和 ConcurrentLinkedQueue的区别.docx

    BlockingQueue&lt;Integer&gt; boundedQueue = new LinkedBlockingQueue&lt;&gt;(100); 当然,我们也可以通过不指定大小,来创建一个无界的 LinkedBlockingQueue : BlockingQueue&lt;Integer&gt; unboundedQueue = new ...

    Java JDK实例宝典

    19 线程——BlockingQueue &lt;br&gt;第17章 Java与XML &lt;br&gt;17. 1 用DOM处理XML文档 &lt;br&gt;17. 2 用SAX处理XML文档 &lt;br&gt;17. 3 用XSLT转换XML &lt;br&gt;17. 4 对象与XML的转换 &lt;br&gt;第18章 Java Mail &lt;br&gt;18. 1 使用SMTP协议发送...

    RustBlockingQueue:线程安全队列,在空时阻止出队

    RustBlockingQueue ... Create new empty BlockingQueue&lt;T&gt;. 2. en_q(&self, t: T) -&gt; Result&lt;()&gt; Push_back t into internal VecDec&lt;T&gt;. 3. de_q(&self) -&gt; Result&lt;T&gt;; Pop_front t from internal VecDec&lt;T&gt;.

    线程池原理-ThreadPoolExecutor源码解析

    线程池原理-ThreadPoolExecutor源码解析 1.构造方法及参数 2.阻塞对列: BlockingQueue&lt;Runnable&gt; 3.线程工厂: DefaultThreadFactory 4.拒绝策略: RejectedExecutionHandler 5.执行线程 Executor

    disruptor:Disruptor BlockingQueue

    Conversant Concurrent... $ mvn -U clean package Conversant Disruptor 在 Maven Central 上对于 Java 9 及更高版本: &lt;dependency&gt; &lt;groupId&gt;com.conversantmedia&lt;/groupId&gt; &lt;artifactId&gt;disruptor&lt;/artifactId&gt;

    第7章-JUC多线程v1.1.pdf

    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue&lt;Runnable&gt; workQueu, ThreadFactory threadFactory,RejectedExecutionHandler handler);...

    zp-concurrent-lib:我自己编写的一些高性能并发库

    zp-concurrent-lib ...&lt;artifactId&gt;zp-concurrent-lib&lt;/artifactId&gt; &lt;version&gt;1.0&lt;/version&gt; 目前实现的类 com.lzp.util.concurrent.threadpool com.lzp.util.concurrent.blockingQueue.withlock ...

    java线程池概念.txt

    BlockingQueue&lt;Runnable&gt; workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize &lt; 0 || maximumPoolSize &lt;= 0 || maximumPoolSize &lt; corePoolSize || ...

    线程----BlockingQueue

    线程----BlockingQueue 的介绍说明

    goncurrent:Golang之类的频道并选择Java

    Chan&lt;Integer&gt; ch1 = Chan.create(1) // '1' is the buffer length of the channelch1.send(10); Integer value = ch1.receive();通道已满时,send()会阻塞。 当通道为空时,receive()块。无缓冲通道可以通过将...

    Linux C++ 使用condition实现阻塞队列的方法

    实例如下: ...#include &lt;iostream&gt; #include &lt;pthread&gt; using namespace std; //template &lt;typename&gt; class BlockingQueue { public: BlockingQueue(); BlockingQueue(int capacity); ~Blocking

    BlockingQueue(阻塞队列)详解

    在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。本文详细介绍了BlockingQueue...

    BlockingQueue

    BlockingQueue java 的工具类,初次要用于消费者,生产者的同步问题。

    Java容器.xmind

    Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet​() 线程不安全,速度快,允许存放null键,null值。 SortedMap 标记: class TreeMap 对键进行排序 HashTable 标记: class Properties 标记: class 线程安全,速度慢,不允许...

    C++写的跨平台BlockingQueue

    类似java BlockingQueue,C++写的,支持Windows与Linux。

    简单实现BlockingQueue,BlockingQueue源码详解

    简单实现BlockingQueue,BlockingQueue源码详解

    BlockingQueue的使用

    这个demo主要讲解了BlockingQueue的使用希望可以帮户需要的同学.

    BlockingQueue队列自定义超时时间取消线程池任务

    定义全局线程池,将用户的请求放入自定义队列中,排队等候线程调用,等待超时则自动取消该任务,实现超时可取消的异步任务

    14-阻塞队列BlockingQueue实战及其原理分析二.pdf

    14-阻塞队列BlockingQueue实战及其原理分析二.pdf

    阻塞队列BlockingQueue的使用

    本文简要介绍下BlockingQueue接口中几个方法的作用及区别。 boolean add(E e) (1)在不违反容量限制的情况下,可立即将指定元素插入此队列,成功返回true。 (2)当无可用空间时候,抛出IllegalStateException异常 ...

Global site tag (gtag.js) - Google Analytics