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
元素。试图 add
、put
或 offer
一个 null
元素时,某些实现会抛出 NullPointerException
。null
被用作指示 poll
操作失败的警戒值。
BlockingQueue
可以是限定容量的。它在任意给定时间都可以有一个 remainingCapacity
,超出此容量,便无法无阻塞地 put
额外的元素。没有任何内部容量约束的 BlockingQueue
总是报告 Integer.MAX_VALUE
的剩余容量。
BlockingQueue
实现主要用于生产者-使用者队列,但它另外还支持 Collection
接口。因此,举例来说,使用 remove(x)
从队列中移除任意一个元素是有可能的。然而,这种操作通常不
会有效执行,只能有计划地偶尔使用,比如在取消排队信息时。
BlockingQueue
实现是线程安全的。所有排队方法都可以使用内部锁定或其他形式的并发控制来自动达到它们的目的。然而,大量的
Collection 操作(addAll
、containsAll
、retainAll
和 removeAll
)没有
必要自动执行,除非在实现中特别说明。因此,举例来说,在只添加了 c
中的一些元素后,addAll(c)
有可能失败(抛出一个异常)。
BlockingQueue
实质上不
支持使用任何一种“close”或“shutdown”操作来指示不再添加任何项。这种功能的需求和使用有依赖于实现的倾向。例如,一种常用的策略是:对于生产者,插入特殊的 end-of-stream
或 poison
对象,并根据使用者获取这些对象的时间来对它们进行解释。
以下是基于典型的生产者-使用者场景的一个用例。注意,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
()
检索并移除此队列的头部,如果此队列不存在任何元素,则一直等待。 |
<!-- -->
分享到:
相关推荐
BlockingQueue<Integer> boundedQueue = new LinkedBlockingQueue<>(100); 当然,我们也可以通过不指定大小,来创建一个无界的 LinkedBlockingQueue : BlockingQueue<Integer> unboundedQueue = new ...
19 线程——BlockingQueue <br>第17章 Java与XML <br>17. 1 用DOM处理XML文档 <br>17. 2 用SAX处理XML文档 <br>17. 3 用XSLT转换XML <br>17. 4 对象与XML的转换 <br>第18章 Java Mail <br>18. 1 使用SMTP协议发送...
RustBlockingQueue ... Create new empty BlockingQueue<T>. 2. en_q(&self, t: T) -> Result<()> Push_back t into internal VecDec<T>. 3. de_q(&self) -> Result<T>; Pop_front t from internal VecDec<T>.
线程池原理-ThreadPoolExecutor源码解析 1.构造方法及参数 2.阻塞对列: BlockingQueue<Runnable> 3.线程工厂: DefaultThreadFactory 4.拒绝策略: RejectedExecutionHandler 5.执行线程 Executor
Conversant Concurrent... $ mvn -U clean package Conversant Disruptor 在 Maven Central 上对于 Java 9 及更高版本: <dependency> <groupId>com.conversantmedia</groupId> <artifactId>disruptor</artifactId>
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueu, ThreadFactory threadFactory,RejectedExecutionHandler handler);...
zp-concurrent-lib ...<artifactId>zp-concurrent-lib</artifactId> <version>1.0</version> 目前实现的类 com.lzp.util.concurrent.threadpool com.lzp.util.concurrent.blockingQueue.withlock ...
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || ...
线程----BlockingQueue 的介绍说明
Chan<Integer> ch1 = Chan.create(1) // '1' is the buffer length of the channelch1.send(10); Integer value = ch1.receive();通道已满时,send()会阻塞。 当通道为空时,receive()块。无缓冲通道可以通过将...
实例如下: ...#include <iostream> #include <pthread> using namespace std; //template <typename> class BlockingQueue { public: BlockingQueue(); BlockingQueue(int capacity); ~Blocking
在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。本文详细介绍了BlockingQueue...
BlockingQueue java 的工具类,初次要用于消费者,生产者的同步问题。
Set<Map.Entry<K,V>> entrySet() 线程不安全,速度快,允许存放null键,null值。 SortedMap 标记: class TreeMap 对键进行排序 HashTable 标记: class Properties 标记: class 线程安全,速度慢,不允许...
类似java BlockingQueue,C++写的,支持Windows与Linux。
简单实现BlockingQueue,BlockingQueue源码详解
这个demo主要讲解了BlockingQueue的使用希望可以帮户需要的同学.
定义全局线程池,将用户的请求放入自定义队列中,排队等候线程调用,等待超时则自动取消该任务,实现超时可取消的异步任务
14-阻塞队列BlockingQueue实战及其原理分析二.pdf
本文简要介绍下BlockingQueue接口中几个方法的作用及区别。 boolean add(E e) (1)在不违反容量限制的情况下,可立即将指定元素插入此队列,成功返回true。 (2)当无可用空间时候,抛出IllegalStateException异常 ...