Class FixedInstancePool<T>

  • Type Parameters:
    T - the class of objects stored in the instance pool

    abstract class FixedInstancePool<T>
    extends java.lang.Object
    A very simple object instance pool with a fixed size.

    This is essentially an immutable circular queue. Elements are not added nor removed. Pointers to the head and tail of the queue identify what is the next available entry.

    Use allocate() to get an available Entry from the pool. If all objects are allocated then the thread will block until an element is released.

    release(Entry) releases an allocated Entry for reuse.

    See Also:
    Entry
    • Constructor Summary

      Constructors 
      Constructor Description
      FixedInstancePool​(int size)
      Creates a new instance pool with the given size.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      Entry<T> allocate()
      Retrieves the next available entry in this instance pool.
      protected abstract T newInstance()
      Creates a new instance of the given type of objects stored as entries of this instance pool This method is called in the constructor of this class for initialization of the instance array and must always return a new instance.
      void release​(Entry<T> e)
      Releases the given entry and makes it available for allocation (by allocate())
      • Methods inherited from class java.lang.Object

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

      • instancePool

        final Entry<T>[] instancePool
      • instanceIndexes

        private final int[] instanceIndexes
      • head

        private int head
      • tail

        private int tail
      • count

        int count
      • lastInstanceIndex

        private int lastInstanceIndex
    • Constructor Detail

      • FixedInstancePool

        FixedInstancePool​(int size)
        Creates a new instance pool with the given size. Upon instantiation, the newInstance() method will be called to fill in the instance pool, and the pool can then have its entries allocated for use (and reuse).
        Parameters:
        size - the size of the fixed instance pool.
    • Method Detail

      • newInstance

        protected abstract T newInstance()
        Creates a new instance of the given type of objects stored as entries of this instance pool This method is called in the constructor of this class for initialization of the instance array and must always return a new instance.
        Returns:
        returns a new instance to use in the pool
      • allocate

        public Entry<T> allocate()
        Retrieves the next available entry in this instance pool. Blocks until an entry becomes available (through release(Entry)).
        Returns:
        the next available entry in this instance pool
      • release

        public void release​(Entry<T> e)
        Releases the given entry and makes it available for allocation (by allocate())
        Parameters:
        e - the entry to be released and made available for reuse.