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."],[[["\u003cp\u003e\u003ccode\u003eInheritableThreadLocal\u003c/code\u003e allows child threads to inherit values from their parent threads for thread-local variables.\u003c/p\u003e\n"],["\u003cp\u003eIt's primarily used for propagating per-thread attributes like User IDs or Transaction IDs to child threads automatically.\u003c/p\u003e\n"],["\u003cp\u003eChild values are typically identical to the parent's but can be customized by overriding the \u003ccode\u003echildValue\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThis class extends \u003ccode\u003eThreadLocal\u003c/code\u003e and provides additional inheritance functionality.\u003c/p\u003e\n"]]],["`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"],null,["# InheritableThreadLocal\n\npublic class **InheritableThreadLocal** extends [ThreadLocal](../../../reference/java/lang/ThreadLocal.html)\\\u003cT\\\u003e \nThis class extends ThreadLocal to provide inheritance of values\nfrom parent thread to child thread: when a child thread is created, the\nchild receives initial values for all inheritable thread-local variables\nfor which the parent has values. Normally the child's values will be\nidentical to the parent's; however, the child's value can be made an\narbitrary function of the parent's by overriding the childValue\nmethod in this class.\n\nInheritable thread-local variables are used in preference to\nordinary thread-local variables when the per-thread-attribute being\nmaintained in the variable (e.g., User ID, Transaction ID) must be\nautomatically transmitted to any child threads that are created. \n\n##### See Also\n\n- [ThreadLocal](../../../reference/java/lang/ThreadLocal.html) \n\n### Public Constructor Summary\n\n|---|---------------------------------------------------------------------------------------------------------------|\n| | [InheritableThreadLocal](../../../reference/java/lang/InheritableThreadLocal.html#InheritableThreadLocal())() |\n\n### Protected Method Summary\n\n|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| T | [childValue](../../../reference/java/lang/InheritableThreadLocal.html#childValue(T))(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. |\n\n### Inherited Method Summary\n\nFrom class [java.lang.ThreadLocal](../../../reference/java/lang/ThreadLocal.html) \n\n|--------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| T | [get](../../../reference/java/lang/ThreadLocal.html#get())() Returns the value in the current thread's copy of this thread-local variable. |\n| T | [initialValue](../../../reference/java/lang/ThreadLocal.html#initialValue())() Returns the current thread's \"initial value\" for this thread-local variable. |\n| void | [remove](../../../reference/java/lang/ThreadLocal.html#remove())() Removes the current thread's value for this thread-local variable. |\n| void | [set](../../../reference/java/lang/ThreadLocal.html#set(T))(T value) Sets the current thread's copy of this thread-local variable to the specified value. |\n| static \\\u003cS\\\u003e [ThreadLocal](../../../reference/java/lang/ThreadLocal.html)\\\u003cS\\\u003e | [withInitial](../../../reference/java/lang/ThreadLocal.html#withInitial(java.util.function.Supplier\u003c?%20extends%20S\u003e))([Supplier](../../../reference/java/util/function/Supplier.html)\\\u003c? extends S\\\u003e supplier) Creates a thread local variable. |\n\nFrom class [java.lang.Object](../../../reference/java/lang/Object.html) \n\n|-------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Object](../../../reference/java/lang/Object.html) | [clone](../../../reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |\n| boolean | [equals](../../../reference/java/lang/Object.html#equals(java.lang.Object))([Object](../../../reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |\n| void | [finalize](../../../reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |\n| final [Class](../../../reference/java/lang/Class.html)\\\u003c?\\\u003e | [getClass](../../../reference/java/lang/Object.html#getClass())() Returns the unique instance of [Class](../../../reference/java/lang/Class.html) that represents this object's class. |\n| int | [hashCode](../../../reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |\n| final void | [notify](../../../reference/java/lang/Object.html#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. |\n| final void | [notifyAll](../../../reference/java/lang/Object.html#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. |\n| [String](../../../reference/java/lang/String.html) | [toString](../../../reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait(long,%20int))(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. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait(long))(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. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |\n\nPublic Constructors\n-------------------\n\n#### public\n**InheritableThreadLocal**\n()\n\n\u003cbr /\u003e\n\nProtected Methods\n-----------------\n\n#### protected T\n**childValue**\n(T parentValue)\n\nComputes the child's initial value for this inheritable thread-local\nvariable as a function of the parent's value at the time the child\nthread is created. This method is called from within the parent\nthread before the child is started.\n\n\nThis method merely returns its input argument, and should be overridden\nif a different behavior is desired. \n\n##### Parameters\n\n| parentValue | the parent thread's value |\n|-------------|---------------------------|\n\n##### Returns\n\n- the child thread's initial value"]]