Package net.logstash.logback.util
Class ThreadLocalHolder<T>
java.lang.Object
net.logstash.logback.util.ThreadLocalHolder<T>
- Type Parameters:
T
- type of instances returned by thisThreadLocalHolder
.
- Direct Known Subclasses:
ThreadLocalReusableByteBuffer
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
Modifier and TypeClassDescriptionprotected class
AWeakReference
to a thread with theThreadLocalHolder.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
Modifier and TypeFieldDescriptionprotected final Map<Long,
ThreadLocalHolder<T>.HolderRef> Collection of values assigned to each thread -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal 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.void
close()
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()
Release the value and recycle it if possible.
-
Field Details
-
threadValues
Collection of values assigned to each thread
-
-
Constructor Details
-
ThreadLocalHolder
Create a new instance of the pool.- Parameters:
factory
- the factory used to create new instances.
-
-
Method Details
-
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 berelease()
to ensure proper life cycle before it can beacquire()
again.- Returns:
- the value assigned to this thread
- Throws:
IllegalStateException
- if the value is already in use andrelease()
was not yet invoked.
-
release
public final void release()Release the value and recycle it if possible.- Throws:
IllegalStateException
- if the value was not previouslyacquire()
.
-
close
public void close() -
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
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
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.
-