Class ThreadLocalObjectPool<E extends AutoCloseable>

  • Type Parameters:
    E - pooled objects type

    public final class ThreadLocalObjectPool<E extends AutoCloseable>
    extends Object
    Special pool for managing limited number objects for further reuse. This pool maintains separate object bag for each thread by means of ThreadLocal.

    If object is borrowed on one thread and recycled on different then it will be returned to recycling thread bag. For thread-safe use either pooled objects should be thread-safe or happens-before should be established between borrowing object and subsequent recycling.

    • Constructor Detail

      • ThreadLocalObjectPool

        public ThreadLocalObjectPool​(int poolSize,
                                     Supplier<E> objFactory,
                                     Consumer<E> closer,
                                     Consumer<E> recycler)
        Parameters:
        poolSize - number of objects which pool can contain.
        objFactory - factory used for new objects creation.
        closer - close callback.
        recycler - recycle callback.
    • Method Detail

      • borrow

        public ThreadLocalObjectPool.Reusable borrow()
        Picks an object from the pool if one is present or creates new one otherwise. Returns an object wrapper which could be returned to the pool.
        Returns:
        reusable object wrapper.