public class CleaningThread extends Thread
Threads can retain references left in their ThreadLocalMap even once
the ThreadLocal instance is no longer reachable. Long-lived threads
may therefore accumulate stale values and leak memory. Reflection is used to
walk the map and remove any entries created by CleaningThreadLocal so
they are not retained after the task completes.
Thread.State, Thread.UncaughtExceptionHandlerMAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY| Constructor and Description |
|---|
CleaningThread(Runnable target)
Constructs a new CleaningThread with the specified target Runnable.
|
CleaningThread(Runnable target,
String name)
Constructs a new CleaningThread with the specified target Runnable and name.
|
CleaningThread(Runnable target,
String name,
boolean inEventLoop)
Constructs a new CleaningThread with the specified target Runnable, name, and inEventLoop flag.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
inEventLoop()
Returns the inEventLoop flag, indicating whether this thread is in an event loop.
|
static boolean |
inEventLoop(Thread t)
Checks if the given thread is an instance of CleaningThread and if it is in an event loop.
|
static void |
performCleanup(Thread thread)
Purges all
CleaningThreadLocal entries for the supplied thread. |
static void |
performCleanup(Thread thread,
CleaningThreadLocal<?> ctl)
Cleans up a specific
CleaningThreadLocal instance associated with the given thread. |
void |
run()
Executes the target
Runnable and then clears thread-local values. |
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yieldpublic CleaningThread(Runnable target)
target - The Runnable object to execute in the new thread.public CleaningThread(Runnable target, String name)
target - The Runnable object to execute in the new thread.name - The name for the new thread.public CleaningThread(Runnable target, String name, boolean inEventLoop)
target - The Runnable object to execute in the new thread.name - The name for the new thread.inEventLoop - The flag indicating whether the thread is in an event loop.public static void performCleanup(Thread thread)
CleaningThreadLocal entries for the supplied thread.
Thread-local maps hold strong references to their values until removed. If a
task discards its CleaningThreadLocal but the thread lives on, those
values leak. This method walks the hidden map via reflection and removes the
entries we created. If garbage collection has cleared a weak key the value
cannot be located and remains until the table is resized.
thread - the thread whose CleaningThreadLocal instances are to be
clearedpublic static void performCleanup(Thread thread, CleaningThreadLocal<?> ctl)
CleaningThreadLocal instance associated with the given thread.thread - The thread whose specific CleaningThreadLocal instance is to be cleaned up.ctl - The specific CleaningThreadLocal instance to clean up. If null, cleans all.public static boolean inEventLoop(Thread t)
t - The thread to check.public void run()
Runnable and then clears thread-local values.
If an event loop pinned this thread to a single CPU, the affinity is
reset to AffinityLock.BASE_AFFINITY before user code runs so the
binding does not leak once the loop has finished.
Sub-classes overriding this method should call
super.run() to retain the affinity reset and cleanup behaviour.
public boolean inEventLoop()
Copyright © 2026 Chronicle Software Ltd. All rights reserved.