Class PushPullQueue<T>

  • Type Parameters:
    T - the type of object in this queue.


    public class PushPullQueue<T>
    extends Object
    This is a one-element list; at most only one element can exist in this queue at any time. It is intended to be used by two threads for handing off information.

    In the future this can be replaced with a SynchronousQueue when this codebase is adapted to Java 1.5. But for Java 1.4 I ended up adapting my own solution.

    • Constructor Summary

      Constructors

      Constructor and Description
      PushPullQueue()
       
    • Method Summary

      Modifier and Type Method and Description
      boolean isEmpty()
       
      protected void iteratePull()
      This method is called during pull(...).
      protected void iteratePush()
      This method is called during push(...).
      T pull(long timeout)
      Pulls an element from the queue.
      void push(T newObject)
      This pushes an object on the queue and immediately returns.
      void push(T newObject, long timeout)
      This puts on object on the queue, and then blocks until another thread removes it.
    • Constructor Detail

      • PushPullQueue

        public PushPullQueue()
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Returns:
        true if there is no object waiting to be removed.
      • pull

        public T pull(long timeout)
        Pulls an element from the queue.
        Parameters:
        timeout - the duration to wait, or a non-positive value if this request should never timeout.
        Returns:
        an object recently added to this queue. This will never return null.
      • push

        public void push(T newObject)
        This pushes an object on the queue and immediately returns. Unlike the other push() method this does not wait for another thread to pull this value before returning.
        Parameters:
        newObject - the newObject to add to the queue.
      • push

        public void push(T newObject,
                         long timeout)
        This puts on object on the queue, and then blocks until another thread removes it.
        Parameters:
        newObject - the object to add to the queue.
        timeout - the duration to wait, or a non-positive value if this method should never timeout.
      • iteratePush

        protected void iteratePush()
        This method is called during push(...).

        Currently this does nothing, but subclasses can override it to abort push operations by throwing a RuntimeException if this operation is aborted.

      • iteratePull

        protected void iteratePull()
        This method is called during pull(...).

        Currently this does nothing, but subclasses can override it to abort pull operations by throwing a RuntimeException if this operation is aborted.