Class ScheduledQueue<E extends io.pravega.common.concurrent.Scheduled>

  • 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 which Scheduled items can be added to. Items which are scheduled will not be returned from poll() or take() 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
      boolean add​(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.
      int drainTo​(java.util.Collection<? super E> c)  
      int drainTo​(java.util.Collection<? super E> c, int maxElements)  
      java.util.Iterator<E> iterator()
      Returns an iterator over all the items in the queue.
      boolean offer​(E e)
      Inserts the specified element into this delay queue.
      boolean offer​(E e, long timeout, java.util.concurrent.TimeUnit unit)
      Inserts the specified element into this delay queue.
      E peek()
      Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
      E poll()  
      E poll​(long timeout, java.util.concurrent.TimeUnit unit)  
      void put​(E e)
      Inserts the specified element into this delay queue.
      int remainingCapacity()
      Always returns Integer.MAX_VALUE because a ScheduledQueue is not capacity constrained.
      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.
      int size()
      Returns the size of this collection.
      E take()
      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.AbstractQueue

        addAll, clear, element, remove
      • 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
      • Methods inherited from interface java.util.concurrent.BlockingQueue

        contains
      • Methods inherited from interface java.util.Collection

        addAll, clear, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Queue

        element, remove
    • Constructor Detail

      • ScheduledQueue

        public ScheduledQueue()
    • 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:
        take in interface java.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:
        poll in interface java.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
        Throws:
        java.lang.InterruptedException
      • poll

        public E poll()
        Specified by:
        poll in interface java.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:
        add in interface java.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        add in interface java.util.Collection<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        add in interface java.util.Queue<E extends io.pravega.common.concurrent.Scheduled>
        Overrides:
        add in class java.util.AbstractQueue<E extends io.pravega.common.concurrent.Scheduled>
        Parameters:
        e - the element to add
        Returns:
        true (as specified by Collection.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:
        offer in interface java.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        offer in interface java.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:
        offer in interface java.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
        Parameters:
        e - the element to add
        timeout - This parameter is ignored as the method never blocks
        unit - 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:
        put in interface java.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 returns null if this queue is empty. Unlike poll, this method returns the elements who's scheduled time has not yet arrived. (Items added concurrently may or may not be observed)
        Specified by:
        peek in interface java.util.Queue<E extends io.pravega.common.concurrent.Scheduled>
        Returns:
        the head of this queue, or null if 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).
        Specified by:
        size in interface java.util.Collection<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        size in class java.util.AbstractCollection<E extends io.pravega.common.concurrent.Scheduled>
      • remainingCapacity

        public int remainingCapacity()
        Always returns Integer.MAX_VALUE because a ScheduledQueue is not capacity constrained.
        Specified by:
        remainingCapacity in interface java.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:
        toArray in interface java.util.Collection<E extends io.pravega.common.concurrent.Scheduled>
        Overrides:
        toArray in class java.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:
        remove in interface java.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        remove in interface java.util.Collection<E extends io.pravega.common.concurrent.Scheduled>
        Overrides:
        remove in class java.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:
        iterator in interface java.util.Collection<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        iterator in interface java.lang.Iterable<E extends io.pravega.common.concurrent.Scheduled>
        Specified by:
        iterator in class java.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:
        drainTo in interface java.util.concurrent.BlockingQueue<E extends io.pravega.common.concurrent.Scheduled>
      • drainTo

        public int drainTo​(java.util.Collection<? super E> c,
                           int maxElements)
        Specified by:
        drainTo in interface java.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.