Class ThreadLocalHolder<T>

java.lang.Object
net.logstash.logback.util.ThreadLocalHolder<T>
Type Parameters:
T - type of instances returned by this ThreadLocalHolder.
Direct Known Subclasses:
ThreadLocalReusableByteBuffer

public class ThreadLocalHolder<T> extends Object
Maintains a per-thread value created by the Supplier given to the constructor.

A thread obtains the value by calling acquire() and must release it after use by calling release(). If the value is not released, subsequent calls to acquire() will throw an IllegalStateException.

Instances value may also implement the optional ThreadLocalHolder.Lifecycle interface if they wish to be notified when they are recycled or disposed.

The holder keeps track of each requesting thread and takes care of disposing the allocated value when it dies. All allocated values are automatically disposed when close() is called.

Note: This class is for internal use only and subject to backward incompatible change at any time.

Author:
brenuart
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected class 
    A WeakReference to a thread with the ThreadLocalHolder.Holder assigned to it.
    static interface 
    Optional interface that pooled instances may implement if they wish to be notified of life cycle events.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Collection of values assigned to each thread
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new instance of the pool.
  • Method Summary

    Modifier and Type
    Method
    Description
    final T
    Get the value assigned to the current thread, creating a new one if none is assigned yet or the previous has been disposed.
    void
    Close the holder and dispose all values.
    protected T
    Create a new object instance (must be non-null).
    protected void
    disposeInstance(T instance)
    Dispose the object instance by calling its life cycle methods.
    protected boolean
    recycleInstance(T instance)
    Recycle the instance before returning it to the pool.
    final void
    Release the value and recycle it if possible.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • ThreadLocalHolder

      public ThreadLocalHolder(Supplier<T> factory)
      Create a new instance of the pool.
      Parameters:
      factory - the factory used to create new instances.
  • Method Details

    • acquire

      public final T acquire()
      Get the value assigned to the current thread, creating a new one if none is assigned yet or the previous has been disposed. The value must be release() to ensure proper life cycle before it can be acquire() again.
      Returns:
      the value assigned to this thread
      Throws:
      IllegalStateException - if the value is already in use and release() was not yet invoked.
    • release

      public final void release()
      Release the value and recycle it if possible.
      Throws:
      IllegalStateException - if the value was not previously acquire().
    • close

      public void close()
      Close the holder and dispose all values. Threads are still able to acquire() values after the holder is closed, but they will be disposed immediately when release() instead of recycled.
    • createInstance

      protected T createInstance()
      Create a new object instance (must be non-null). Sub-classes may override this method to implement their own custom logic if needed.
      Returns:
      a new object instance
    • disposeInstance

      protected void disposeInstance(T instance)
      Dispose the object instance by calling its life cycle methods. Sub-classes may override this method if they wish to implement their own custom logic.
      Parameters:
      instance - the instance to dispose
    • recycleInstance

      protected boolean recycleInstance(T instance)
      Recycle the instance before returning it to the pool. Sub-classes may override this method if they wish to implement their own custom logic.
      Parameters:
      instance - the instance to recycle
      Returns:
      true if the instance can be recycled and returned to the pool, false if not.