Class TransmittableThreadLocal<T>
- java.lang.Object
-
- java.lang.ThreadLocal<T>
-
- java.lang.InheritableThreadLocal<T>
-
- com.alibaba.ttl.TransmittableThreadLocal<T>
-
- All Implemented Interfaces:
TtlCopier<T>
public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> implements TtlCopier<T>
TransmittableThreadLocal(TTL) can transmit the value from the thread of submitting task to the thread of executing task even using thread pooling components.Note:
TransmittableThreadLocalextendsInheritableThreadLocal, soTransmittableThreadLocalfirst is aInheritableThreadLocal.
If the inheritable ability fromInheritableThreadLocalhas potential leaking problem, you can disable the inheritable ability:❶ For thread pooling components(
ThreadPoolExecutor,ForkJoinPool), Inheritable feature should never happen, since threads in thread pooling components is pre-created and pooled, these threads is neutral to biz logic/data.
Disable inheritable for thread pooling components by wrapping thread factories using methodsgetDisableInheritableThreadFactory/getDefaultDisableInheritableForkJoinWorkerThreadFactory.
Or you can turn on "disable inheritable for thread pool" byTtlAgentto wrap thread factories for thread pooling components automatically and transparently.❷ In other cases, disable inheritable by overriding method
InheritableThreadLocal.childValue(Object).
Whether the value should be inheritable or not can be controlled by the data owner, disable it carefully when data owner have a clear idea.TransmittableThreadLocal<String> transmittableThreadLocal = new TransmittableThreadLocal<>() { protected String childValue(String parentValue) { return initialValue(); } }More discussion about "disable the inheritable ability" see issue #100: disable Inheritable when it's not necessary and buggy.
- Since:
- 0.10.0
- Author:
- Jerry Lee (oldratlee at gmail dot com), Yang Fang (snoop dot fy at gmail dot com)
- See Also:
- user guide docs and code repo of TransmittableThreadLocal(TTL),
TtlRunnable,TtlCallable,TtlExecutors,TtlExecutors.getTtlExecutor(java.util.concurrent.Executor),TtlExecutors.getTtlExecutorService(java.util.concurrent.ExecutorService),TtlExecutors.getTtlScheduledExecutorService(java.util.concurrent.ScheduledExecutorService),TtlExecutors.getDefaultDisableInheritableThreadFactory(),TtlExecutors.getDisableInheritableThreadFactory(java.util.concurrent.ThreadFactory),TtlForkJoinPoolHelper,TtlForkJoinPoolHelper.getDefaultDisableInheritableForkJoinWorkerThreadFactory(),TtlForkJoinPoolHelper.getDisableInheritableForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory),TtlAgent
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTransmittableThreadLocal.TransmitterTransmittertransmit allTransmittableThreadLocaland registeredThreadLocalvalues of the current thread to other thread.
-
Constructor Summary
Constructors Constructor Description TransmittableThreadLocal()Default constructor.TransmittableThreadLocal(boolean disableIgnoreNullValueSemantics)Constructor, create aTransmittableThreadLocalinstance with parameterdisableIgnoreNullValueSemanticsto control "Ignore-Null-Value Semantics".
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterExecute()Callback method after task object(TtlRunnable/TtlCallable) execute.protected voidbeforeExecute()Callback method before task object(TtlRunnable/TtlCallable) execute.Tcopy(T parentValue)Computes the value for this transmittable thread-local variable as a function of the source thread's value at the time the task Object is created.Tget()voidremove()voidset(T value)static <S> TransmittableThreadLocal<S>withInitial(Supplier<? extends S> supplier)Creates a transmittable thread local variable.static <S> TransmittableThreadLocal<S>withInitialAndCopier(Supplier<? extends S> supplier, TtlCopier<S> copierForChildValueAndCopy)Creates a transmittable thread local variable.static <S> TransmittableThreadLocal<S>withInitialAndCopier(Supplier<? extends S> supplier, TtlCopier<S> copierForChildValue, TtlCopier<S> copierForCopy)Creates a transmittable thread local variable.-
Methods inherited from class java.lang.InheritableThreadLocal
childValue
-
Methods inherited from class java.lang.ThreadLocal
initialValue
-
-
-
-
Constructor Detail
-
TransmittableThreadLocal
public TransmittableThreadLocal()
Default constructor. Create aTransmittableThreadLocalinstance with "Ignore-Null-Value Semantics".About "Ignore-Null-Value Semantics":
- If value is
null(check byget()method), do NOT transmit thisThreadLocal. - If set
nullvalue, also remove value(invokeremove()method).
This is a pragmatic design decision:
- use explicit value type rather than
nullvalue to express biz intent. - safer and more robust code(avoid
NPErisk).
So it's strongly not recommended to use
nullvalue.But the behavior of "Ignore-Null-Value Semantics" is NOT compatible with
ThreadLocalandInheritableThreadLocal, you can disable this behavior/semantics via using constructorTransmittableThreadLocal(boolean)and setting parameterdisableIgnoreNullValueSemanticstotrue.More discussion about "Ignore-Null-Value Semantics" see Issue #157.
- See Also:
TransmittableThreadLocal(boolean)
- If value is
-
TransmittableThreadLocal
public TransmittableThreadLocal(boolean disableIgnoreNullValueSemantics)
Constructor, create aTransmittableThreadLocalinstance with parameterdisableIgnoreNullValueSemanticsto control "Ignore-Null-Value Semantics".- Parameters:
disableIgnoreNullValueSemantics- disable "Ignore-Null-Value Semantics"- Since:
- 2.11.3
- See Also:
TransmittableThreadLocal()
-
-
Method Detail
-
withInitial
@NonNull public static <S> TransmittableThreadLocal<S> withInitial(@NonNull Supplier<? extends S> supplier)
Creates a transmittable thread local variable. The initial value(ThreadLocal.initialValue()) of the variable is determined by invoking theget()method on theSupplier.- Type Parameters:
S- the type of the thread local's value- Parameters:
supplier- the supplier to be used to determine the initial value- Returns:
- a new transmittable thread local variable
- Throws:
NullPointerException- if the specified supplier is null- Since:
- 2.12.2
- See Also:
withInitialAndCopier(Supplier, TtlCopier)
-
withInitialAndCopier
@NonNull @ParametersAreNonnullByDefault public static <S> TransmittableThreadLocal<S> withInitialAndCopier(Supplier<? extends S> supplier, TtlCopier<S> copierForChildValueAndCopy)
Creates a transmittable thread local variable. The initial value(ThreadLocal.initialValue()) of the variable is determined by invoking theget()method on theSupplier; and the child value(InheritableThreadLocal.childValue(Object)) and the transmitting value(copy(Object)) of the variable is determined by invoking theTtlCopier.copy(Object)method on theTtlCopier.- Type Parameters:
S- the type of the thread local's value- Parameters:
supplier- the supplier to be used to determine the initial valuecopierForChildValueAndCopy- the ttl copier to be used to determine the child value and the transmitting value- Returns:
- a new transmittable thread local variable
- Throws:
NullPointerException- if the specified supplier or copier is null- Since:
- 2.12.3
- See Also:
withInitial(Supplier)
-
withInitialAndCopier
@NonNull @ParametersAreNonnullByDefault public static <S> TransmittableThreadLocal<S> withInitialAndCopier(Supplier<? extends S> supplier, TtlCopier<S> copierForChildValue, TtlCopier<S> copierForCopy)
Creates a transmittable thread local variable. The initial value(ThreadLocal.initialValue()) of the variable is determined by invoking theget()method on theSupplier; and the child value(InheritableThreadLocal.childValue(Object)) and the transmitting value(copy(Object)) of the variable is determined by invoking theTtlCopier.copy(Object)method on theTtlCopier.NOTE:
Recommend usewithInitialAndCopier(Supplier, TtlCopier)instead of this method. In most cases, the logic of determining the child value(InheritableThreadLocal.childValue(Object)) and the transmitting value(copy(Object)) should be the same.- Type Parameters:
S- the type of the thread local's value- Parameters:
supplier- the supplier to be used to determine the initial valuecopierForChildValue- the ttl copier to be used to determine the child valuecopierForCopy- the ttl copier to be used to determine the transmitting value- Returns:
- a new transmittable thread local variable
- Throws:
NullPointerException- if the specified supplier or copier is null- Since:
- 2.12.3
- See Also:
withInitial(Supplier),withInitialAndCopier(Supplier, TtlCopier)
-
copy
public T copy(T parentValue)
Computes the value for this transmittable thread-local variable as a function of the source thread's value at the time the task Object is created.This method is called from
TtlRunnableorTtlCallablewhen it create, before the task is started.This method merely returns reference of its source thread value(the shadow copy), and should be overridden if a different behavior is desired.
- Specified by:
copyin interfaceTtlCopier<T>- Since:
- 1.0.0
- See Also:
TransmittableThreadLocal.Transmitter.registerThreadLocal(ThreadLocal, TtlCopier),TransmittableThreadLocal.Transmitter.registerThreadLocalWithShadowCopier(ThreadLocal),TransmittableThreadLocal.Transmitter.unregisterThreadLocal(java.lang.ThreadLocal<T>)
-
beforeExecute
protected void beforeExecute()
Callback method before task object(TtlRunnable/TtlCallable) execute.Default behavior is to do nothing, and should be overridden if a different behavior is desired.
Do not throw any exception, just ignored.
- Since:
- 1.2.0
-
afterExecute
protected void afterExecute()
Callback method after task object(TtlRunnable/TtlCallable) execute.Default behavior is to do nothing, and should be overridden if a different behavior is desired.
Do not throw any exception, just ignored.
- Since:
- 1.2.0
-
get
public final T get()
- Overrides:
getin classThreadLocal<T>
-
set
public final void set(T value)
- Overrides:
setin classThreadLocal<T>
-
remove
public final void remove()
- Overrides:
removein classThreadLocal<T>
-
-