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:
// 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
Recommended Posts:
- URL class in Java with Examples
- JavaFX | Cursor class with examples
- Java AWT | Cursor class with examples
- AbstractSet Class in Java with Examples
- Calendar Class in Java with examples
- StringBuilder Class in Java with Examples
- Date class in Java (With Examples)
- Java.util.DoubleSummaryStatistics class with Examples
- BitSet class methods in Java with Examples | Set 3
- Math class methods in Java with Examples | Set 2
- Java.util.LongSummaryStatistics class with Examples
- ResourceBundle and ListResourceBundle class in Java with Examples
- Java.util.IntSummaryStatistics class with Examples
- util.date class methods in Java with Examples
- Java Guava | binomial(int n, int k) of LongMath Class with Examples
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.



