Class TransmittableThreadLocal<T>
- java.lang.Object
-
- java.lang.ThreadLocal<T>
-
- java.lang.InheritableThreadLocal<T>
-
- com.alibaba.ttl.TransmittableThreadLocal<T>
-
public class TransmittableThreadLocal<T> extends InheritableThreadLocal<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.getDefaultDisableInheritableThreadFactory(),TtlExecutors.getDisableInheritableThreadFactory(java.util.concurrent.ThreadFactory),TtlForkJoinPoolHelper.getDefaultDisableInheritableForkJoinWorkerThreadFactory(),TtlForkJoinPoolHelper.getDisableInheritableForkJoinWorkerThreadFactory(java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTransmittableThreadLocal.TransmitterTransmittableThreadLocal.Transmittertransmit allTransmittableThreadLocalvalues of 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()
-
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.protected 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
-
-
-
-
Method Detail
-
copy
protected 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 fromTtlRunnableorTtlCallablewhen it create, before the task is started.This method merely returns reference of its source thread value, and should be overridden if a different behavior is desired.
- Since:
- 1.0.0
-
beforeExecute
protected void beforeExecute()
Callback method before task object(TtlRunnable/TtlCallable) execute.Default behavior is 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 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>
-
-