Class SuppliedInheritableThreadLocal<T>

java.lang.Object
java.lang.ThreadLocal<T>
java.lang.InheritableThreadLocal<T>
org.daiitech.naftah.builtin.utils.concurrent.SuppliedInheritableThreadLocal<T>
Type Parameters:
T - the type of value stored in the thread-local variable

public final class SuppliedInheritableThreadLocal<T> extends InheritableThreadLocal<T>
An extension of InheritableThreadLocal that allows:
  • Specifying a Supplier to provide the initial value.
  • Optionally defining a Function to 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, the clone method 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 Details

    • supplier

      private final Supplier<? extends T> supplier
    • shareParentRef

      private final boolean shareParentRef
    • copyFunction

      private Function<T,T> copyFunction
  • Constructor Details

    • SuppliedInheritableThreadLocal

      private SuppliedInheritableThreadLocal(Supplier<? extends T> supplier, boolean shareParentRef)
      Constructs a SuppliedInheritableThreadLocal with a supplier for the initial value.

      The initial value for each thread is obtained by calling Supplier.get(). The shareParentRef flag 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 null
      shareParentRef - 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 a SuppliedInheritableThreadLocal with a supplier and a copy function to control the value passed to child threads.

      The copyFunction is used to produce the child thread's value from the parent thread's value. This takes precedence over the shareParentRef flag.

      Parameters:
      supplier - the supplier for initial values; must not be null
      shareParentRef - ignored if copyFunction is provided; if no copy function, controls reference * sharing
      copyFunction - 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

      public static <S> InheritableThreadLocal<S> withInitial(Supplier<? extends S> supplier)
      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 InheritableThreadLocal instance
      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 null
      shareParentRef - if true, child threads inherit the parent's value by reference; otherwise a new value * from the supplier
      Returns:
      a new InheritableThreadLocal instance
      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 shareParentRef flag is ignored.

      Type Parameters:
      S - the type of the thread-local's value
      Parameters:
      supplier - the supplier for initial values; must not be null
      copyFunction - a function to copy/transform the parent value for child threads; must not be null
      Returns:
      a new InheritableThreadLocal instance
      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 shareParentRef flag.

      Type Parameters:
      S - the type of the thread-local's value
      Parameters:
      supplier - the supplier for initial values; must not be null
      shareParentRef - if true, child threads share the reference when no copy function is provided
      copyFunction - a function to copy/transform the parent value for child threads; must not be null
      Returns:
      a new InheritableThreadLocal instance
      Throws:
      NullPointerException - if either supplier or copyFunction is null
    • initialValue

      protected T initialValue()
      Provides the initial value for the current thread by calling the supplier.
      Overrides:
      initialValue in class ThreadLocal<T>
      Returns:
      the initial value for this thread
    • childValue

      protected T childValue(T parentValue)
      Determines the value to be passed to a child thread.

      The priority is:

      1. If the parent value is null, return null.
      2. If a copy function is defined, use it to produce the child value.
      3. If the value implements Cloneable, attempt to clone it.
      4. Otherwise, return the parent reference (shared).
      Overrides:
      childValue in class InheritableThreadLocal<T>
      Parameters:
      parentValue - the value from the parent thread
      Returns:
      the value for the child thread