InheritableThreadLocal
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
InheritableThreadLocal allows child threads to inherit values from their parent threads for thread-local variables.
It's primarily used for propagating per-thread attributes like User IDs or Transaction IDs to child threads automatically.
Child values are typically identical to the parent's but can be customized by overriding the childValue method.
This class extends ThreadLocal and provides additional inheritance functionality.
public class
InheritableThreadLocal
extends ThreadLocal<T>
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.
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.
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
Public Constructors
public
InheritableThreadLocal()
Protected Methods
protected
T
childValue(T parentValue)
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[],["`InheritableThreadLocal` extends `ThreadLocal`, enabling child threads to inherit values from parent threads. When a child thread starts, it receives initial values for inheritable thread-local variables from its parent. The `childValue` method, which defaults to returning the parent's value, can be overridden to customize the child's initial value based on the parent's. This mechanism is useful for automatically transmitting per-thread attributes (e.g., User ID) to child threads.\n"]]