IBlockingQueue 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
一个 , Queue
它另外支持在检索元素时等待队列变为非空的操作,并在存储元素时等待队列中的可用空间。
[Android.Runtime.Register("java/util/concurrent/BlockingQueue", "", "Java.Util.Concurrent.IBlockingQueueInvoker")]
[Java.Interop.JavaTypeParameters(new System.String[] { "E" })]
public interface IBlockingQueue : IDisposable, Java.Interop.IJavaPeerable, Java.Util.IQueue
[<Android.Runtime.Register("java/util/concurrent/BlockingQueue", "", "Java.Util.Concurrent.IBlockingQueueInvoker")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "E" })>]
type IBlockingQueue = interface
interface IQueue
interface ICollection
interface IIterable
interface IJavaObject
interface IDisposable
interface IJavaPeerable
- 派生
- 属性
- 实现
注解
一个 , Queue
它另外支持在检索元素时等待队列变为非空的操作,并在存储元素时等待队列中的可用空间。
BlockingQueue
方法有四种形式,具有不同的处理操作方式,这些操作不能立即得到满足,但在将来的某个时间点可能满足:一个会引发异常,第二个方法返回一个特殊值 (null
或 false
,根据操作) ,第三个方法无限期地阻止当前线程,直到操作成功, 和第四个块仅在给定的最大时间限制后放弃。 下表汇总了这些方法:
<table class=“plain”>描述文字Summary of BlockingQueue methods</描述文字<>tr<>td></td><th scope=“col” style=“font-weight:normal; font-style:italic”>Throws exception</th<>scope=“col” style=“font-weight:normal; font-style:italic”>Special value</th><th scope=“col” style=“font-weight:normal; font-style:italic”>Blocks</th><><th scope=“col” style=“font-weight:normal; font-style:italic”>Times out</th></tr><tr><th scope=“row” style=“text-align:left”>Insert</th><td>#add(Object) add(e)
</td><td><#offer(Object) offer(e)
/td><td>#put(Object) put(e)
</td><td>#offer(Object, long, TimeUnit) offer(e, time, unit)
</td></tr><<>th scope=“row” style=“text-align:left”>Remove</th><td>/td><td>#poll() poll()
</td><td>#take() take()
</td><td>#poll(long, TimeUnit) poll(time, unit)
</td></tr><><th scope=“row” style=“text-align:left”>Examine</th<>td#element() element()
></td td<<#peek() peek()
>>/td<>td style=“font-style: italic”>not applicable</td<>td style=“font-style: italic”>not applicable</td></tr #remove() remove()
<></表>
不接受 BlockingQueue
null
元素。 实现在尝试 add
、 put
或 offer
null
时引发NullPointerException
。 null
用作 sentinel 值来指示操作失败poll
。
可能是 BlockingQueue
容量限制。 在任何给定时间,它都有 一个 remainingCapacity
,超过此时间后,任何其他元素都 put
不能不阻止。 BlockingQueue
没有任何内部容量约束的 始终报告 剩余容量Integer.MAX_VALUE
。
BlockingQueue
实现主要用于生成者-使用者队列,但另外还支持 Collection
接口。 因此,例如,可以使用 从队列 remove(x)
中删除任意元素。 但是,此类操作通常<>不会<>非常高效地执行,并且仅用于偶尔使用,例如在取消排队消息时。
BlockingQueue
实现是线程安全的。 所有队列方法使用内部锁或其他形式的并发控制以原子方式实现其效果。 但是,除非在实现中<另有指定,否则 em bulk</>em> 集合操作 addAll
、 containsAll
retainAll
和 removeAll
不><<一定以原子方式执行。> 因此,例如,在 中c
仅添加一些元素后,addAll(c)
(引发异常) 可能会失败。
A BlockingQueue
在<>本质上不支持<>任何类型的 &商;close"或 "shutdown"操作,指示不再添加任何项。 此类功能的需求和用法往往依赖于实现。 例如,一种常见的策略是,生成者插入特殊的 <em>end-of-stream</em> 或 <em>poison</em> 对象,这些对象在使用者采用时会相应地进行解释。
使用示例,基于典型的生成者-使用者方案。 请注意, BlockingQueue
可以安全地与多个生成者和多个使用者一起使用。
{@code
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();
}
}}
内存一致性影响:与其他并发集合一样,在将对象放入 i>happen-before</i> 操作之后,线程中的操作会从BlockingQueue
另一个BlockingQueue
<线程的 中访问或删除该元素。
此接口是 Java 集合框架的成员。
在 1.5 中添加。
的 java.util.concurrent.BlockingQueue
Java 文档。
此页面的某些部分是基于 创建和共享的工作进行的修改,并根据 署名许可中所述的条款使用。
属性
Handle |
获取基础 Android 对象的 JNI 值。 (继承自 IJavaObject) |
IsEmpty |
如果它 |
JniIdentityHashCode |
返回包装实例的 的值 |
JniManagedPeerState |
托管对等方的状态。 (继承自 IJavaPeerable) |
JniPeerMembers |
成员访问和调用支持。 (继承自 IJavaPeerable) |
PeerReference |
返回 JniObjectReference 包装的 Java 对象实例的 。 (继承自 IJavaPeerable) |
方法
Add(Object) |
如果可以立即在不违反容量限制的情况下将指定的元素插入到此队列中,成功后返回 |
AddAll(ICollection) |
将指定集合中的所有元素添加到此集合 (可选操作) 。 (继承自 ICollection) |
Clear() |
从此集合中删除所有元素, (可选操作) 。 (继承自 ICollection) |
Contains(Object) |
|
ContainsAll(ICollection) |
|
Disposed() |
在释放实例时调用。 (继承自 IJavaPeerable) |
DisposeUnlessReferenced() |
如果没有对此实例的未完成引用,则调用 |
DrainTo(ICollection) |
从此队列中删除所有可用元素,并将其添加到给定集合中。 |
DrainTo(ICollection, Int32) |
从此队列中删除最多给定数量的可用元素,并将其添加到给定集合中。 |
Element() |
检索(但不删除)此队列的头。 (继承自 IQueue) |
Equals(Object) |
将指定的 对象与此集合相等性进行比较。 (继承自 ICollection) |
Finalized() |
在实例完成时调用。 (继承自 IJavaPeerable) |
ForEach(IConsumer) |
对 的每个元素执行给定操作, |
GetHashCode() |
返回此集合的哈希代码值。 (继承自 ICollection) |
Iterator() |
返回此集合中元素的迭代器。 (继承自 ICollection) |
Offer(Object) |
如果可以立即执行此操作而不违反容量限制,则在成功时返回 |
Offer(Object, Int64, TimeUnit) |
将指定的元素插入此队列,并在必要时等待指定的等待时间,以便空间变得可用。 |
Peek() |
检索但不删除此队列的头,如果此队列为空,则 |
Poll() |
检索并删除此队列的头,如果此队列为空,则返回 |
Poll(Int64, TimeUnit) |
检索并删除此队列的头,并在必要时等待指定的等待时间,使元素变得可用。 |
Put(Object) |
将指定的元素插入此队列,并在必要时等待可用空间。 |
RemainingCapacity() |
返回在没有内存或资源约束的情况下,如果不存在内存或资源约束,或者没有固有限制, |
Remove() |
检索并删除此队列的头。 (继承自 IQueue) |
Remove(Object) |
从此队列中删除指定元素的单个实例(如果存在)。 |
RemoveAll(ICollection) |
删除此集合的所有元素,这些元素也包含在指定集合中, (可选操作) 。 (继承自 ICollection) |
RemoveIf(IPredicate) |
删除此集合中满足给定谓词的所有元素。 (继承自 ICollection) |
RetainAll(ICollection) |
仅保留指定集合中包含的此集合中的元素, (可选操作) 。 (继承自 ICollection) |
SetJniIdentityHashCode(Int32) |
设置 返回 |
SetJniManagedPeerState(JniManagedPeerStates) |
一个 , |
SetPeerReference(JniObjectReference) |
设置 返回 |
Size() |
返回此集合中的元素数。 (继承自 ICollection) |
Spliterator() |
|
Take() |
检索并删除此队列的头,并在必要时等待元素可用。 |
ToArray() |
返回一个数组,其中包含此集合中的所有元素。 (继承自 ICollection) |
ToArray(IIntFunction) |
返回一个数组,该数组包含此集合中的所有元素,并使用提供的 |
ToArray(Object[]) |
返回一个数组,其中包含此集合中的所有元素;返回数组的运行时类型为指定数组的运行时类型。 (继承自 ICollection) |
UnregisterFromRuntime() |
取消注册此实例,以便运行时不会从将来 Java.Interop.JniRuntime+JniValueManager.PeekValue 的调用中返回它。 (继承自 IJavaPeerable) |
显式接口实现
IIterable.Spliterator() |
|
扩展方法
JavaCast<TResult>(IJavaObject) |
执行 Android 运行时检查的类型转换。 |
JavaCast<TResult>(IJavaObject) |
一个 , |
GetJniTypeName(IJavaPeerable) |
一个 , |
OfferAsync(IBlockingQueue, Object) |
一个 , |
OfferAsync(IBlockingQueue, Object, Int64, TimeUnit) |
一个 , |
PollAsync(IBlockingQueue, Int64, TimeUnit) |
一个 , |
PutAsync(IBlockingQueue, Object) |
一个 , |
TakeAsync(IBlockingQueue) |
一个 , |
ToEnumerable(IIterable) |
一个 , |
ToEnumerable<T>(IIterable) |
一个 , |