Class SynchronizedQueue<E>

java.lang.Object
rs.baselib.util.SynchronizedQueue<E>

public class SynchronizedQueue<E>
extends java.lang.Object
Implements a thread-safe synchronized object queue that also wakes up waiting threads.
Author:
ralph
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.util.List<E> available
    The object queue
    protected int capacity
    The maximum number of objects to be held in this queue instance
    static int MAX_CAPACITY
    Default number of objects to hold in this queue
  • Constructor Summary

    Constructors 
    Constructor Description
    SynchronizedQueue()
    Constructor.
    SynchronizedQueue​(int capacity)
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    E poll()
    Delivers the next object from the queue.
    void push​(E o)
    Adds a new object to the list of available objects.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_CAPACITY

      public static final int MAX_CAPACITY
      Default number of objects to hold in this queue
      See Also:
      Constant Field Values
    • available

      protected java.util.List<E> available
      The object queue
    • capacity

      protected int capacity
      The maximum number of objects to be held in this queue instance
  • Constructor Details

    • SynchronizedQueue

      public SynchronizedQueue()
      Constructor.

      Will create the queue using MAX_CAPACITY as capacity.

    • SynchronizedQueue

      public SynchronizedQueue​(int capacity)
      Constructor.
      Parameters:
      capacity - - capacity of this queue
  • Method Details

    • poll

      public E poll()
      Delivers the next object from the queue.

      The method will block when no object is available.

      Used by the reader/consumer thread.

      Returns:
      next object in queue
    • push

      public void push​(E o)
      Adds a new object to the list of available objects.

      The method will block when the queue is full.

      Used by the writer/producer thread.

      Parameters:
      o - - the object to be queued