Class InheritableThreadLocal<T>
java.lang.Object
java.lang.ThreadLocal<T>
java.lang.InheritableThreadLocal<T>
- Type Parameters:
T- the type of the inheritable thread local's value
This class extends
ThreadLocal to provide inheritance of valuesfrom parent thread to child thread: when a child thread is created, thechild receives initial values for all inheritable thread-local variablesfor which the parent has values. Normally the child's values will beidentical to the parent's; however, the child's value can be made anarbitrary function of the parent's by overriding thechildValuemethod in this class.Inheritable thread-local variables are used in preference toordinary thread-local variables when the per-thread-attribute beingmaintained in the variable (e.g., User ID, Transaction ID) must beautomatically transmitted to any child threads that are created.
Note: During the creation of a newthread, it ispossible toopt out of receiving initial values for inheritablethread-local variables.
- Since:
- 1.2
- See Also:
Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprotectedTchildValue(T parentValue) Computes the child's initial value for this inheritable thread-localvariable as a function of the parent's value at the time the childthread is created.Methods declared in class ThreadLocal
get,initialValue,remove,set,withInitial
Constructor Details
InheritableThreadLocal
public InheritableThreadLocal()Creates an inheritable thread local variable.
Method Details
childValue
Computes the child's initial value for this inheritable thread-localvariable as a function of the parent's value at the time the childthread is created. This method is called from within the parentthread before the child is started.This method merely returns its input argument, and should be overriddenif a different behavior is desired.
- Parameters:
parentValue- the parent thread's value- Returns:
- the child thread's initial value