Class ExplicitThreadLocal<U>

java.lang.Object
io.github.jbellis.jvector.util.ExplicitThreadLocal<U>

public abstract class ExplicitThreadLocal<U> extends Object
The standard ThreadLocal appears to be designed to be used with relatively short-lived Threads. Specifically, it uses a ThreadLocalMap to store ThreadLocal key/value Entry objects, and there are no guarantees as to when Entry references are expunged unless you can explicitly call remove() on the ThreadLocal instance. This means that objects referenced by ThreadLocals will not be able to be GC'd for the lifetime of the Thread, effectively "leaking" these objects even if there are no other references.

This makes ThreadLocal a bad fit for long-lived threads, such as those in the thread pools used by JVector.

Because ExplicitThreadLocal doesn't hook into Thread internals, any referenced values can be GC'd as expected as soon as the ETL instance itself is no longer referenced.

ExplicitThreadLocal is a drop-in replacement for ThreadLocal, and is used in the same way.

  • Constructor Details

    • ExplicitThreadLocal

      public ExplicitThreadLocal()
  • Method Details

    • get

      public U get()
    • initialValue

      protected abstract U initialValue()
    • withInitial

      public static <U> ExplicitThreadLocal<U> withInitial(Supplier<U> initialValue)