PriorityBlockingQueue

整体描述

An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations.

注意

  1. While this queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing OutOfMemoryError).
  2. This class does not permit null elements.
  3. 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() 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 ? 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;
   }
 }}

你可能感兴趣的:(PriorityBlockingQueue)