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
Modifier and TypeFieldDescriptionprotected final ReentrantLock
protected final Condition
protected final PriorityQueue<E>
-
Constructor Summary
ConstructorDescriptionCreates aPriorityBlockingQueue
with the default initial capacity (11) that orders its elements according to their natural ordering.JDKPriorityBlockingQueue
(int initialCapacity) Creates aPriorityBlockingQueue
with the specified initial capacity that orders its elements according to their natural ordering.JDKPriorityBlockingQueue
(int initialCapacity, Comparator<? super E> comparator) Creates aPriorityBlockingQueue
with the specified initial capacity that orders its elements according to the specified comparator.JDKPriorityBlockingQueue
(Collection<? extends E> c) Creates aPriorityBlockingQueue
containing the elements in the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Inserts the specified element into this priority queues.void
clear()
Atomically removes all of the elements from this queues.Comparator<? super E>
Returns the comparator used to order the elements in this queues, ornull
if this queues uses the natural ordering of its elements.boolean
Returnstrue
if this queues contains the specified element.int
drainTo
(Collection<? super E> c) int
drainTo
(Collection<? super E> c, int maxElements) iterator()
Returns an iterator over the elements in this queues.boolean
Inserts the specified element into this priority queues.boolean
Inserts the specified element into this priority queues.peek()
poll()
void
Inserts the specified element into this priority queues.int
Always returnsInteger.MAX_VALUE
because aPriorityBlockingQueue
is not capacity constrained.boolean
Removes a single instance of the specified element from this queues, if it is present.int
size()
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, remove
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods 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 aPriorityBlockingQueue
with the default initial capacity (11) that orders its elements according to their natural ordering. -
JDKPriorityBlockingQueue
public JDKPriorityBlockingQueue(int initialCapacity) Creates aPriorityBlockingQueue
with the specified initial capacity that orders its elements according to their natural ordering.- Parameters:
initialCapacity
- the initial capacity for this priority queues- Throws:
IllegalArgumentException
- ifinitialCapacity
is less than 1
-
JDKPriorityBlockingQueue
Creates aPriorityBlockingQueue
with 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
- ifinitialCapacity
is less than 1
-
JDKPriorityBlockingQueue
Creates aPriorityBlockingQueue
containing the elements in the specified collection. If the specified collection is aSortedSet
or 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:
add
in interfaceBlockingQueue<E>
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceQueue<E>
- Overrides:
add
in 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:
offer
in interfaceBlockingQueue<E>
- Specified by:
offer
in 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:
put
in 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:
offer
in 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:
take
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
poll
- Specified by:
poll
in interfaceBlockingQueue<E>
- Throws:
InterruptedException
-
peek
-
comparator
Returns the comparator used to order the elements in this queues, ornull
if this queues uses the natural ordering of its elements.- Returns:
- the comparator used to order the elements in this queues,
or
null
if this queues uses the natural ordering of its elements
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in classAbstractCollection<E>
-
remainingCapacity
public int remainingCapacity()Always returnsInteger.MAX_VALUE
because aPriorityBlockingQueue
is not capacity constrained.- Specified by:
remainingCapacity
in 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 elemente
such thato.equals(e)
, if this queues contains one or more such elements. Returnstrue
if and only if this queues contained the specified element (or equivalently, if this queues changed as a result of the call).- Specified by:
remove
in interfaceBlockingQueue<E>
- Specified by:
remove
in interfaceCollection<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
o
- element to be removed from this queues, if present- Returns:
true
if this queues changed as a result of the call
-
contains
Returnstrue
if this queues contains the specified element. More formally, returnstrue
if and only if this queues contains at least one elemente
such thato.equals(e)
.- Specified by:
contains
in interfaceBlockingQueue<E>
- Specified by:
contains
in interfaceCollection<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
o
- object to be checked for containment in this queues- Returns:
true
if 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:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Returns:
- an array containing all of the elements in this queues
-
toString
- Overrides:
toString
in classAbstractCollection<E>
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<E>
- Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
-
drainTo
- Specified by:
drainTo
in interfaceBlockingQueue<E>
- Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
-
clear
public void clear()Atomically removes all of the elements from this queues. The queues will be empty after this call returns.- Specified by:
clear
in interfaceCollection<E>
- Overrides:
clear
in 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
x
is 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:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in 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 returnedIterator
is 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:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in classAbstractCollection<E>
- Returns:
- an iterator over the elements in this queues
-