Class CloseableThreadLocal<T extends AutoCloseable>

java.lang.Object
com.linkedin.venice.utils.concurrent.CloseableThreadLocal<T>
Type Parameters:
T - The AutoCloseable type whose objects will be held by an object of this class.
All Implemented Interfaces:
AutoCloseable

public class CloseableThreadLocal<T extends AutoCloseable> extends Object implements 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 Details

    • CloseableThreadLocal

      public CloseableThreadLocal(Supplier<T> initialValue)
      Creates a closeable thread local. The initial value of the variable is determined by invoking the get method on the Supplier.
      Parameters:
      initialValue - the supplier to be used to determine the initial value for each Thread
  • Method Details

    • withInitial

      public static <T> CloseableThreadLocal withInitial(Supplier<T> initialValue)
    • get

      public T 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 the initialValue method.
      Returns:
      the current thread's value of this thread-local
    • close

      public void close()
      Clean up the resources held by this object. It triggers close 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 interface AutoCloseable