Class CloseableThreadLocal<T extends AutoCloseable>
java.lang.Object
com.linkedin.venice.utils.concurrent.CloseableThreadLocal<T>
- Type Parameters:
T
- TheAutoCloseable
type whose objects will be held by an object of this class.
- All Implemented Interfaces:
AutoCloseable
A wrapper of
ThreadLocal
for AutoCloseable
objects.
Java's ThreadLocal
only dereferences the thread-local objects
when the Thread
exits which triggers their garbage collection.
It does not close
the AutoCloseable
thread-local objects.-
Constructor Summary
ConstructorDescriptionCloseableThreadLocal
(Supplier<T> initialValue) Creates a closeable thread local. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Clean up the resources held by this object.get()
Returns the value in the current thread's copy of this thread-local variable.static <T> CloseableThreadLocal
withInitial
(Supplier<T> initialValue)
-
Constructor Details
-
CloseableThreadLocal
Creates a closeable thread local. The initial value of the variable is determined by invoking theget
method on theSupplier
.- Parameters:
initialValue
- the supplier to be used to determine the initial value for each Thread
-
-
Method Details
-
withInitial
-
get
Returns the value in the current thread's copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of theinitialValue
method.- Returns:
- the current thread's value of this thread-local
-
close
public void close()Clean up the resources held by this object. It triggersclose
on each thread's value. It is the responsibility of the caller to ensure that all threads have finished processing the thread-local objects.- Specified by:
close
in interfaceAutoCloseable
-