Delayed
elements, in which an element generally becomes eligible for removal when its
delay has expired.
While this class implements the BlockingQueue
interface, it
intentionally violates the general contract of BlockingQueue
, in that
the following methods disregard the presence of unexpired elements and only
ever remove the expired head:
All other methods operate on both expired and unexpired elements.
For example, the size()
method returns the count of all elements.
Method peek()
may return the (non-null) head even when
take()
would block waiting for that element to expire.
This queue does not permit null elements.
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 DelayQueue in any
particular order.
This class is a member of the Java Collections Framework.
Modifier and Type | Class and Description |
---|---|
private class | DelayQueue.
Snapshot iterator that works off copy of underlying q array. |
Modifier and Type | Field and Description |
---|---|
private final Condition | available
Condition signalled when a newer element becomes available at the head of the queue or a new thread may need to become leader. |
private Thread | leader
Thread designated to wait for the element at the head of the queue. |
private final transient ReentrantLock | |
private final PriorityQueue |
Access | Constructor and Description |
---|---|
public | |
public | DelayQueue(Collection<? extends E>
the collection of elements to initially contain c)Creates a |
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
the element to add e)Overrides java. Implements java. Inserts the specified element into this delay queue. |
public void | clear()
Overrides java. Implements java. Atomically removes all of the elements from this delay queue. |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c)Implements java. Removes all available elements from this queue and adds them to the given collection. |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c, int the maximum number of elements to transfer maxElements)Implements java. Removes at most the given number of available elements from this queue and adds them to the given collection. |
public Iterator | Returns: an iterator over the elements in this queueImplements abstract java. Implements java. Returns an iterator over all the elements (both expired and unexpired) in this queue. |
public boolean | Returns: true the element to add e)Implements java. Inserts the specified element into this delay queue. |
public boolean | Returns: true the element to add e, long This parameter is ignored as the method never blocks timeout, TimeUnit This parameter is ignored as the method never blocks unit)Implements java. Inserts the specified element into this delay queue. |
public E | Returns: the head of this queue, ornull if this
queue is emptyImplements java. Retrieves, but does not remove, the head of this
queue, or returns |
public E | Returns: the expired head of this queue, ornull if this
queue has no elements with an expired delayImplements java. Retrieves and removes the expired head of
this queue, or returns |
public E | Returns: the expired head of this queue, ornull if the
specified waiting time elapses before an element with
an expired delay becomes availablehow long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterImplements java. Retrieves and removes the expired head of this queue, waiting if necessary until an expired element is available on this queue, or the specified wait time expires. |
public void | put(E
the element to add e)Implements java. Inserts the specified element into this delay queue. |
public int | Returns: Integer.MAX_VALUE Implements java. Always returns |
public E | Returns: the expired head of this queueOverrides java. Implements java. Retrieves and removes the expired head of this queue, or throws an exception if this queue has no expired elements. |
public boolean | remove(Object
element to be removed from this queue, if present o)Overrides java. Implements java. Removes a single instance of the specified element from this queue, if it is present, whether or not it has expired. |
pack-priv void | |
public int | size()
Implements abstract java. Implements java. Returns the number of elements in this collection. |
public E | Returns: the expired head of this queueImplements java. Retrieves and removes the expired head of this queue, waiting if necessary until an expired element is available on this queue. |
public Object[] | Returns: an array containing all of the elements in this queueOverrides java. Implements java. Returns an array containing all of the elements in this queue. |
public <T> T[] | Returns: an array containing all of the elements in this queuethe array into which the elements of the queue are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose a)Overrides java. Implements java. Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. |
available | back to summary |
---|---|
private final Condition available Condition signalled when a newer element becomes available at the head of the queue or a new thread may need to become leader. |
leader | back to summary |
---|---|
private Thread leader Thread designated to wait for the element at the head of the queue. This variant of the Leader-Follower pattern (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to minimize unnecessary timed waiting. When a thread becomes the leader, it waits only for the next delay to elapse, but other threads await indefinitely. The leader thread must signal some other thread before returning from take() or poll(...), unless some other thread becomes leader in the interim. Whenever the head of the queue is replaced with an element with an earlier expiration time, the leader field is invalidated by being reset to null, and some waiting thread, but not necessarily the current leader, is signalled. So waiting threads must be prepared to acquire and lose leadership while waiting. |
lock | back to summary |
---|---|
private final transient ReentrantLock lock |
q | back to summary |
---|---|
private final PriorityQueue<E> q |
DelayQueue | back to summary |
---|---|
public DelayQueue() Creates a new |
DelayQueue | back to summary |
---|---|
public DelayQueue(Collection<? extends E> c) Creates a
|
add | back to summary |
---|---|
public boolean add(E e) Overrides java. Implements java. Inserts the specified element into this delay queue.
|
clear | back to summary |
---|---|
public void clear() Overrides java. Implements java. Atomically removes all of the elements from this delay queue. The queue will be empty after this call returns. Elements with an unexpired delay are not waited for; they are simply discarded from the queue. |
drainTo | back to summary |
---|---|
public int drainTo(Collection<? super E> c) Implements java. Doc from java. Removes all available elements from this queue and adds them
to the given collection. This operation may be more
efficient than repeatedly polling this queue. A failure
encountered while attempting to add elements to
collection
|
drainTo | back to summary |
---|---|
public int drainTo(Collection<? super E> c, int maxElements) Implements java. Doc from java. Removes at most the given number of available elements from
this queue and adds them to the given collection. A failure
encountered while attempting to add elements to
collection
|
iterator | back to summary |
---|---|
public Iterator Implements abstract java. Implements java. Returns an iterator over all the elements (both expired and unexpired) in this queue. The iterator does not return the elements in any particular order. The returned iterator is weakly consistent.
|
offer | back to summary |
---|---|
public boolean offer(E e) Implements java. Inserts the specified element into this delay queue.
|
offer | back to summary |
---|---|
public boolean offer(E e, long timeout, TimeUnit unit) Implements java. Inserts the specified element into this delay queue. As the queue is unbounded this method will never block.
|
peek | back to summary |
---|---|
public E peek() Implements java. Retrieves, but does not remove, the head of this
queue, or returns
|
poll | back to summary |
---|---|
public E poll() Implements java. Retrieves and removes the expired head of
this queue, or returns
|
poll | back to summary |
---|---|
public E poll(long timeout, TimeUnit unit) throws InterruptedException Implements java. Retrieves and removes the expired head of this queue, waiting if necessary until an expired element is available on this queue, or the specified wait time expires.
|
put | back to summary |
---|---|
public void put(E e) Implements java. Inserts the specified element into this delay queue. As the queue is unbounded this method will never block.
|
remainingCapacity | back to summary |
---|---|
public int remainingCapacity() Implements java. Always returns
|
remove | back to summary |
---|---|
public E remove() Overrides java. Implements java. Retrieves and removes the expired head of this queue, or throws an exception if this queue has no expired elements.
|
remove | back to summary |
---|---|
public boolean remove(Object o) Overrides java. Implements java. Removes a single instance of the specified element from this queue, if it is present, whether or not it has expired.
|
removeEQ | back to summary |
---|---|
pack-priv void removeEQ(Object o) Identity-based version for use in Itr.remove. |
size | back to summary |
---|---|
public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this collection. If this collection
contains more than
|
take | back to summary |
---|---|
public E take() throws InterruptedException Implements java. Retrieves and removes the expired head of this queue, waiting if necessary until an expired element is available on this queue.
|
toArray | back to summary |
---|---|
public Object[] toArray() Overrides java. Implements java. Returns an array containing all of the elements in this queue. 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 queue. (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.
|
toArray | back to summary |
---|---|
public <T> T[] toArray(T[] a) Overrides java. Implements java. Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. The returned array elements are in no particular order. If the queue 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 queue. If this queue fits in the specified array with room to spare
(i.e., the array has more elements than this queue), the element in
the array immediately following the end of the queue is set to
Like the The following code can be used to dump a delay queue into a newly
allocated array of
Note that toArray(new Object[0]) is identical in function to
toArray() .
|
Modifier and Type | Field and Description |
---|---|
pack-priv final Object[] | |
pack-priv int | |
pack-priv int |
Modifier and Type | Method and Description |
---|---|
public boolean | |
public E | |
public void | remove()
Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
array | back to summary |
---|---|
pack-priv final Object[] array |
cursor | back to summary |
---|---|
pack-priv int cursor |
lastRet | back to summary |
---|---|
pack-priv int lastRet |
Itr | back to summary |
---|---|
pack-priv Itr(Object[] array) |
hasNext | back to summary |
---|---|
public boolean hasNext() Implements java. Doc from java. Returns
|
next | back to summary |
---|---|
public E next() Implements java. Doc from java. Returns the next element in the iteration.
|
remove | back to summary |
---|---|
public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the |