PriorityQueue
and supplies
blocking retrieval operations. While this queue is logically
unbounded, attempted additions may fail due to resource exhaustion
(causing OutOfMemoryError
). This class does not permit
null
elements. A priority queue relying on natural ordering also does not permit insertion of
non-comparable objects (doing so results in
ClassCastException
).
This class and its iterator implement all of the optional
methods of the Collection
and Iterator
interfaces.
The Iterator provided in method iterator()
and the
Spliterator provided in method spliterator()
are not
guaranteed to traverse the elements of the PriorityBlockingQueue in
any particular order. If you need ordered traversal, consider using
Arrays.sort(pq.toArray())
. Also, method drainTo
can
be used to remove some or all elements in priority order and
place them in another collection.
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 a
new FIFOEntry(anEntry)
instead of a plain entry object.
class FIFOEntry<E extends Comparable<? super E>>
implements Comparable<FIFOEntry<E>> {
static final 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;
}
}
This class is a member of the Java Collections Framework.
Modifier and Type | Class and Description |
---|---|
pack-priv class | PriorityBlockingQueue.
Snapshot iterator that works off copy of underlying q array. |
pack-priv class | PriorityBlockingQueue.
Immutable snapshot spliterator that binds to elements "late". |
Modifier and Type | Field and Description |
---|---|
private transient volatile int | allocationSpinLock
Spinlock for allocation, acquired via CAS. |
private static final VarHandle | |
private transient Comparator | comparator
The comparator, or null if priority queue uses elements' natural ordering. |
private static final int | DEFAULT_INITIAL_CAPACITY
Default array capacity. |
private final ReentrantLock | lock
Lock used for all public operations. |
private final Condition | notEmpty
Condition for blocking when empty. |
private PriorityQueue | q
A plain PriorityQueue used only for serialization, to maintain compatibility with previous versions of this class. |
private transient Object[] | queue
Priority queue represented as a balanced binary heap: the two children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. |
private static final long | |
private transient int | size
The number of elements in the priority queue. |
Access | Constructor and Description |
---|---|
public | PriorityBlockingQueue()
Creates a |
public | PriorityBlockingQueue(int
the initial capacity for this priority queue initialCapacity)Creates a |
public | PriorityBlockingQueue(int
the initial capacity for this priority queue initialCapacity, Comparator<? super E> the comparator that will be used to order this
priority queue. If comparator)null , the natural ordering of the elements will be used.Creates a |
public | PriorityBlockingQueue(Collection<? extends E>
the collection whose elements are to be placed
into this priority queue c)Creates a |
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
the element to add e)Overrides java. Implements java. Inserts the specified element into this priority queue. |
private boolean | |
public void | clear()
Overrides java. Implements java. Atomically removes all of the elements from this queue. |
public Comparator | Returns: the comparator used to order the elements in this queue, ornull if this queue uses the natural
ordering of its elementsReturns the comparator used to order the elements in this queue,
or |
public boolean | Returns: true if this queue contains the specified elementobject to be checked for containment in this queue o)Overrides java. Implements java. Returns |
private E | |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c)Implements java. Removes all available elements from this queue and adds them to the given collection. |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c, int the maximum number of elements to transfer maxElements)Implements java. Removes at most the given number of available elements from this queue and adds them to the given collection. |
private static Object[] | |
public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Performs the given action for each element of the |
private void | heapify()
Establishes the heap invariant (described above) in the entire tree, assuming nothing about the order of the elements prior to the call. |
private int | |
private static boolean | |
public Iterator | Returns: an iterator over the elements in this queueImplements abstract java. Implements java. Returns an iterator over the elements in this queue. |
private static long[] | |
public boolean | offer(E
the element to add e)Implements java. Inserts the specified element into this priority queue. |
public boolean | offer(E
the element to add e, long This parameter is ignored as the method never blocks timeout, TimeUnit This parameter is ignored as the method never blocks unit)Implements java. Inserts the specified element into this priority queue. |
public E | peek()
Implements java. Retrieves, but does not remove, the head of this queue,
or returns |
public E | poll()
Implements java. Retrieves and removes the head of this queue,
or returns |
public E | poll(long
how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterImplements java. Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available. |
public void | put(E
the element to add e)Implements java. Inserts the specified element into this priority queue. |
private void | readObject(ObjectInputStream
the stream s)Reconstitutes this queue from a stream (that is, deserializes it). |
public int | Returns: Integer.MAX_VALUE alwaysImplements java. Always returns |
public boolean | Returns: true if this queue changed as a result of the callelement to be removed from this queue, if present o)Overrides java. Implements java. Removes a single instance of the specified element from this queue, if it is present. |
public boolean | removeAll(Collection<?>
collection containing elements to be removed from this collection c)Overrides java. Implements java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
private void | |
pack-priv void | |
public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
public boolean | retainAll(Collection<?>
collection containing elements to be retained in this collection c)Overrides java. Implements java. Retains only the elements in this collection that are contained in the specified collection (optional operation). |
private static void | |
private static <T> void | siftDownComparable(int
the position to fill k, T the item to insert x, Object[] the heap array es, int heap size n)Inserts item x at position k, maintaining heap invariant by demoting x down the tree repeatedly until it is less than or equal to its children or is a leaf. |
private static <T> void | |
private static <T> void | siftUpComparable(int
the position to fill k, T the item to insert x, Object[] the heap array es)Inserts item x at position k, maintaining heap invariant by promoting x up the tree until it is greater than or equal to its parent, or is the root. |
private static <T> void | |
public int | size()
Implements abstract java. Implements java. Returns the number of elements in this collection. |
public Spliterator | Returns: aSpliterator over the elements in this queueOverrides default java. Returns a |
public E | take()
Implements java. Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
public Object[] | Returns: an array containing all of the elements in this queueOverrides java. Implements java. Returns an array containing all of the elements in this queue. |
public <T> T[] | Returns: an array containing all of the elements in this queuethe array into which the elements of the queue are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose a)Overrides java. Implements java. Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. |
public String | toString()
Overrides java. Returns a string representation of this collection. |
private void | |
private void |