public final class ThreadLocalHelper extends Object
ThreadLocal without locking.
Each thread keeps its own instance so no synchronisation is required. Where
WeakReferences are used the value can be reclaimed once nothing else
refers to it, preventing user-level leaks if the thread outlives the value.
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
getSTL(@NotNull ThreadLocal<T> threadLocal,
@NotNull Supplier<T> supplier)
Gets a strong thread-local value without needing locks.
|
static <T,A> T |
getTL(@NotNull ThreadLocal<WeakReference<T>> threadLocal,
A a,
@NotNull Function<A,T> function)
Gets a thread-local value via a function using a
WeakReference. |
static <T,A> T |
getTL(@NotNull ThreadLocal<WeakReference<T>> threadLocal,
A supplyingEntity,
@NotNull Function<A,T> constructor,
@Nullable ReferenceQueue<T> referenceQueue,
@Nullable Consumer<WeakReference<T>> registrar)
Gets a thread-local value via a function and records the weak reference.
|
static <T> T |
getTL(@NotNull ThreadLocal<WeakReference<T>> threadLocal,
@NotNull Supplier<T> supplier)
Gets a thread-local value backed by a
WeakReference without locks. |
@NotNull
public static <T> T getTL(@NotNull
@NotNull ThreadLocal<WeakReference<T>> threadLocal,
@NotNull
@NotNull Supplier<T> supplier)
WeakReference without locks.
The value is created via the supplier when absent. Storing the weak reference means it can be cleared when the caller drops the strong reference, avoiding memory leaks if the thread persists.
T - Type of value heldthreadLocal - ThreadLocal storing the weak referencesupplier - Supplier used to create the value on first access@NotNull
public static <T> T getSTL(@NotNull
@NotNull ThreadLocal<T> threadLocal,
@NotNull
@NotNull Supplier<T> supplier)
The supplier is called once per thread to create the value. The returned
object remains strongly referenced by the thread until removed, so use
getTL(ThreadLocal, Supplier) if the value should be reclaimable.
T - Type of value heldthreadLocal - ThreadLocal holding the valuesupplier - Supplier used to create the value when absent@NotNull
public static <T,A> T getTL(@NotNull
@NotNull ThreadLocal<WeakReference<T>> threadLocal,
A a,
@NotNull
@NotNull Function<A,T> function)
WeakReference.
Each thread holds its own instance so no locking is needed. The supplier is invoked only when the reference is absent or has been cleared.
T - Type of value heldA - Type of the supplied argumentthreadLocal - ThreadLocal storing the weak referencea - Input passed to the constructor functionfunction - Function used to create the value when required@NotNull
public static <T,A> T getTL(@NotNull
@NotNull ThreadLocal<WeakReference<T>> threadLocal,
@NotNull
A supplyingEntity,
@NotNull
@NotNull Function<A,T> constructor,
@Nullable
@Nullable ReferenceQueue<T> referenceQueue,
@Nullable
@Nullable Consumer<WeakReference<T>> registrar)
When a new value is created the reference may be registered in the given
referenceQueue using the supplied registrar. This lets a
background cleaner notice when the value has been cleared and remove any
associated state outside the event loop.
T - Type of value heldA - Type of the supplied argumentthreadLocal - ThreadLocal holding the weak referencesupplyingEntity - Argument passed to the constructorconstructor - Function to create the value if requiredreferenceQueue - Optional queue in which to enqueue the weak referenceregistrar - Optional consumer used to register the referenceCopyright © 2026 Chronicle Software Ltd. All rights reserved.