Java.lang.InheritableThreadLocal Class with Examples

22 May, 2018
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:
// 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();    }}
Output:
Parent Thread Value:parent data
Child Thread Value:null (by default initialValue is null)

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

Java-Multithreading
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.