Package io.pravega.common.concurrent
Class ScheduledQueue<E extends io.pravega.common.concurrent.Scheduled>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- io.pravega.common.concurrent.ScheduledQueue<E>
-
- All Implemented Interfaces:
java.lang.Iterable<E>,java.util.Collection<E>,java.util.concurrent.BlockingQueue<E>,java.util.Queue<E>
public class ScheduledQueue<E extends io.pravega.common.concurrent.Scheduled> extends java.util.AbstractQueue<E> implements java.util.concurrent.BlockingQueue<E>Provides a unbounded blocking queue whichScheduleditems can be added to. Items which are scheduled will not be returned frompoll()ortake()until their scheduled time. This class is similar to DelayQueue but it allows a delay to be optional. This allows adding and polling non-delayed tasks in O(1). It also it lock-free, and may offer higher throughput under contention.
-
-
Constructor Summary
Constructors Constructor Description ScheduledQueue()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E e)Inserts the specified element into this delay queue.java.util.List<E>drainDelayed()Clears all delayed tasks from the queue but leaves those which can be polled immediately.intdrainTo(java.util.Collection<? super E> c)intdrainTo(java.util.Collection<? super E> c, int maxElements)java.util.Iterator<E>iterator()Returns an iterator over all the items in the queue.booleanoffer(E e)Inserts the specified element into this delay queue.booleanoffer(E e, long timeout, java.util.concurrent.TimeUnit unit)Inserts the specified element into this delay queue.Epeek()Retrieves, but does not remove, the head of this queue, or returnsnullif this queue is empty.Epoll()Epoll(long timeout, java.util.concurrent.TimeUnit unit)voidput(E e)Inserts the specified element into this delay queue.intremainingCapacity()Always returnsInteger.MAX_VALUEbecause aScheduledQueueis not capacity constrained.booleanremove(java.lang.Object o)Removes a single instance of the specified element from this queue, if it is present, whether or not it has expired.intsize()Returns the size of this collection.Etake()Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.<T> T[]toArray(T[] a)Returns an array containing all of the elements in this queue.-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, removeAll, retainAll, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Method Detail
-
take
public E take() throws java.lang.InterruptedException
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.- Specified by:
takein interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Returns:
- the head of this queue
- Throws:
java.lang.InterruptedException- if interrupted while waiting
-
poll
public E poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Specified by:
pollin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Throws:
java.lang.InterruptedException
-
poll
public E poll()
- Specified by:
pollin interfacejava.util.Queue<E extends io.pravega.common.concurrent.Scheduled>
-
add
public boolean add(E e)
Inserts the specified element into this delay queue.- Specified by:
addin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Specified by:
addin interfacejava.util.Collection<E extends io.pravega.common.concurrent.Scheduled>- Specified by:
addin interfacejava.util.Queue<E extends io.pravega.common.concurrent.Scheduled>- Overrides:
addin classjava.util.AbstractQueue<E extends io.pravega.common.concurrent.Scheduled>- Parameters:
e- the element to add- Returns:
true(as specified byCollection.add(E))- Throws:
java.lang.NullPointerException- if the specified element is null
-
offer
public boolean offer(E e)
Inserts the specified element into this delay queue.- Specified by:
offerin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Specified by:
offerin interfacejava.util.Queue<E extends io.pravega.common.concurrent.Scheduled>- Parameters:
e- the element to add- Returns:
true- Throws:
java.lang.NullPointerException- if the specified element is null
-
offer
public boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit)
Inserts the specified element into this delay queue. As the queue is unbounded this method will never block.- Specified by:
offerin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- 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:
java.lang.NullPointerException
-
put
public void put(E e)
Inserts the specified element into this delay queue. As the queue is unbounded this method will never block.- Specified by:
putin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Parameters:
e- the element to add- Throws:
java.lang.NullPointerException
-
peek
public E peek()
Retrieves, but does not remove, the head of this queue, or returnsnullif this queue is empty. Unlikepoll, this method returns the elements who's scheduled time has not yet arrived. (Items added concurrently may or may not be observed)- Specified by:
peekin interfacejava.util.Queue<E extends io.pravega.common.concurrent.Scheduled>- Returns:
- the head of this queue, or
nullif this queue is empty
-
size
public int size()
Returns the size of this collection. The value is only guaranteed to be accurate if there are not concurrent operations being performed. If there are, it may reflect the operation or not. This call is O(1).
-
remainingCapacity
public int remainingCapacity()
Always returnsInteger.MAX_VALUEbecause aScheduledQueueis not capacity constrained.- Specified by:
remainingCapacityin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Returns:
Integer.MAX_VALUE
-
toArray
public <T> T[] toArray(@Nonnull T[] a)Returns an array containing all of the elements in this queue. (Items added concurrently may or may not be included)- Specified by:
toArrayin interfacejava.util.Collection<E extends io.pravega.common.concurrent.Scheduled>- Overrides:
toArrayin classjava.util.AbstractCollection<E extends io.pravega.common.concurrent.Scheduled>- Parameters:
a- the 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- Returns:
- an array containing all of the elements in this queue
-
remove
public boolean remove(java.lang.Object o)
Removes a single instance of the specified element from this queue, if it is present, whether or not it has expired.- Specified by:
removein interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>- Specified by:
removein interfacejava.util.Collection<E extends io.pravega.common.concurrent.Scheduled>- Overrides:
removein classjava.util.AbstractCollection<E extends io.pravega.common.concurrent.Scheduled>
-
iterator
public java.util.Iterator<E> iterator()
Returns an iterator over all the items in the queue.The returned iterator is weakly consistent.
- Specified by:
iteratorin interfacejava.util.Collection<E extends io.pravega.common.concurrent.Scheduled>- Specified by:
iteratorin interfacejava.lang.Iterable<E extends io.pravega.common.concurrent.Scheduled>- Specified by:
iteratorin classjava.util.AbstractCollection<E extends io.pravega.common.concurrent.Scheduled>- Returns:
- an iterator over the elements in this queue
-
drainTo
public int drainTo(java.util.Collection<? super E> c)
- Specified by:
drainToin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
-
drainTo
public int drainTo(java.util.Collection<? super E> c, int maxElements)
- Specified by:
drainToin interfacejava.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
-
drainDelayed
public java.util.List<E> drainDelayed()
Clears all delayed tasks from the queue but leaves those which can be polled immediately.
-
-