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>
TransmittableThreadLocalcan transmit value from the thread of submitting task to the thread of executing task.Note:
TransmittableThreadLocalextendsInheritableThreadLocal, soTransmittableThreadLocalfirst is aInheritableThreadLocal.
If the inheritable ability fromInheritableThreadLocalhas potential leaking problem, you can disable the inheritable ability:❶ by wrapping thread factory using method
TtlExecutors.getDisableInheritableThreadFactory(java.util.concurrent.ThreadFactory)/TtlForkJoinPoolHelper.getDefaultDisableInheritableForkJoinWorkerThreadFactory()for thread pooling components(ThreadPoolExecutor,ForkJoinPool). Inheritable feature in thread pooling components should never happen, because threads in thread pooling components is pre-created and pooled, these threads is neutral for biz logic/data.
You can turn on "disable inheritable for thread pool" byTtlAgentso as to wrap thread factory for thread pooling components (ThreadPoolExecutor,ForkJoinPool) automatically and transparently.❷ or 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<String>() { 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:
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.TransmitterTransmittableThreadLocal.Transmittertransmit allTransmittableThreadLocaland registeredThreadLocal(registered byTransmittableThreadLocal.Transmitter.registerThreadLocal(java.lang.ThreadLocal<T>, com.alibaba.ttl.TtlCopier<T>)) values of the current thread to other thread by static methodsTransmittableThreadLocal.Transmitter.capture()=>TransmittableThreadLocal.Transmitter.replay(Object)=>TransmittableThreadLocal.Transmitter.restore(Object)(akaCRRoperation).
-
Constructor Summary
Constructors Constructor Description TransmittableThreadLocal()Default constructor.TransmittableThreadLocal(boolean disableIgnoreNullValueSemantics)Constructor, create aTransmittableThreadLocalwith parameterdisableIgnoreNullValueSemanticsto control "Ignore-Null-Value Semantics".
-
Method Summary
All 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)-
Methods inherited from class java.lang.InheritableThreadLocal
childValue
-
Methods inherited from class java.lang.ThreadLocal
initialValue, withInitial
-
-
-
-
Constructor Detail
-
TransmittableThreadLocal
public TransmittableThreadLocal()
Default constructor.Create a
TransmittableThreadLocalinstance 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
nullto biz intent. - more safe(avoid
NPE) and gc friendly.
So it's 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 set parameterdisableIgnoreNullValueSemanticsinstead.More info see Issue #157.
- See Also:
TransmittableThreadLocal(boolean)
- If value is
-
TransmittableThreadLocal
public TransmittableThreadLocal(boolean disableIgnoreNullValueSemantics)
Constructor, create aTransmittableThreadLocalwith parameterdisableIgnoreNullValueSemanticsto control "Ignore-Null-Value Semantics".- Parameters:
disableIgnoreNullValueSemantics- disable "Ignore-Null-Value Semantics"- Since:
- 2.11.3
- See Also:
TransmittableThreadLocal()
-
-
Method Detail
-
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>
-
-