Stay organized with collections
Save and categorize content based on your preferences.
InheritableThreadLocal
open class InheritableThreadLocal<T : Any!> : 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.
Note: During the creation of a new thread
, it is possible to opt out of receiving initial values for inheritable thread-local variables.
Summary
Protected methods |
open T |
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 functions |
From class ThreadLocal
T? |
get()
Returns the value in the current thread's copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue method.
|
T? |
initialValue()
Returns the current thread's "initial value" for this thread-local variable. This method will be invoked the first time a thread accesses the variable with the get method, unless the thread previously invoked the set method, in which case the initialValue method will not be invoked for the thread. Normally, this method is invoked at most once per thread, but it may be invoked again in case of subsequent invocations of remove followed by get .
This implementation simply returns null ; if the programmer desires thread-local variables to have an initial value other than null , ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used.
|
Unit |
remove()
Removes the current thread's value for this thread-local variable. If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking its initialValue method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current thread.
|
Unit |
set(value: T)
Sets the current thread's copy of this thread-local variable to the specified value. Most subclasses will have no need to override this method, relying solely on the initialValue method to set the values of thread-locals.
|
ThreadLocal<S> |
withInitial(supplier: Supplier<out S>)
Creates a thread local variable. The initial value of the variable is determined by invoking the get method on the Supplier .
|
|
Public constructors
InheritableThreadLocal
InheritableThreadLocal()
Protected methods
childValue
protected open fun childValue(parentValue: T): T
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 |
Return |
T |
the child thread's initial value |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2022-02-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 2022-02-10 UTC."],[],[]]