Advanced   Java   Services PriorityBlockingQueue Back Next Up Home


PriorityBlockingQueue

Aus der API

An unbounded 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 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.





Beispiel

Das einfache Beispiel ist eine Übertragung des Beispiels zu PriorityQueue und zeigt das Blockieren falls die Queue leer ist.

public static void main(String[] args) throws InterruptedException
{
   PriorityBlockingQueue pq = new PriorityBlockingQueue<>();
   System.out.println("Aufnahme von Elementen" );
   pq.add(new MyString("eins"));
   pq.add(new MyString("zwei"));
   pq.add(new MyString("drei"));
   System.out.println("Enthaltene Elemente: " + pq);

   // Die toString()-Methode ist so überschrieben, daß die enthaltenen Elemente
   // in eckigen Klammern ausgegeben werden, Head ganz links.
   // Die Elemente sind nicht geordnet, aber beim Aufnehmen wird mit compareTo()
   // ermittelt, welchers Element an die Spitze muß

   System.out.println("--------------------------------");
   System.out.println("head = " + pq.peek());
   System.out.println("take = " + pq.take());  // holt das head-Element raus
   // bestimmt den neuen head, bevor der alte head geliefert wird
   System.out.println("head = " + pq.peek());
   System.out.println("Enthaltene Elemente: " + pq);

   System.out.println("--------------------------------");
   System.out.println("head = " + pq.peek());
   System.out.println("take = " + pq.take());
   System.out.println("head = " + pq.peek());
   System.out.println("Enthaltene Elemente: " + pq);

   System.out.println("--------------------------------");
   System.out.println("head = " + pq.peek());
   System.out.println("take = " + pq.take());
   System.out.println("head = " + pq.peek());
   System.out.println("Enthaltene Elemente:" + pq);

   System.out.println("--------------------------------");
   System.out.println("head = " + pq.peek()); // liefet null
   System.out.println("take = " + pq.take()); // blockiert
}

Die Klasse MyString macht die Aufrufe von compareTo() sichtbar.

class MyString implements Comparable<MyString>
{
   String string;

   public MyString(String string)
   {
      this.string = string;
   }

   @Override
   public int compareTo(MyString ob)
   {
      System.out.println("compare " + this.string + "  " + ob.string);
      return this.string.compareTo(ob.toString());
   }

   @Override
   public String toString()
   {
      return string;
   }
}

Output

Aufnahme von Elementen
compare zwei  eins
compare drei  eins
Enthaltene Elemente: [drei, zwei, eins]
--------------------------------
head = drei
compare eins  zwei
take = drei
head = eins
Enthaltene Elemente: [eins, zwei]
--------------------------------
head = eins
take = eins
head = zwei
Enthaltene Elemente: [zwei]
--------------------------------
head = zwei
take = zwei
head = null
Enthaltene Elemente:[]
--------------------------------
head = null
Valid XHTML 1.0 Strict top Back Next Up Home