Class Pool<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    java.lang.AutoCloseable, Dumpable

    public class Pool<T>
    extends java.lang.Object
    implements java.lang.AutoCloseable, Dumpable
    A fast pool of objects, with optional support for multiplexing, max usage count and several optimized strategies plus an optional ThreadLocal cache of the last release entry.

    When the method close() is called, all Closeables in the pool are also closed.

    • Constructor Detail

      • Pool

        public Pool​(Pool.StrategyType strategyType,
                    int maxEntries)
        Construct a Pool with a specified lookup strategy and no ThreadLocal cache.
        strategyType - The strategy to used for looking up entries.
        maxEntries - the maximum amount of entries that the pool will accept.
      • Pool

        public Pool​(Pool.StrategyType strategyType,
                    int maxEntries,
                    boolean cache)
        Construct a Pool with the specified thread-local cache size and an optional ThreadLocal cache.
        strategyType - The strategy to used for looking up entries.
        maxEntries - the maximum amount of entries that the pool will accept.
        cache - True if a ThreadLocal cache should be used to try the most recently released entry.
    • Method Detail

      • getReservedCount

        public int getReservedCount()
      • getIdleCount

        public int getIdleCount()
      • getInUseCount

        public int getInUseCount()
      • getMaxEntries

        public int getMaxEntries()
      • getMaxMultiplex

        public int getMaxMultiplex()
      • setMaxMultiplex

        public final void setMaxMultiplex​(int maxMultiplex)
      • getMaxUsageCount

        public int getMaxUsageCount()
      • setMaxUsageCount

        public final void setMaxUsageCount​(int maxUsageCount)
      • reserve

        public Pool.Entry reserve​(int allotment)
        Create a new disabled slot into the pool. The returned entry must ultimately have the Pool.Entry.enable(Object, boolean) method called or be removed via Pool.Entry.remove() or remove(Pool.Entry).
        allotment - the desired allotment, where each entry handles an allotment of maxMultiplex, or a negative number to always trigger the reservation of a new entry.
        a disabled entry that is contained in the pool, or null if the pool is closed or if the pool already contains getMaxEntries() entries, or the allotment has already been reserved
      • acquireAt

        public Pool.Entry acquireAt​(int idx)
        No longer supported. Instead use a Pool.StrategyType to configure the pool.
        Acquire the entry from the pool at the specified index. This method bypasses the thread-local mechanism.
        idx - the index of the entry to acquire.
        the specified entry or null if there is none at the specified index or if it is not available.
      • acquire

        public Pool.Entry acquire()
        Acquire an entry from the pool. Only enabled entries will be returned from this method and their enable method must not be called.
        an entry from the pool or null if none is available.
      • acquire

        public Pool.Entry acquire​(java.util.function.Function<Pool.Entry,​T> creator)
        Utility method to acquire an entry from the pool, reserving and creating a new entry if necessary.
        creator - a function to create the pooled value for a reserved entry.
        an entry from the pool or null if none is available.
      • release

        public boolean release​(Pool.Entry entry)
        This method will return an acquired object to the pool. Objects that are acquired from the pool but never released will result in a memory leak.
        entry - the value to return to the pool
        true if the entry was released and could be acquired again, false if the entry should be removed by calling remove(Pool.Entry) and the object contained by the entry should be disposed.
        java.lang.NullPointerException - if value is null
      • remove

        public boolean remove​(Pool.Entry entry)
        Remove a value from the pool.
        entry - the value to remove
        true if the entry was removed, false otherwise
      • isClosed

        public boolean isClosed()
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • size

        public int size()
      • values

        public java.util.Collection<Pool.Entry> values()
      • dump

        public void dump​(java.lang.Appendable out,
                         java.lang.String indent)
        Description copied from interface: Dumpable
        Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.
        Specified by:
        dump in interface Dumpable
        out - The appendable to dump to
        indent - The indent to apply after any new lines.
        Throws: - if unable to write to Appendable
      • toString

        public java.lang.String toString()
        toString in class java.lang.Object