<wbr style="line-height:25px">一个无界的阻塞队列</wbr><wbr style="line-height:25px">,它使用与类PriorityQueue相同的顺序规则,并且提供了阻塞检索的操作。<br style="line-height:25px"> 虽然此队列逻辑上是无界的,但是由于资源被耗尽,所以试图执行添加操作可能会失败(导致OutOfMemoryError)。<br style="line-height:25px"> 此类不允许使用null元素。依赖自然顺序的优先级队列也不允许插入不可比较的对象(因为这样做会抛出ClassCastException)。</wbr>
Operations on this class make no guarantees about the ordering of elements with equal priority. If you need to enforce an ordering, you can define custom classes or comparators that use a secondary key to break ties in primary priority values. For example, here is a class that applies first-in-first-out tie-breaking to comparable elements. To use it, you would insert anew FIFOEntry(anEntry)
instead of a plain entry object.
构造函数class FIFOEntry< super="" e="" >>
implements Comparable> {
static final AtomicLong seq = new AtomicLong(0);
final long seqNum;
final E entry;
public FIFOEntry(E entry) {
seqNum = seq.getAndIncrement();
this.entry = entry;
public E getEntry() { return entry; }
public int compareTo(FIFOEntry other) {
int res = entry.compareTo(other.entry);
if (res == 0 && other.entry != this.entry)
res = (seqNum < other.seqNum ? -1 : 1);
return res;
}
}}
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/PriorityBlockingQueue.html#PriorityBlockingQueue()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">PriorityBlockingQueue</a></span>()</nobr>
Creates a
PriorityBlockingQueue with the default initial capacity (11) that orders its elements according to their
natural ordering .
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/PriorityBlockingQueue.html#PriorityBlockingQueue(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">PriorityBlockingQueue</a></span>(int initialCapacity)</nobr>
Creates a
PriorityBlockingQueue with the specified initial capacity that orders its elements according to their
natural ordering .
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/PriorityBlockingQueue.html#PriorityBlockingQueue(int,%20java.util.Comparator<?%20super%20E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">PriorityBlockingQueue</a></span>(int initialCapacity,<a rel="nofollow" href="http://developer.android.com/reference/java/util/Comparator.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Comparator</a><?superE> comparator)</nobr>
Creates a
PriorityBlockingQueue with the specified initial capacity that orders its elements according to the specified comparator.
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/PriorityBlockingQueue.html#PriorityBlockingQueue(java.util.Collection<?%20extends%20E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">PriorityBlockingQueue</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Collection</a><?extendsE> c)</nobr>
Creates a
PriorityBlockingQueue containing the elements in the specified collection.
|
<wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px">:它是无界阻塞队列,容量是无限的,它使用与类PriorityQueue相同的顺序规则。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:它是线程安全的,是阻塞的<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:不允许使用null元素。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:对于put(Eo)和offer(Eo,longtimeout,TimeUnitunit),由于该队列是无界的,所以此方法永远不会阻塞。<br style="line-height:25px"> 因此参数timeout和unit没意义,会被忽略掉。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:iterator()方法中所提供的迭代器并不保证以特定的顺序遍历PriorityBlockingQueue的元素。<br style="line-height:25px"> 如果需要有序地遍历,则应考虑使用Arrays.sort(pq.toArray())。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意6</wbr></span><wbr style="line-height:25px">:关于<span style="color:#993300; line-height:25px">PriorityBlockingQueue</span>的排序原理请参照《</wbr></wbr></wbr></wbr></wbr></wbr>
PriorityQueue
》
至于使用和别的BlockingQueue(ArrayBlockingQueue,LinkedBlockingQueue)相似,可以参照它们。
<wbr style="line-height:25px">注意7</wbr><wbr style="line-height:25px">:此类及其迭代器实现了Collection和Iterator接口的所有可选方法。</wbr><wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px">注意7</wbr><wbr style="line-height:25px">:此类及其迭代器实现了Collection和Iterator接口的所有可选方法。</wbr><wbr style="line-height:25px"></wbr>