Class JDKPriorityBlockingQueue<E>
- Type Parameters:
E- element in queue
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,BlockingQueue<E>,Queue<E>
- Direct Known Subclasses:
SimulationQueue
- Author:
- poussin
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ReentrantLockprotected final Conditionprotected final PriorityQueue<E> -
Constructor Summary
ConstructorsConstructorDescriptionCreates aPriorityBlockingQueuewith the default initial capacity (11) that orders its elements according to their natural ordering.JDKPriorityBlockingQueue(int initialCapacity) Creates aPriorityBlockingQueuewith the specified initial capacity that orders its elements according to their natural ordering.JDKPriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator) Creates aPriorityBlockingQueuewith the specified initial capacity that orders its elements according to the specified comparator.JDKPriorityBlockingQueue(Collection<? extends E> c) Creates aPriorityBlockingQueuecontaining the elements in the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanInserts the specified element into this priority queues.voidclear()Atomically removes all of the elements from this queues.Comparator<? super E>Returns the comparator used to order the elements in this queues, ornullif this queues uses the natural ordering of its elements.booleanReturnstrueif this queues contains the specified element.intdrainTo(Collection<? super E> c) intdrainTo(Collection<? super E> c, int maxElements) iterator()Returns an iterator over the elements in this queues.booleanInserts the specified element into this priority queues.booleanInserts the specified element into this priority queues.peek()poll()voidInserts the specified element into this priority queues.intAlways returnsInteger.MAX_VALUEbecause aPriorityBlockingQueueis not capacity constrained.booleanRemoves a single instance of the specified element from this queues, if it is present.intsize()take()Object[]toArray()Returns an array containing all of the elements in this queues.<T> T[]toArray(T[] a) Returns an array containing all of the elements in this queues; the runtime type of the returned array is that of the specified array.toString()Methods inherited from class java.util.AbstractQueue
addAll, element, removeMethods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAllMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray
-
Field Details
-
q
-
lock
-
notEmpty
-
-
Constructor Details
-
JDKPriorityBlockingQueue
public JDKPriorityBlockingQueue()Creates aPriorityBlockingQueuewith the default initial capacity (11) that orders its elements according to their natural ordering. -
JDKPriorityBlockingQueue
public JDKPriorityBlockingQueue(int initialCapacity) Creates aPriorityBlockingQueuewith the specified initial capacity that orders its elements according to their natural ordering.- Parameters:
initialCapacity- the initial capacity for this priority queues- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1
-
JDKPriorityBlockingQueue
Creates aPriorityBlockingQueuewith the specified initial capacity that orders its elements according to the specified comparator.- Parameters:
initialCapacity- the initial capacity for this priority queuescomparator- the comparator that will be used to order this priority queues. Ifnull, the natural ordering of the elements will be used.- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1
-
JDKPriorityBlockingQueue
Creates aPriorityBlockingQueuecontaining the elements in the specified collection. If the specified collection is aSortedSetor aPriorityQueue, this priority queues will be ordered according to the same ordering. Otherwise, this priority queues will be ordered according to the natural ordering of its elements.- Parameters:
c- the collection whose elements are to be placed into this priority queues- Throws:
ClassCastException- if elements of the specified collection cannot be compared to one another according to the priority queues's orderingNullPointerException- if the specified collection or any of its elements are null
-
-
Method Details
-
add
Inserts the specified element into this priority queues.- Specified by:
addin interfaceBlockingQueue<E>- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractQueue<E>- Parameters:
e- the element to add- Returns:
true(as specified byCollection.add(E))- Throws:
ClassCastException- if the specified element cannot be compared with elements currently in the priority queues according to the priority queues's orderingNullPointerException- if the specified element is null
-
offer
Inserts the specified element into this priority queues.- Specified by:
offerin interfaceBlockingQueue<E>- Specified by:
offerin interfaceQueue<E>- Parameters:
e- the element to add- Returns:
true(as specified byQueue.offer(E))- Throws:
ClassCastException- if the specified element cannot be compared with elements currently in the priority queues according to the priority queues's orderingNullPointerException- if the specified element is null
-
put
Inserts the specified element into this priority queues. As the queues is unbounded this method will never block.- Specified by:
putin interfaceBlockingQueue<E>- Parameters:
e- the element to add- Throws:
ClassCastException- if the specified element cannot be compared with elements currently in the priority queues according to the priority queues's orderingNullPointerException- if the specified element is null
-
offer
Inserts the specified element into this priority queues. As the queues is unbounded this method will never block.- Specified by:
offerin interfaceBlockingQueue<E>- Parameters:
e- the element to addtimeout- This parameter is ignored as the method never blocksunit- This parameter is ignored as the method never blocks- Returns:
true- Throws:
ClassCastException- if the specified element cannot be compared with elements currently in the priority queues according to the priority queues's orderingNullPointerException- if the specified element is null
-
poll
-
take
- Specified by:
takein interfaceBlockingQueue<E>- Throws:
InterruptedException
-
poll
- Specified by:
pollin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
peek
-
comparator
Returns the comparator used to order the elements in this queues, ornullif this queues uses the natural ordering of its elements.- Returns:
- the comparator used to order the elements in this queues,
or
nullif this queues uses the natural ordering of its elements
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
remainingCapacity
public int remainingCapacity()Always returnsInteger.MAX_VALUEbecause aPriorityBlockingQueueis not capacity constrained.- Specified by:
remainingCapacityin interfaceBlockingQueue<E>- Returns:
Integer.MAX_VALUE
-
remove
Removes a single instance of the specified element from this queues, if it is present. More formally, removes an elementesuch thato.equals(e), if this queues contains one or more such elements. Returnstrueif and only if this queues contained the specified element (or equivalently, if this queues changed as a result of the call).- Specified by:
removein interfaceBlockingQueue<E>- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- element to be removed from this queues, if present- Returns:
trueif this queues changed as a result of the call
-
contains
Returnstrueif this queues contains the specified element. More formally, returnstrueif and only if this queues contains at least one elementesuch thato.equals(e).- Specified by:
containsin interfaceBlockingQueue<E>- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- object to be checked for containment in this queues- Returns:
trueif this queues contains the specified element
-
toArray
Returns an array containing all of the elements in this queues. The returned array elements are in no particular order.The returned array will be "safe" in that no references to it are maintained by this queues. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array containing all of the elements in this queues
-
toString
- Overrides:
toStringin classAbstractCollection<E>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>- Throws:
UnsupportedOperationExceptionClassCastExceptionNullPointerExceptionIllegalArgumentException
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>- Throws:
UnsupportedOperationExceptionClassCastExceptionNullPointerExceptionIllegalArgumentException
-
clear
public void clear()Atomically removes all of the elements from this queues. The queues will be empty after this call returns.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>
-
toArray
public <T> T[] toArray(T[] a) Returns an array containing all of the elements in this queues; the runtime type of the returned array is that of the specified array. The returned array elements are in no particular order. If the queues fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queues.If this queues fits in the specified array with room to spare (i.e., the array has more elements than this queues), the element in the array immediately following the end of the queues is set to
null.Like the
toArray()method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.Suppose
xis a queues known to contain only strings. The following code can be used to dump the queues into a newly allocated array ofString:String[] y = x.toArray(new String[0]);Note thattoArray(new Object[0])is identical in function totoArray().- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Parameters:
a- the array into which the elements of the queues are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose- Returns:
- an array containing all of the elements in this queues
- Throws:
ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of every element in this queuesNullPointerException- if the specified array is null
-
iterator
Returns an iterator over the elements in this queues. The iterator does not return the elements in any particular order. The returnedIteratoris a "weakly consistent" iterator that will never throwConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- an iterator over the elements in this queues
-