T
- The type of resource this CleaningThreadLocal holds.public class CleaningThreadLocal<T> extends ThreadLocal<T>
Note that this class does not clean up resources if the ThreadLocal instance itself is discarded.
Modifier and Type | Method and Description |
---|---|
void |
cleanup(T value)
Performs cleanup of the provided value.
|
static void |
cleanupNonCleaningThreads()
Cleans up resources held by threads that are no longer alive.
|
T |
get()
Returns the value of this CleaningThreadLocal.
|
protected T |
initialValue()
Returns the initial value of this CleaningThreadLocal.
|
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.
|
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 strategy.
|
static <T> CleaningThreadLocal<T> |
withCloseQuietly(Supplier<T> supplier)
Creates a CleaningThreadLocal with a Closeable cleanup strategy.
|
withInitial
public static <T> CleaningThreadLocal<T> withCloseQuietly(Supplier<T> supplier)
supplier
- The supplier that provides the resource.public static <T> CleaningThreadLocal<T> withCleanup(ThrowingConsumer<T,Exception> cleanup)
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()
protected T initialValue()
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(T value)
value
- The value to be cleaned up.Copyright © 2023. All rights reserved.