PriorityBlockingQueue

public class PriorityBlockingQueue extends AbstractQueue<E>
implements BlockingQueue<E> Serializable

An unbounded {@linkplain BlockingQueue blocking queue} that uses the same ordering rules as class 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 {@linkplain Comparable 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() is 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(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<E> other) {
     int res = entry.compareTo(other.entry);
     if (res == 0 && other.entry != this.entry)
       res = (seqNum < other.seqNum ? -1 : 1);
     return res;
   }
 }

Public Constructor Summary

PriorityBlockingQueue()
Creates a PriorityBlockingQueue with the default initial capacity (11) that orders its elements according to their {@linkplain Comparable natural ordering}.
PriorityBlockingQueue(int initialCapacity)
Creates a PriorityBlockingQueue with the specified initial capacity that orders its elements according to their {@linkplain Comparable natural ordering}.
PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityBlockingQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityBlockingQueue(Collection<? extends E> c)
Creates a PriorityBlockingQueue containing the elements in the specified collection.

Public Method Summary

boolean
add(E e)
Inserts the specified element into this priority queue.
void
clear()
Atomically removes all of the elements from this queue.
Comparator<? super E>
comparator()
Returns the comparator used to order the elements in this queue, or null if this queue uses the {@linkplain Comparable natural ordering} of its elements.
boolean
contains(Object o)
Returns true if this queue contains the specified element.
int
drainTo(Collection<? super E> c, int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given collection.
int
drainTo(Collection<? super E> c)
Removes all available elements from this queue and adds them to the given collection.
Iterator<E>
iterator()
Returns an iterator over the elements in this queue.
boolean
offer(E e)
Inserts the specified element into this priority queue.
boolean
offer(E e, long timeout, TimeUnit unit)
Inserts the specified element into this priority queue.
E
peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E
poll(long timeout, TimeUnit unit)
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
E
poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
void
put(E e)
Inserts the specified element into this priority queue.
int
remainingCapacity()
Always returns Integer.MAX_VALUE because a PriorityBlockingQueue is not capacity constrained.
boolean
remove(Object o)
Removes a single instance of the specified element from this queue, if it is present.
int
size()
Returns the number of elements in this collection.
Spliterator<E>
spliterator()
Returns a Spliterator over the elements in this queue.
E
take()
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
<T> T[]
toArray(T[] a)
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.
Object[]
toArray()
Returns an array containing all of the elements in this queue.
String
toString()
Returns a string representation of this collection.

Inherited Method Summary