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:
TransmittableThreadLocal extends InheritableThreadLocal, so TransmittableThreadLocal first is a InheritableThreadLocal.
If the inheritable ability from InheritableThreadLocal has 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 methods getDisableInheritableThreadFactory / getDefaultDisableInheritableForkJoinWorkerThreadFactory.
Or you can turn on "disable inheritable for thread pool" by TtlAgent to 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: