InheritableThreadLocal Class in Java
Last Updated :
14 May, 2025
The java.lang.InheritableThreadLocal class extends ThreadLocal to provide inheritance of values from the parent thread to the child thread. When a child thread is created, the child receives the initial values for all inheritable thread-local variables that the parent thread has set. This is different from the default behavior of ThreadLocal, where thread-local variables are not inherited by child threads.
What is InheritableThreadLocal
InheritableThreadLocal is a subclass of ThreadLocal that allows a child thread to inherit the values of thread-local variables set by its parent thread. This is very useful when we want child threads to have access to contextual information or configuration initialized in the parent thread.
Constructor
The constructor of this class is mentioned below:
InheritableThreadLocal<T> gfg_tl = new InheritableThreadLocal<>();
Note: It is the child class of ThreadLocal and hence all methods present in ThreadLocal by default available to InheritableThreadLocal.
Method
It contains only one method which is listed below:
childValue(Object parentValue):
This method is called(overridden) within the parent thread before the child thread is started. If we want to make parent thread, thread local variable value available to the child thread, then we should go for InheritableThreadLocal class.By default, child thread value is exactly the same as parent thread value. But we can provide our own customized value for child thread by overriding childValue method.
Syntax:
The syntax of childValue(Object parentValue) is listed below:
public Object childValue(Object parentValue)
When to Use InheritableThreadLocalClass?
If we want a parent thread's ThreadLocal variable value to be available to its child threads then we can use the InheritableThreadLocal class. By default, the child thread gets the same value as the parent. But if we want the child to get a different value, we just need to override the childValue() method to set the custom value.
Let's now see an example for better understanding.
Example 1: Demonstrating the default behavior of ThreadLocal (Using ThreadLocal with no Inheritance)
Java
// Java program to demonstrates ThreadLocal variable
// by default is not available to child thread
class ParentThread extends Thread {
public static ThreadLocal<String> gfg_tl = new ThreadLocal<>();
public void run() {
// setting the new value
gfg_tl.set("parent data");
// returns the ThreadLocal value associated with current thread
System.out.println("Parent Thread Value: " + gfg_tl.get());
// Creating child thread
ChildThread gfg_ct = new ChildThread();
gfg_ct.start();
}
}
class ChildThread extends Thread {
public void run() {
// returns the ThreadLocal value associated with current thread
System.out.println("Child Thread Value: " + ParentThread.gfg_tl.get());
}
}
class Geeks {
public static void main(String[] args) {
ParentThread p = new ParentThread();
p.start();
}
}
OutputParent Thread Value: parent data
Child Thread Value: null
Note: ThreadLocal variable is not available to child thread.
Now, we are going to discuss how InheritableThreadLocal works when we override the childValue method to give the child thread a custom value.
Example 2: InheritableThreadLocal with Custom Inherited Value
Java
// Java program to demonstrates inheritance of customized
// value from parent thread to child thread
class ParentThread extends Thread {
// anonymous inner class for overriding childValue method
public static InheritableThreadLocal<String> gfg_tl
= new InheritableThreadLocal<String>() {
public String childValue(String parentValue) {
return "child data";
}
};
public void run() {
// setting the new value
gfg_tl.set("parent data");
// prints the parent data
System.out.println("Parent Thread Value: " + gfg_tl.get());
// Creating child thread
ChildThread gfg_ct = new ChildThread();
gfg_ct.start();
}
}
class ChildThread extends Thread {
public void run() {
// prints the child data
System.out.println("Child Thread Value: "
+ ParentThread.gfg_tl.get());
}
}
class Geeks {
public static void main(String[] args) {
ParentThread g = new ParentThread();
g.start();
}
}
OutputParent Thread Value: parent data
Child Thread Value: child data
Let's now discuss some scenario based examples for more understanding.
Scenario-Based Output Explanation
1. ThreadLocal without overriding childValue:
In the above program, if we replace InheritableThreadLocal with ThreadLocal and we are not overriding childValue method then the output will be:
Parent Thread Value: parent data
Child Thread Value: null (by default initialValue is null)
2. InheritableThreadLocal without overriding childValue:
If we are maintaining InheritableThreadLocal and we are not overriding childValue method, then the output will be:
Parent Thread Value: parent data
Child Thread Value: parent data
3. InheritableThreadLocal with overriding childValue:
If we are maintaining InheritableThreadLocal and we are also overriding childValue method, then the output will be:
Parent Thread Value: parent data
Child Thread Value: child data
Similar Reads
Java.lang.InheritableThreadLocal Class with Examples
The java.lang.InheritableThreadLocal class extends ThreadLocal to provide inheritance of values from the parent thread to the child thread. When a child thread is created, the child receives the initial values for all inheritable thread-local variables that the parent thread has set. This is differe
4 min read
Java.io.InputStream Class in Java
Java InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents an input stream of bytes. Applications that are defining a subclass of the Java InputStream class must provide a method, that returns the next byte of input. A reset() method is i
3 min read
Interfaces and Inheritance in Java
A class can extend another class and can implement one and more than one Java interface. Also, this topic has a major influence on the concept of Java and Multiple Inheritance. Example:Java// A class can implement multiple interfaces import java.io.*; interface intfA { void m1(); } interface intfB {
7 min read
Local Inner Class in Java
Prerequisites: Nested Classes in Java Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block the
5 min read
Inner Class in Java
In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is object-oriented so bringing it closer to the real world. Now geeks you must be wondering why they were introduced? There are certai
11 min read
Java.lang.ThreadLocal Class in Java
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. Basically, it is another way to achieve thread safety apart from writing im
4 min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Random vs ThreadLocalRandom Classes in Java
The Random Class of the java.util package is used for generating a stream of pseudorandom numbers. It uses a 48-bit seed, which is amended by implementing a Linear Congruential Formula. The general form of a Linear Congruential Formula is an+1 = k * an + c (mod m) where a0 is the seed, a1, a2, ... a
5 min read
ThreadLocalRandom vs SecureRandom Class in Java
ThreadLocalRandom class of java.util package is a random number generator that generates random numbers isolated to the current thread. It is a subclass of the Random class. It is initialized with an internally generated seed value that cannot be modified. The class view is as follows: --> java.u
4 min read
Java Class vs Interfaces
In Java, the difference between a class and an interface is syntactically similar; both contain methods and variables, but they are different in many aspects. The main difference is, A class defines the state of behaviour of objects.An interface defines the methods that a class must implement.Class
5 min read