InheritableThreadLocal
Stay organized with collections
Save and categorize content based on your preferences.
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.
Public Constructor Summary
Protected Method Summary
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.
|
Inherited Method Summary
From class
java.lang.ThreadLocal
T
|
get()
Returns the value in the current thread's copy of this
thread-local variable.
|
T
|
initialValue()
Returns the current thread's "initial value" for this
thread-local variable.
|
void
|
remove()
Removes the current thread's value for this thread-local
variable.
|
void
|
set(T value)
Sets the current thread's copy of this thread-local variable
to the specified value.
|
static
<S>
ThreadLocal<S>
|
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
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.
Parameters
parentValue |
the parent thread's value |
Returns
- the child thread's initial value
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["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` 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."]]],["`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"]]