Class Pool<T>


  • public abstract class Pool<T>
    extends Object
    A pool of objects that can be reused to avoid allocations. The pool is optionally thread safe and can be configured to use soft references.
    Author:
    Nathan Sweet, Martin Grotzke
    • Constructor Summary

      Constructors 
      Constructor Description
      Pool​(boolean threadSafe, boolean softReferences)
      Creates a pool with no maximum.
      Pool​(boolean threadSafe, boolean softReferences, int maximumCapacity)  
    • Constructor Detail

      • Pool

        public Pool​(boolean threadSafe,
                    boolean softReferences)
        Creates a pool with no maximum.
      • Pool

        public Pool​(boolean threadSafe,
                    boolean softReferences,
                    int maximumCapacity)
        Parameters:
        maximumCapacity - The maximum number of free objects to store in this pool. Objects are not created until obtain() is called and no free objects are available.
    • Method Detail

      • create

        protected abstract T create()
      • obtain

        public T obtain()
        Returns an object from this pool. The object may be new (from create()) or reused (previously freed).
      • free

        public void free​(T object)
        Puts the specified object in the pool, making it eligible to be returned by obtain(). If the pool already contains the maximum number of free objects, the specified object is reset but not added to the pool.

        If using soft references and the pool contains the maximum number of free objects, the first soft reference whose object has been garbage collected is discarded to make room.

      • reset

        protected void reset​(T object)
        Called when an object is freed to clear the state of the object for possible later reuse. The default implementation calls Pool.Poolable.reset() if the object is Pool.Poolable.
      • clear

        public void clear()
        Removes all free objects from this pool.
      • clean

        public void clean()
        If using soft references, all soft references whose objects have been garbage collected are removed from the pool. This can be useful to reduce the number of objects in the pool before calling getFree() or when the pool has no maximum capacity. It is not necessary to call clean() before calling free(Object), which will try to remove an empty reference if the maximum capacity has been reached.
      • getFree

        public int getFree()
        The number of objects available to be obtained.

        If using soft references, this number may include objects that have been garbage collected. clean() may be used first to remove empty soft references.

      • getPeak

        public int getPeak()
        The all-time highest number of free objects. This can help determine if a pool's maximum capacity is set appropriately. It can be reset any time with resetPeak().

        If using soft references, this number may include objects that have been garbage collected.

      • resetPeak

        public void resetPeak()