BlockingQueue

发布时间 2023-11-01 15:02:53作者: anpeiyong

概述

{@code BlockingQueue} methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future:
one throws an exception,
the second returns a special value (either null or false, depending on the operation),
the third blocks the current thread indefinitely until the operation can succeed,
and the fourth blocks for only a given maximum time limit before giving up.

BlockingQueue的方法 有四种形式,它们具有不同的操作处理方式,这些操作不能立即满足,但可能会在将来的某个时间点得到满足:
1、抛出异常,
2、返回一个特殊值(null 或 false,具体取决于操作),
3、无限期地阻塞当前线程,直到操作成功,
4、阻塞直到在timeout后放弃

 

A {@code BlockingQueue} does not accept {@code null} elements.
Implementations throw {@code NullPointerException} on attempts to {@code add}, {@code put} or {@code offer} a {@code null}.
A {@code null} is used as a sentinel value to indicate failure of {@code poll} operations.

BlockingQueue不接受 null元素
尝试 add、put或 offer时抛出npe。
null用作哨兵值,以指示 poll 操作失败。

 

A {@code BlockingQueue} may be capacity bounded.
At any given time it may have a {@code remainingCapacity} beyond which no additional elements can be {@code put} without blocking.
A {@code BlockingQueue} without any intrinsic capacity constraints always reports a remaining capacity of {@code Integer.MAX_VALUE}.

{@code BlockingQueue} 可能受容量限制
在任何给定时间,它可能有一个 {@code remainingCapacity},超过这个容量,就不能再{放置@code其他元素了。
没有任何内部容量约束的 {@code BlockingQueue} 始终报告剩余容量为 {@code Integer.MAX_VALUE}。

 

{@code BlockingQueue} implementations are designed to be used primarily for producer-consumer queues, but additionally support the {@link java.util.Collection} interface.
So, for example, it is possible to remove an arbitrary element from a queue using {@code remove(x)}.
However, such operations are in general not performed very efficiently, and are intended for only occasional use, such as when a queued message is cancelled.

BlockingQueue主要用于生产者-使用者队列,但也支持 {@link java.util.Collection} 接口。
因此,例如,可以使用 {@code remove(x)} 从队列中删除任意元素。
但是,此类操作通常执行效率不高,并且仅供偶尔使用,例如取消排队消息时;

 

{@code BlockingQueue} implementations are thread-safe.
All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.
However, the bulk Collection operations {@code addAll}, {@code containsAll}, {@code retainAll} and {@code removeAll} are not necessarily performed atomically unless specified otherwise in an implementation.
So it is possible, for example, for {@code addAll(c)} to fail (throwing an exception) after adding only some of the elements in {@code c}.

BlockingQueue实现是线程安全的。
所有排队方法都使用内部锁或其他形式的并发控制原子方式实现其效果。
但是,除非在实现中另有指定,否则批量收集操作 {@code addAll}、{@code containsAll}、{@code retainAll} 和 {@code removeAll} 不一定以原子方式执行。
因此,例如,{@code addAll(c)} 在仅添加 {@code c} 中的某些元素后可能会失败(引发异常);

 

一端生产,一端消费

 

使用示例

/*
     * 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();
     *   }
     * }
     */

  

使用场景

  生产者-消费者场景