Open In App

InheritableThreadLocal Class in Java

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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();
    }
}

Output
Parent 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();
    }
}

Output
Parent 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


Article Tags :
Practice Tags :

Similar Reads