T - the type stored in the thread-local variablepublic class CleaningThreadLocal<T> extends ThreadLocal<T>
CleaningThreadLocal augments ThreadLocal with two additional
capabilities:
CleaningThreads.CleaningThread is cleaned immediately
(still on that same thread) when it is replaced or remove() is called.CleaningThread are registered globally. The static method
cleanupNonCleaningThreads() can be invoked-on a timer, in a
background executor, or during JVM shutdown-to free resources that
belong to threads which have already terminated.This class is typically used for off-heap buffers, direct I/O handles,
Closeables, or any resource that must be released
deterministically even when user code forgets to call close().
Orphan tracking can be toggled in two mutually-aware ways (highest precedence first):
-Ddisable.ctl.orphan.tracking
globally disables orphan tracking for every
CleaningThreadLocal in the JVM, regardless of assertions or
constructor flags. This is the easiest "set-and-forget" option for
ultra-low-latency production deployments.-ea ).
This keeps the production fast path allocation-free by default.Summary of common launch options:
# production, no tracking java -Ddisable.ctl.orphan.tracking ... # production, tracking ON for diagnostics java -ea:net.openhft.chronicle.core.threads.CleaningThreadLocal -cp ... # tests: assertions on, but tracking OFF for this one class java -ea -da:net.openhft.chronicle.core.threads.CleaningThreadLocal ...
CleaningThread,
cleanupNonCleaningThreads()| Modifier and Type | Method and Description |
|---|---|
void |
cleanup(T value)
Idempotent helper that applies
cleanup to value. |
static void |
cleanupNonCleaningThreads()
Performs a single orphan-sweep:
Iterates through all live
CleaningThreadLocals that track
non-CleaningThreads.
Identifies threads that have terminated.
Invokes the configured cleanup for each stale value.
Purges the entry from internal bookkeeping.
|
T |
get()
Returns the value of this CleaningThreadLocal.
|
protected T |
initialValue()
Returns the initial value and records it for later cleanup when the
thread is not a
CleaningThread. |
void |
remove()
Removes the value for this CleaningThreadLocal from the current thread and performs cleanup.
|
void |
set(T value)
Sets the value of this CleaningThreadLocal and performs cleanup if necessary.
|
String |
toString() |
static <T> CleaningThreadLocal<T> |
withCleanup(Supplier<T> supplier,
ThrowingConsumer<T,Exception> cleanup)
Creates a CleaningThreadLocal with a supplier and a custom cleanup strategy.
|
static <T> CleaningThreadLocal<T> |
withCleanup(Supplier<T> supplier,
ThrowingConsumer<T,Exception> cleanup,
Function<T,T> getWrapper)
Creates a CleaningThreadLocal with a supplier, a custom cleanup strategy, and a function to apply when the get method is called.
|
static <T> CleaningThreadLocal<T> |
withCleanup(ThrowingConsumer<T,Exception> cleanup)
Creates a
CleaningThreadLocal with a custom cleanup action but
without an initial-value supplier. |
static <T> CleaningThreadLocal<T> |
withCloseQuietly(Supplier<T> supplier)
Creates a
CleaningThreadLocal whose cleanup simply calls
Closeable.closeQuietly(java.lang.Object...). |
withInitialpublic static <T> CleaningThreadLocal<T> withCloseQuietly(Supplier<T> supplier)
CleaningThreadLocal whose cleanup simply calls
Closeable.closeQuietly(java.lang.Object...).T - any subtype of Closeablesupplier - supplies the resource (may return null)CleaningThreadLocalpublic static <T> CleaningThreadLocal<T> withCleanup(ThrowingConsumer<T,Exception> cleanup)
CleaningThreadLocal with a custom cleanup action but
without an initial-value supplier.cleanup - The consumer that cleans up the resource.public static <T> CleaningThreadLocal<T> withCleanup(Supplier<T> supplier, ThrowingConsumer<T,Exception> cleanup)
supplier - The supplier that provides the resource.cleanup - The consumer that cleans up the resource.public static <T> CleaningThreadLocal<T> withCleanup(Supplier<T> supplier, ThrowingConsumer<T,Exception> cleanup, Function<T,T> getWrapper)
supplier - The supplier that provides the resource.cleanup - The consumer that cleans up the resource.getWrapper - The function to apply when the get method is called.public static void cleanupNonCleaningThreads()
CleaningThreadLocals that track
non-CleaningThreads.Call at whatever cadence suits your application (e.g. every few seconds, once a minute, or only at JVM shutdown).
protected T initialValue()
CleaningThread.initialValue in class ThreadLocal<T>public T get()
get in class ThreadLocal<T>public void set(T value)
set in class ThreadLocal<T>value - The new value to be set.public void remove()
remove in class ThreadLocal<T>public void cleanup(@Nullable
T value)
cleanup to value.
Any exception thrown by user code is swallowed and logged so that
cleanup can never compromise the core invariant of this class.Copyright © 2026 Chronicle Software Ltd. All rights reserved.