Class Queue

  • All Implemented Interfaces:
    Serializable

    public class Queue
    extends Object
    implements Serializable
    The Queue class implements a First-In-First-Out (FIFO) list of objects.

    A queue is for the exclusive use of one single consumer, whereas many producers may access it. It is ready for use after instantiation. A producer may wait for the queue to be empty by calling the stop() method. This method returns when the queue is actually empty, and prohibits any further call to the push method. To be able to use the queue again, it must be re-started through the start() method.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean closed
      true if the queue has been closed.
      private List<Object> elements
      The list holding queue elements.
      private static long serialVersionUID
      define serialVersionUID for interoperability
      private boolean stopping
      true if a producer called the stop() method.
    • Constructor Summary

      Constructors 
      Constructor Description
      Queue()
      Constructs a Queue instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all of the elements from this queue.
      void close()
      Closes the queue.
      Object get()
      Waits for an object to be pushed in the queue, and eventually returns it without removing it.
      Object get​(long timeout)
      Waits for an object to be pushed in the queue or a specified amount of time has elapsed.
      Object getAndPop()
      Waits for an object to be pushed in the queue, then removes and returns the object at the top of this queue.
      boolean isEmpty()
      Returns true if this queue contains no elements.
      String list()
      Returns a stringified form of the queue.
      Object pop()
      Removes and returns the object at the top of this queue.
      void push​(Object item)
      Pushes an item at the end of this queue.
      boolean search​(Comparator<Object> c, Object o2)  
      int size()
      Returns the number of elements in this list.
      void start()
      Authorizes the use of the queue by producers.
      void stop()
      Stops the queue by returning when it is empty and prohibiting any further producers call to the push method.
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        define serialVersionUID for interoperability
        See Also:
        Constant Field Values
      • elements

        private List<Object> elements
        The list holding queue elements.
      • stopping

        private boolean stopping
        true if a producer called the stop() method.
      • closed

        private boolean closed
        true if the queue has been closed.
    • Constructor Detail

      • Queue

        public Queue()
        Constructs a Queue instance.
    • Method Detail

      • push

        public void push​(Object item)
        Pushes an item at the end of this queue.
        Parameters:
        item - The item to be pushed at the end of this queue.
        Throws:
        StoppedQueueException - If the queue is stopping or stopped.
      • get

        public Object get()
                   throws InterruptedException
        Waits for an object to be pushed in the queue, and eventually returns it without removing it.
        Returns:
        The object at the top of this queue.
        Throws:
        InterruptedException - interrupted.
      • get

        public Object get​(long timeout)
                   throws InterruptedException
        Waits for an object to be pushed in the queue or a specified amount of time has elapsed. The first object is returned without removing it.
        Parameters:
        timeout - The maximum time to wait in milliseconds.
        Returns:
        The object at the top of this queue.
        Throws:
        InterruptedException - interrupted.
      • pop

        public Object pop()
        Removes and returns the object at the top of this queue.
        Returns:
        The object at the top of this queue.
        Throws:
        EmptyQueueException - If the queue is empty.
      • getAndPop

        public Object getAndPop()
                         throws InterruptedException
        Waits for an object to be pushed in the queue, then removes and returns the object at the top of this queue.
        Returns:
        The object at the top of this queue.
        Throws:
        InterruptedException - interrupted.
      • start

        public void start()
        Authorizes the use of the queue by producers.
      • stop

        public void stop()
                  throws InterruptedException
        Stops the queue by returning when it is empty and prohibiting any further producers call to the push method.
        Throws:
        InterruptedException - interrupted.
      • isEmpty

        public boolean isEmpty()
        Returns true if this queue contains no elements.
        Returns:
        true if this queue contains no elements, false otherwise.
      • clear

        public void clear()
        Removes all of the elements from this queue.
      • size

        public int size()
        Returns the number of elements in this list.
        Returns:
        the number of elements in this list.
      • list

        public String list()
        Returns a stringified form of the queue.
        Returns:
        a stringified form of the queue.