Java.lang.InheritableThreadLocal Class with Examples

The java.lang.InheritableThreadLocal 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.

Parent thread, ThreadLocal variable by default is not available to child thread.
Constructor :

InheritableThreadLocal gfg_tl = new InheritableThreadLocal();

It is the child class of ThreadLocal and hence all methods present in ThreadLocal by default available to InheritableThreadLocal.
It contains only one method :
Syntax :



public Object 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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate parent thread, ThreadLocal variable
// by default not available to child thread
  
class ParentThread extends Thread {
    public static ThreadLocal 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());
  
        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());
        /* null (parent thread variable 
        thread local value is not available to child thread ) */
    }
}
class ThreadLocalDemo {
    public static void main(String[] args)
    {
        ParentThread gfg_pt = new ParentThread();
        gfg_pt.start();
    }
}

chevron_right


Output:
Parent Thread Value:parent data
Child Thread Value:null (by default initialValue is null)
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate inheritance of customized value
// from parent thread to child thread
  
class ParentThread extends Thread {
    // anonymous inner class  for overriding childValue method.
    public static InheritableThreadLocal gfg_tl = new InheritableThreadLocal() {
        public Object childValue(Object parentValue)
        {
            return "child data";
        }
    };
    public void run()
    {
        // setting the new value
        gfg_tl.set("parent data");
        // parent data
        System.out.println("Parent Thread Value :" + gfg_tl.get());
  
        ChildThread gfg_ct = new ChildThread();
        gfg_ct.start();
    }
}
class ChildThread extends Thread {
  
    public void run()
    {
        // child data
        System.out.println("Child Thread Value :" + ParentThread.gfg_tl.get());
    }
}
class ThreadLocalDemo {
  
    public static void main(String[] args)
    {
        ParentThread gfg_pt = new ParentThread();
        gfg_pt.start();
    }
}

chevron_right


Output:
Parent Thread Value:parent data 
Child Thread Value:child data

1st Scenario : In the above program if we replace InheritableThreadLocal with ThreadLocal and we are not overriding childValue method then the output is :

Output:
Parent Thread Value: parent data 
Child Thread Value:null   (by default initialValue is null)

2nd Scenario : In the above program if we are maintaining InheritableThreadLocal and we are not overriding childValue method, then the output is :

Output :
Parent Thread Value:parent data 
Child Thread Value:parent data

3rd Scenario : In the above program if we are maintaining InheritableThreadLocal and we are also overriding childValue method, then the output is :

Output:
Parent Thread Value:parent data 
Child Thread Value:child data


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.