Top Description Constructors Methods
java.lang

public Class InheritableThreadLocal<T>

extends ThreadLocal<T>
Class Inheritance
Type Parameters
<T>
the type of the inheritable thread local's value

This class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values. Normally the child's values will be identical to the parent's; however, the child's value can be made an arbitrary function of the parent's by overriding the childValue method in this class.

Inheritable thread-local variables are used in preference to ordinary thread-local variables when the per-thread-attribute being maintained in the variable (e.g., User ID, Transaction ID) must be automatically transmitted to any child threads that are created.

Note

During the creation of a new thread, it is possible to opt out of receiving initial values for inheritable thread-local variables.

Author
Josh Bloch and Doug Lea
Since
1.2
See Also
ThreadLocal, Thread.Builder#inheritInheritableThreadLocals(boolean)

Constructor Summary

AccessConstructor and Description
public
InheritableThreadLocal()

Creates an inheritable thread local variable.

Method Summary

Modifier and TypeMethod and Description
protected T

Returns:

the child thread's initial value
childValue
(T
the parent thread's value
parentValue
)

Overrides java.lang.ThreadLocal.childValue.

Computes the child's initial value for this inheritable thread-local variable as a function of the parent's value at the time the child thread is created.

pack-priv void
createMap(Thread
the current thread
t
,
T
value for the initial entry of the table.
firstValue
)

Overrides java.lang.ThreadLocal.createMap.

Create the map associated with a ThreadLocal.

pack-priv ThreadLocal.ThreadLocalMap
getMap(Thread
the current thread
t
)

Overrides java.lang.ThreadLocal.getMap.

Get the map associated with a ThreadLocal.

Inherited from java.lang.ThreadLocal:
createInheritedMapdumpStackIfVirtualThreadgetgetCarrierThreadLocalinitialValueisCarrierThreadLocalPresentremoveremoveCarrierThreadLocalsetsetCarrierThreadLocalwithInitial

Constructor Detail

InheritableThreadLocalback to summary
public InheritableThreadLocal()

Creates an inheritable thread local variable.

Method Detail

childValueback to summary
protected T childValue(T parentValue)

Overrides java.lang.ThreadLocal.childValue.

Computes the child's initial value for this inheritable thread-local variable as a function of the parent's value at the time the child thread is created. This method is called from within the parent thread before the child is started.

This method merely returns its input argument, and should be overridden if a different behavior is desired.

Parameters
parentValue:T

the parent thread's value

Returns:T

the child thread's initial value

createMapback to summary
pack-priv void createMap(Thread t, T firstValue)

Overrides java.lang.ThreadLocal.createMap.

Create the map associated with a ThreadLocal.

Parameters
t:Thread

the current thread

firstValue:T

value for the initial entry of the table.

Annotations
@Override
getMapback to summary
pack-priv ThreadLocal.ThreadLocalMap getMap(Thread t)

Overrides java.lang.ThreadLocal.getMap.

Get the map associated with a ThreadLocal.

Parameters
t:Thread

the current thread

Returns:ThreadLocal.ThreadLocalMap

Doc from java.lang.ThreadLocal.getMap.

the map

Annotations
@Override