Java.lang.InheritableThreadLocal Class with Examples
Last Updated :
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:
class ParentThread extends Thread {
public static ThreadLocal gfg_tl = new ThreadLocal();
public void run()
{
gfg_tl.set( "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()
{
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:null (by default initialValue is null)
class ParentThread extends Thread {
public static InheritableThreadLocal gfg_tl = new InheritableThreadLocal() {
public Object childValue(Object parentValue)
{
return "child data" ;
}
};
public void run()
{
gfg_tl.set( "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()
{
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
Share your thoughts in the comments
Please Login to comment...