Class SuppliedInheritableThreadLocal<T>
- Type Parameters:
T- the type of value stored in the thread-local variable
InheritableThreadLocal that allows:
- Specifying a
Supplierto provide the initial value. - Optionally defining a
Functionto copy the value for child threads.
This is useful when you want thread-local variables that are inherited by child threads, but with controlled initialization and optional cloning/copying of values.
Features:
- Automatic initial value using a
Supplier. - Optional copy function to transform or clone parent value for child threads.
- If no copy function is provided and the value implements
Cloneable, theclonemethod is * invoked. - If neither copy function nor clone is available, the parent reference is shared.
Example usage:
InheritableThreadLocal<Map<String, Object>> threadLocal =
SuppliedInheritableThreadLocal.withInitial(HashMap::new, HashMap::new);
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSuppliedInheritableThreadLocal(Supplier<? extends T> supplier, boolean shareParentRef) Constructs aSuppliedInheritableThreadLocalwith a supplier for the initial value.privateSuppliedInheritableThreadLocal(Supplier<? extends T> supplier, boolean shareParentRef, Function<T, T> copyFunction) Constructs aSuppliedInheritableThreadLocalwith a supplier and a copy function to control the value passed to child threads. -
Method Summary
Modifier and TypeMethodDescriptionprotected TchildValue(T parentValue) Determines the value to be passed to a child thread.protected TProvides the initial value for the current thread by calling the supplier.static <S> InheritableThreadLocal<S>withInitial(Supplier<? extends S> supplier) Creates a thread-local variable initialized from a supplier.static <S> InheritableThreadLocal<S>withInitial(Supplier<? extends S> supplier, boolean shareParentRef) Creates a thread-local variable initialized from a supplier.static <S> InheritableThreadLocal<S>withInitial(Supplier<? extends S> supplier, boolean shareParentRef, Function<S, S> copyFunction) Creates a thread-local variable with a supplier, a share flag, and a copy function.static <S> InheritableThreadLocal<S>withInitial(Supplier<? extends S> supplier, Function<S, S> copyFunction) Creates a thread-local variable with a supplier and a copy function.Methods inherited from class java.lang.ThreadLocal
get, remove, set
-
Field Details
-
supplier
-
copyFunction
-
-
Constructor Details
-
SuppliedInheritableThreadLocal
Constructs aSuppliedInheritableThreadLocalwith a supplier for the initial value.The initial value for each thread is obtained by calling
Supplier.get(). TheshareParentRefflag determines whether child threads will inherit the parent value by reference (true) or start with a new value from the supplier (false).- Parameters:
supplier- the supplier to provide the initial value for each thread; must not be nullshareParentRef- if true, child threads share the same reference as the parent; if false, child threads start with a fresh value from the supplier- Throws:
NullPointerException- if the supplier is null
-
SuppliedInheritableThreadLocal
private SuppliedInheritableThreadLocal(Supplier<? extends T> supplier, boolean shareParentRef, Function<T, T> copyFunction) Constructs aSuppliedInheritableThreadLocalwith a supplier and a copy function to control the value passed to child threads.The
copyFunctionis used to produce the child thread's value from the parent thread's value. This takes precedence over theshareParentRefflag.- Parameters:
supplier- the supplier for initial values; must not be nullshareParentRef- ignored ifcopyFunctionis provided; if no copy function, controls reference * sharingcopyFunction- a function to copy/transform the parent value for child threads; must not be null- Throws:
NullPointerException- if either supplier or copyFunction is null
-
-
Method Details
-
withInitial
Creates a thread-local variable initialized from a supplier. Child threads do not share the parent reference by default.- Type Parameters:
S- the type of the thread-local's value- Parameters:
supplier- the supplier for initial values; must not be null- Returns:
- a new
InheritableThreadLocalinstance - Throws:
NullPointerException- if the supplier is null
-
withInitial
public static <S> InheritableThreadLocal<S> withInitial(Supplier<? extends S> supplier, boolean shareParentRef) Creates a thread-local variable initialized from a supplier.- Type Parameters:
S- the type of the thread-local's value- Parameters:
supplier- the supplier for initial values; must not be nullshareParentRef- if true, child threads inherit the parent's value by reference; otherwise a new value * from the supplier- Returns:
- a new
InheritableThreadLocalinstance - Throws:
NullPointerException- if the supplier is null
-
withInitial
public static <S> InheritableThreadLocal<S> withInitial(Supplier<? extends S> supplier, Function<S, S> copyFunction) Creates a thread-local variable with a supplier and a copy function.The copy function is used to produce the child thread's value from the parent thread's value. The
shareParentRefflag is ignored.- Type Parameters:
S- the type of the thread-local's value- Parameters:
supplier- the supplier for initial values; must not be nullcopyFunction- a function to copy/transform the parent value for child threads; must not be null- Returns:
- a new
InheritableThreadLocalinstance - Throws:
NullPointerException- if either supplier or copyFunction is null
-
withInitial
public static <S> InheritableThreadLocal<S> withInitial(Supplier<? extends S> supplier, boolean shareParentRef, Function<S, S> copyFunction) Creates a thread-local variable with a supplier, a share flag, and a copy function.The copy function takes precedence over the
shareParentRefflag.- Type Parameters:
S- the type of the thread-local's value- Parameters:
supplier- the supplier for initial values; must not be nullshareParentRef- if true, child threads share the reference when no copy function is providedcopyFunction- a function to copy/transform the parent value for child threads; must not be null- Returns:
- a new
InheritableThreadLocalinstance - Throws:
NullPointerException- if either supplier or copyFunction is null
-
initialValue
Provides the initial value for the current thread by calling the supplier.- Overrides:
initialValuein classThreadLocal<T>- Returns:
- the initial value for this thread
-
childValue
Determines the value to be passed to a child thread.The priority is:
- If the parent value is null, return null.
- If a copy function is defined, use it to produce the child value.
- If the value implements
Cloneable, attempt to clone it. - Otherwise, return the parent reference (shared).
- Overrides:
childValuein classInheritableThreadLocal<T>- Parameters:
parentValue- the value from the parent thread- Returns:
- the value for the child thread
-