Class ObjectPipe<E>

  • Type Parameters:
    E - The element type of the object pipe.
    All Implemented Interfaces:
    java.util.Iterator<E>

    public class ObjectPipe<E>
    extends java.lang.Object
    implements java.util.Iterator<E>
    This is a rather simple implementation of a queue designed for passing objects from one Thread to another to form a pipeline. The implementation allows a certain number of objects to be buffered between the two Threads. The writing thread will wait when the buffer is full, and the reading thread will wait while the buffer is empty. DO NOT allow the threads to wait for each other in any way apart from through this object, otherwise deadlocks will occur.
    Author:
    Matthew Wakeling
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectPipe()
      Construct an ObjectPipe with a default maximum buffer size.
      ObjectPipe​(int maxBuffer)
      Construct an ObjectPipe with a certain maximum buffer size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void finish()
      Marks the ObjectPipe as finished - that is, no further Objects will be put into the ObjectPipe.
      boolean hasNext()
      E next()
      void put​(E o)
      Adds an Object onto the end of the queue.
      void putAll​(java.util.Collection<? extends E> col)
      Adds a whole Collection of Objects onto the end of the queue, in the order that the Collection's Iterator presents them.
      void remove()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Constructor Detail

      • ObjectPipe

        public ObjectPipe()
        Construct an ObjectPipe with a default maximum buffer size.
      • ObjectPipe

        public ObjectPipe​(int maxBuffer)
        Construct an ObjectPipe with a certain maximum buffer size.
        Parameters:
        maxBuffer - the maximum buffer size, in objects
    • Method Detail

      • put

        public void put​(E o)
        Adds an Object onto the end of the queue.
        Parameters:
        o - an Object
      • putAll

        public void putAll​(java.util.Collection<? extends E> col)
        Adds a whole Collection of Objects onto the end of the queue, in the order that the Collection's Iterator presents them. Note that this method will add all of the objects if at the beginning the buffer is not full, even if it over-fills the buffer. This is not a problem, as no data is lost, and all the objects were taking up memory anyway. This also helps with performance, because it means that the threads will be switched between with larger granularity.
        Parameters:
        col - a Collection
      • finish

        public void finish()
        Marks the ObjectPipe as finished - that is, no further Objects will be put into the ObjectPipe. The hasNext() method will return false and the next() method will throw a NoSuchElementException once the buffer is emptied, instead of blocking for input. This method will wait until no other Threads are executing in put() or putAll().
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<E>
      • next

        public E next()
        Specified by:
        next in interface java.util.Iterator<E>
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<E>