|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.PriorityBlockingQueue<E>
E - 此 collection 中所保存元素的类型public class PriorityBlockingQueue<E>
一个无界阻塞队列,它使用与类 PriorityQueue 相同的顺序规则,并且提供了阻塞获取操作。虽然此队列逻辑上是无界的,但是资源被耗尽时试图执行 add 操作也将失败(导致 OutOfMemoryError)。此类不允许使用 null 元素。依赖自然顺序的优先级队列也不允许插入不可比较的对象(这样做会导致抛出 ClassCastException)。
此类及其迭代器可以实现 Collection 和 Iterator 接口的所有可选 方法。iterator() 方法中提供的迭代器并不 保证以特定的顺序遍历 PriorityBlockingQueue 的元素。如果需要有序地进行遍历,则应考虑使用 Arrays.sort(pq.toArray())。此外,可以使用方法 drainTo 按优先级顺序移除 全部或部分元素,并将它们放在另一个 collection 中。
在此类上进行的操作不保证具有同等优先级的元素的顺序。如果需要实施某一排序,那么可以定义自定义类或者比较器,比较器可使用修改键断开主优先级值之间的联系。例如,以下是应用先进先出 (first-in-first-out) 规则断开可比较元素之间联系的一个类。要使用该类,则需要插入一个新的 FIFOEntry(anEntry) 来替换普通的条目对象。
class FIFOEntry<E extends Comparable<? super E>>
implements Comparable<FIFOEntry<E>> {
final static AtomicLong seq = new AtomicLong();
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<E> other) {
int res = entry.compareTo(other.entry);
if (res == 0 && other.entry != this.entry)
res = (seqNum < other.seqNum ? -1 : 1);
return res;
}
}
此类是 Java Collections Framework 的成员。
| 构造方法摘要 | |
|---|---|
PriorityBlockingQueue()
用默认的初始容量 (11) 创建一个 PriorityBlockingQueue,并根据元素的自然顺序对其元素进行排序。 |
|
PriorityBlockingQueue(Collection<? extends E> c)
创建一个包含指定 collection 元素的 PriorityBlockingQueue。 |
|
PriorityBlockingQueue(int initialCapacity)
使用指定的初始容量创建一个 PriorityBlockingQueue,并根据元素的自然顺序对其元素进行排序。 |
|
PriorityBlockingQueue(int initialCapacity,
Comparator<? super E> comparator)
使用指定的初始容量创建一个 PriorityBlockingQueue,并根据指定的比较器对其元素进行排序。 |
|
| 方法摘要 | ||
|---|---|---|
boolean |
add(E e)
将指定元素插入此优先级队列。 |
|
void |
clear()
完全移除此队列中的所有元素。 |
|
Comparator<? super E> |
comparator()
返回用于对此队列元素进行排序的比较器;如果此队列使用其元素的自然顺序,则返回 null。 |
|
boolean |
contains(Object o)
如果队列包含指定的元素,则返回 true。 |
|
int |
drainTo(Collection<? super E> c)
移除此队列中所有可用的元素,并将它们添加到给定 collection 中。 |
|
int |
drainTo(Collection<? super E> c,
int maxElements)
最多从此队列中移除给定数量的可用元素,并将这些元素添加到给定 collection 中。 |
|
Iterator<E> |
iterator()
返回在此队列元素上进行迭代的迭代器。 |
|
boolean |
offer(E e)
将指定元素插入此优先级队列。 |
|
boolean |
offer(E e,
long timeout,
TimeUnit unit)
将指定元素插入此优先级队列。 |
|
E |
peek()
获取但不移除此队列的头;如果此队列为空,则返回 null。 |
|
E |
poll()
获取并移除此队列的头,如果此队列为空,则返回 null。 |
|
E |
poll(long timeout,
TimeUnit unit)
获取并移除此队列的头部,在指定的等待时间前等待可用的元素(如果有必要)。 |
|
void |
put(E e)
将指定元素插入此优先级队列。 |
|
int |
remainingCapacity()
总是返回 Integer.MAX_VALUE,因为 PriorityBlockingQueue 没有容量限制。 |
|
boolean |
remove(Object o)
从队列中移除指定元素的单个实例(如果存在)。 |
|
int |
size()
返回此 collection 中的元素数。 |
|
E |
take()
获取并移除此队列的头部,在元素变得可用之前一直等待(如果有必要)。 |
|
Object[] |
toArray()
返回包含此队列所有元素的数组。 |
|
|
toArray(T[] a)
返回一个包含此队列所有元素的数组;返回数组的运行时类型是指定数组的类型。 |
|
String |
toString()
返回此 collection 的字符串表示形式。 |
|
| 从类 java.util.AbstractQueue 继承的方法 |
|---|
addAll, element, remove |
| 从类 java.util.AbstractCollection 继承的方法 |
|---|
containsAll, isEmpty, removeAll, retainAll |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 从接口 java.util.Queue 继承的方法 |
|---|
element, remove |
| 从接口 java.util.Collection 继承的方法 |
|---|
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll |
| 构造方法详细信息 |
|---|
public PriorityBlockingQueue()
public PriorityBlockingQueue(int initialCapacity)
initialCapacity - 此优先级队列的初始容量
IllegalArgumentException - 如果 initialCapacity 小于 1
public PriorityBlockingQueue(int initialCapacity,
Comparator<? super E> comparator)
initialCapacity - 此优先级队列的初始容量comparator - 用于对此优先级队列进行排序的比较器。如果参数为 null,则根据元素的自然顺序进行排序
IllegalArgumentException - 如果 initialCapacity 小于 1public PriorityBlockingQueue(Collection<? extends E> c)
SortedSet 或 PriorityQueue,则此优先级队列将按照相同顺序进行排序。否则,此优先级队列将根据此元素的自然顺序进行排序。
c - 其元素将被放置在此优先级队列中的 collection
ClassCastException - 如果根据优先级队列的顺序,指定 collection 的元素无法与另一个 collection 的元素进行比较
NullPointerException - 如果指定 collection 或其任意元素为 null| 方法详细信息 |
|---|
public boolean add(E e)
Collection<E> 中的 addBlockingQueue<E> 中的 addQueue<E> 中的 addAbstractQueue<E> 中的 addo - 要添加的元素
Collection.add(E) 的规定)
ClassCastException - 如果根据优先级队列的排序规则,无法将指定的元素与优先级队列中当前的元素进行比较
NullPointerException - 如果指定元素为 nullpublic boolean offer(E e)
BlockingQueue<E> 中的 offerQueue<E> 中的 offere - 将添加的元素
Queue.offer(E) 的规定)
ClassCastException - 如果根据优先级队列的排序规则无法将指定元素与优先级队列中当前的元素进行比较
NullPointerException - 如果指定元素为 nullpublic void put(E e)
BlockingQueue<E> 中的 pute - 要添加的元素
ClassCastException - 如果根据优先级队列的排序规则无法将指定元素与优先级队列中当前的元素进行比较
NullPointerException - 如果指定元素为 null
public boolean offer(E e,
long timeout,
TimeUnit unit)
BlockingQueue<E> 中的 offere - 要添加的元素timeout - 忽略此参数,因为此方法不会阻塞unit - 忽略此参数,因为此方法不会阻塞
ClassCastException - 如果根据优先级队列的排序规则无法将指定元素与优先级队列中当前的元素进行比较
NullPointerException - 如果指定元素为 nullpublic E poll()
Queue 复制的描述
Queue<E> 中的 poll
public E take()
throws InterruptedException
BlockingQueue 复制的描述
BlockingQueue<E> 中的 takeInterruptedException - 如果在等待时被中断
public E poll(long timeout,
TimeUnit unit)
throws InterruptedException
BlockingQueue 复制的描述
BlockingQueue<E> 中的 polltimeout - 放弃之前要等待的时间长度,用 unit 的时间单位表示unit - 确定如何解释 timeout 参数的 TimeUnit
InterruptedException - 如果在等待时被中断public E peek()
Queue 复制的描述
Queue<E> 中的 peekpublic Comparator<? super E> comparator()
public int size()
Collection 复制的描述
Collection<E> 中的 sizeAbstractCollection<E> 中的 sizepublic int remainingCapacity()
BlockingQueue<E> 中的 remainingCapacitypublic boolean remove(Object o)
o.equals(e) 的元素 e,则移除一个这样的元素。当且仅当此队列包含指定的元素(或者此队列由于调用而发生更改)时,返回 true。
Collection<E> 中的 removeBlockingQueue<E> 中的 removeAbstractCollection<E> 中的 removeo - 将从此队列中移除的元素(如果存在)
public boolean contains(Object o)
true。更确切地讲,当且仅当此队列至少包含一个满足 o.equals(e) 的元素 e 时,返回 true。
Collection<E> 中的 containsBlockingQueue<E> 中的 containsAbstractCollection<E> 中的 containso - 要检查是否包含于此队列的对象
public Object[] toArray()
由于此队列并不维护对返回数组的任何引用,因而它将是“安全的”。(换句话说,此方法必须分配一个新数组)。因此,调用者可以随意修改返回的数组。
此方法充当基于数组的 API 与基于 collection 的 API 之间的桥梁。
Collection<E> 中的 toArrayAbstractCollection<E> 中的 toArraypublic String toString()
AbstractCollection 复制的描述String.valueOf(Object) 可以将元素转换成字符串。
AbstractCollection<E> 中的 toStringpublic int drainTo(Collection<? super E> c)
BlockingQueue 复制的描述
BlockingQueue<E> 中的 drainToc - 接收传输元素的 collection
UnsupportedOperationException - 如果指定 collection 不支持添加元素
ClassCastException - 如果此队列元素的类不允许将其添加到指定 collection
NullPointerException - 如果指定 collection 为 null
IllegalArgumentException - 如果指定 collection 是此队列,或者此队列元素的某些属性不允许将其添加到指定 collection
public int drainTo(Collection<? super E> c,
int maxElements)
BlockingQueue 复制的描述
BlockingQueue<E> 中的 drainToc - 接收传输元素的 collectionmaxElements - 传输元素的最大数量
UnsupportedOperationException - 如果指定 collection 不支持添加元素
ClassCastException - 如果此队列元素的类不允许将其添加到指定 collection
NullPointerException - 如果指定 collection 为 null
IllegalArgumentException - 如果指定 collection 是此队列,或者此队列元素的某些属性不允许将其添加到指定 collectionpublic void clear()
Collection<E> 中的 clearAbstractQueue<E> 中的 clearpublic <T> T[] toArray(T[] a)
如果指定的数组能容纳队列,并有剩余的空间(即数组的元素比队列多),那么会将紧接队列尾部的元素设置为 null。
像 toArray() 方法一样,此方法充当基于数组 的 API 与基于 collection 的 API 之间的桥梁。更进一步说,此方法允许对输出数组的运行时类型进行精确控制,在某些情况下,可以用来节省分配开销。
假定 x 是只包含字符串的一个已知队列。以下代码用来将该队列转储到一个新分配的 String 数组:
String[] y = x.toArray(new String[0]);
注意,toArray(new Object[0]) 和 toArray() 在功能上是相同的。
Collection<E> 中的 toArrayAbstractCollection<E> 中的 toArraya - 用来存储队列元素的数组(如果该数组足够大);否则为此分配一个具有相同运行时类型的新数组
ArrayStoreException - 如果指定数组的运行时类型不是此队列每个元素的运行时类型的超类型
NullPointerException - 如果指定数组为 nullpublic Iterator<E> iterator()
ConcurrentModificationException,并且可确保遍历迭代器构造后存在的所有元素,并且可能(但并不保证)反映构造后的所有修改。
Iterable<E> 中的 iteratorCollection<E> 中的 iteratorAbstractCollection<E> 中的 iterator
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。