1 *java.util.concurrent.PriorityBlockingQueue* *PriorityBlockingQueue* An unbounde
3 public class PriorityBlockingQueue<E>
4 extends |java.util.AbstractQueue|
5 implements |java.util.concurrent.BlockingQueue|
8 |java.util.concurrent.PriorityBlockingQueue_Description|
9 |java.util.concurrent.PriorityBlockingQueue_Fields|
10 |java.util.concurrent.PriorityBlockingQueue_Constructors|
11 |java.util.concurrent.PriorityBlockingQueue_Methods|
13 ================================================================================
15 *java.util.concurrent.PriorityBlockingQueue_Constructors*
16 |java.util.concurrent.PriorityBlockingQueue()|Creates a PriorityBlockingQueue w
17 |java.util.concurrent.PriorityBlockingQueue(Collection<?extendsE>)|Creates a Pr
18 |java.util.concurrent.PriorityBlockingQueue(int)|Creates a PriorityBlockingQueu
19 |java.util.concurrent.PriorityBlockingQueue(int,Comparator<?superE>)|Creates a
21 *java.util.concurrent.PriorityBlockingQueue_Methods*
22 |java.util.concurrent.PriorityBlockingQueue.add(E)|Inserts the specified elemen
23 |java.util.concurrent.PriorityBlockingQueue.clear()|Atomically removes all of t
24 |java.util.concurrent.PriorityBlockingQueue.comparator()|Returns the comparator
25 |java.util.concurrent.PriorityBlockingQueue.contains(Object)|Returnstrueif this
26 |java.util.concurrent.PriorityBlockingQueue.drainTo(Collection<?superE>)|
27 |java.util.concurrent.PriorityBlockingQueue.drainTo(Collection<?superE>,int)|
28 |java.util.concurrent.PriorityBlockingQueue.iterator()|Returns an iterator over
29 |java.util.concurrent.PriorityBlockingQueue.offer(E)|Inserts the specified elem
30 |java.util.concurrent.PriorityBlockingQueue.offer(E,long,TimeUnit)|Inserts the
31 |java.util.concurrent.PriorityBlockingQueue.peek()|
32 |java.util.concurrent.PriorityBlockingQueue.poll()|
33 |java.util.concurrent.PriorityBlockingQueue.poll(long,TimeUnit)|
34 |java.util.concurrent.PriorityBlockingQueue.put(E)|Inserts the specified elemen
35 |java.util.concurrent.PriorityBlockingQueue.remainingCapacity()|Always returns
36 |java.util.concurrent.PriorityBlockingQueue.remove(Object)|Removes a single ins
37 |java.util.concurrent.PriorityBlockingQueue.size()|
38 |java.util.concurrent.PriorityBlockingQueue.take()|
39 |java.util.concurrent.PriorityBlockingQueue.toArray()|Returns an array containi
40 |java.util.concurrent.PriorityBlockingQueue.toArray(T[])|Returns an array conta
41 |java.util.concurrent.PriorityBlockingQueue.toString()|
43 *java.util.concurrent.PriorityBlockingQueue_Description*
45 An unbounded blocking queue(|java.util.concurrent.BlockingQueue|) that uses the
46 same ordering rules as class (|java.util.PriorityQueue|) and supplies blocking
47 retrieval operations. While this queue is logically unbounded, attempted
48 additions may fail due to resource exhaustion (causing OutOfMemoryError). This
49 class does not permit null elements. A priority queue relying on natural
50 ordering(|java.lang.Comparable|) also does not permit insertion of
51 non-comparable objects (doing so results in ClassCastException).
53 This class and its iterator implement all of the optional methods of the
54 (|java.util.Collection|) and (|java.util.Iterator|) interfaces. The Iterator
55 provided in method (|java.util.concurrent.PriorityBlockingQueue|) is not
56 guaranteed to traverse the elements of the PriorityBlockingQueue in any
57 particular order. If you need ordered traversal, consider using
58 Arrays.sort(pq.toArray()). Also, method drainTo can be used to remove some or
59 all elements in priority order and place them in another collection.
61 Operations on this class make no guarantees about the ordering of elements with
62 equal priority. If you need to enforce an ordering, you can define custom
63 classes or comparators that use a secondary key to break ties in primary
64 priority values. For example, here is a class that applies first-in-first-out
65 tie-breaking to comparable elements. To use it, you would insert a new
66 FIFOEntry(anEntry) instead of a plain entry object.
70 class FIFOEntry<E extends Comparable<? super E>> implements
71 Comparable<FIFOEntry<E>> { final static AtomicLong seq = new AtomicLong();
72 final long seqNum; final E entry; public FIFOEntry(E entry) { seqNum =
73 seq.getAndIncrement(); this.entry = entry; } public E getEntry() { return
74 entry; } public int compareTo(FIFOEntry<E> other) { int res =
75 entry.compareTo(other.entry); if (res == 0 and and other.entry != this.entry)
76 res = (seqNum < other.seqNum ? -1 : 1); return res; } }
78 This class is a member of the <a
79 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
83 *java.util.concurrent.PriorityBlockingQueue()*
85 public PriorityBlockingQueue()
87 Creates a PriorityBlockingQueue with the default initial capacity (11) that
88 orders its elements according to their natural ordering(|java.lang.Comparable|)
92 *java.util.concurrent.PriorityBlockingQueue(Collection<?extendsE>)*
94 public PriorityBlockingQueue(java.util.Collection<? extends E> c)
96 Creates a PriorityBlockingQueue containing the elements in the specified
97 collection. If the specified collection is a (|java.util.SortedSet|) or a
98 (|java.util.PriorityQueue|) , this priority queue will be ordered according to
99 the same ordering. Otherwise, this priority queue will be ordered according to
100 the natural ordering(|java.lang.Comparable|) of its elements.
102 c - the collection whose elements are to be placed into this priority queue
104 *java.util.concurrent.PriorityBlockingQueue(int)*
106 public PriorityBlockingQueue(int initialCapacity)
108 Creates a PriorityBlockingQueue with the specified initial capacity that orders
109 its elements according to their natural ordering(|java.lang.Comparable|) .
111 initialCapacity - the initial capacity for this priority queue
113 *java.util.concurrent.PriorityBlockingQueue(int,Comparator<?superE>)*
115 public PriorityBlockingQueue(
117 java.util.Comparator<? super E> comparator)
119 Creates a PriorityBlockingQueue with the specified initial capacity that orders
120 its elements according to the specified comparator.
122 initialCapacity - the initial capacity for this priority queue
123 comparator - the comparator that will be used to order this priority queue. If {@code null},
124 the {@linkplain Comparable natural ordering} of the elements will be
127 *java.util.concurrent.PriorityBlockingQueue.add(E)*
129 public boolean add(E e)
131 Inserts the specified element into this priority queue.
134 e - the element to add
136 Returns: true (as specified by {@link Collection#add})
138 *java.util.concurrent.PriorityBlockingQueue.clear()*
142 Atomically removes all of the elements from this queue. The queue will be empty
143 after this call returns.
147 *java.util.concurrent.PriorityBlockingQueue.comparator()*
149 public |java.util.Comparator|<? super E> comparator()
151 Returns the comparator used to order the elements in this queue, or null if
152 this queue uses the natural ordering(|java.lang.Comparable|) of its elements.
156 Returns: the comparator used to order the elements in this queue, or null if this queue
157 uses the natural ordering of its elements
159 *java.util.concurrent.PriorityBlockingQueue.contains(Object)*
161 public boolean contains(java.lang.Object o)
163 Returnstrueif this queue contains the specified element. More formally,
164 returnstrueif and only if this queue contains at least one elementesuch
168 o - object to be checked for containment in this queue
170 Returns: true if this queue contains the specified element
172 *java.util.concurrent.PriorityBlockingQueue.drainTo(Collection<?superE>)*
174 public int drainTo(java.util.Collection<? super E> c)
180 *java.util.concurrent.PriorityBlockingQueue.drainTo(Collection<?superE>,int)*
183 java.util.Collection<? super E> c,
190 *java.util.concurrent.PriorityBlockingQueue.iterator()*
192 public |java.util.Iterator|<E> iterator()
194 Returns an iterator over the elements in this queue. The iterator does not
195 return the elements in any particular order. The returned Iterator is a "weakly
196 consistent" iterator that will never throw
197 (|java.util.ConcurrentModificationException|) , and guarantees to traverse
198 elements as they existed upon construction of the iterator, and may (but is not
199 guaranteed to) reflect any modifications subsequent to construction.
203 Returns: an iterator over the elements in this queue
205 *java.util.concurrent.PriorityBlockingQueue.offer(E)*
207 public boolean offer(E e)
209 Inserts the specified element into this priority queue.
212 e - the element to add
214 Returns: true (as specified by {@link Queue#offer})
216 *java.util.concurrent.PriorityBlockingQueue.offer(E,long,TimeUnit)*
218 public boolean offer(
221 java.util.concurrent.TimeUnit unit)
223 Inserts the specified element into this priority queue. As the queue is
224 unbounded this method will never block.
227 e - the element to add
228 timeout - This parameter is ignored as the method never blocks
229 unit - This parameter is ignored as the method never blocks
233 *java.util.concurrent.PriorityBlockingQueue.peek()*
241 *java.util.concurrent.PriorityBlockingQueue.poll()*
249 *java.util.concurrent.PriorityBlockingQueue.poll(long,TimeUnit)*
253 java.util.concurrent.TimeUnit unit)
254 throws |java.lang.InterruptedException|
260 *java.util.concurrent.PriorityBlockingQueue.put(E)*
264 Inserts the specified element into this priority queue. As the queue is
265 unbounded this method will never block.
268 e - the element to add
270 *java.util.concurrent.PriorityBlockingQueue.remainingCapacity()*
272 public int remainingCapacity()
274 Always returns Integer.MAX_VALUE because a PriorityBlockingQueue is not
275 capacity constrained.
279 Returns: Integer.MAX_VALUE
281 *java.util.concurrent.PriorityBlockingQueue.remove(Object)*
283 public boolean remove(java.lang.Object o)
285 Removes a single instance of the specified element from this queue, if it is
286 present. More formally, removes an elementesuch thato.equals(e), if this queue
287 contains one or more such elements. Returnstrueif and only if this queue
288 contained the specified element (or equivalently, if this queue changed as a
292 o - element to be removed from this queue, if present
294 Returns: true if this queue changed as a result of the call
296 *java.util.concurrent.PriorityBlockingQueue.size()*
304 *java.util.concurrent.PriorityBlockingQueue.take()*
307 throws |java.lang.InterruptedException|
313 *java.util.concurrent.PriorityBlockingQueue.toArray()*
315 public |java.lang.Object|[] toArray()
317 Returns an array containing all of the elements in this queue. The returned
318 array elements are in no particular order.
320 The returned array will be "safe" in that no references to it are maintained by
321 this queue. (In other words, this method must allocate a new array). The caller
322 is thus free to modify the returned array.
324 This method acts as bridge between array-based and collection-based APIs.
328 Returns: an array containing all of the elements in this queue
330 *java.util.concurrent.PriorityBlockingQueue.toArray(T[])*
332 public |T|[] toArray(T[] a)
334 Returns an array containing all of the elements in this queue; the runtime type
335 of the returned array is that of the specified array. The returned array
336 elements are in no particular order. If the queue fits in the specified array,
337 it is returned therein. Otherwise, a new array is allocated with the runtime
338 type of the specified array and the size of this queue.
340 If this queue fits in the specified array with room to spare (i.e., the array
341 has more elements than this queue), the element in the array immediately
342 following the end of the queue is set to null.
344 Like the (|java.util.concurrent.PriorityBlockingQueue|) method, this method
345 acts as bridge between array-based and collection-based APIs. Further, this
346 method allows precise control over the runtime type of the output array, and
347 may, under certain circumstances, be used to save allocation costs.
349 Suppose x is a queue known to contain only strings. The following code can be
350 used to dump the queue into a newly allocated array of String:
354 String[] y = x.toArray(new String[0]);
356 Note that toArray(new Object[0]) is identical in function to toArray().
359 a - the array into which the elements of the queue are to be stored, if it is big
360 enough; otherwise, a new array of the same runtime type is allocated for
363 Returns: an array containing all of the elements in this queue
365 *java.util.concurrent.PriorityBlockingQueue.toString()*
367 public |java.lang.String| toString()