The InheritableThreadLocal class inherits from the ThreadLocal class, so it has the characteristics of the ThreadLocal class, but it is also a special ThreadLocal. Its particularity is that the InheritableThreadLocal variable value will be automatically passed to all subthreads, the normal ThreadLocal variable does not work. Can the subthread modify the InheritableThreadLocal variable value and pass it back to the main thread? Let's first look at a group of test code and results:
import java.util.concurrent.TimeUnit;public class TestThreadLocal { private static ThreadLocal<String> stringItl = new InheritableThreadLocal<String>(){ protected String initialValue() { System.out.println(Thread.currentThread().getName() + " initialize stringItl variable."); return "String init"; } }; private static ThreadLocal<String> stringItl2 = new InheritableThreadLocal<String>(){ protected String initialValue() { System.out.println(Thread.currentThread().getName() + " initialize stringItl2 variable."); return "String2 init"; } }; private static ThreadLocal<StringBuffer> stringBufferItl = new InheritableThreadLocal<StringBuffer>(){ protected StringBuffer initialValue() { System.out.println(Thread.currentThread().getName() + " initialize stringBufferItl variable."); return new StringBuffer("StringBuffer init"); } }; private static ThreadLocal<StringBuffer> stringBufferItl2 = new InheritableThreadLocal<StringBuffer>(){ protected StringBuffer initialValue() { System.out.println(Thread.currentThread().getName() + " initialize stringBufferItl2 variable."); return new StringBuffer("StringBuffer2 init"); } }; public static void main(String[] args) throws InterruptedException { stringItl.set("Parent"); stringBufferItl.set(new StringBuffer("ParentBuffer")); System.out.println(Thread.currentThread().getName() + " first get stringItl : " + stringItl.get()); System.out.println(Thread.currentThread().getName() + " first get stringBufferItl : " + stringBufferItl.get().toString()); for(int i=0; i<2; i++){ new Thread(){ public void run(){ System.out.println(Thread.currentThread().getName() + " first get stringItl : " + stringItl.get()); stringItl.set(Thread.currentThread().getName() + "Child"); System.out.println(Thread.currentThread().getName() + " get after set stringItl : " + stringItl.get()); System.out.println(Thread.currentThread().getName() + " first get stringItl2 : " + stringItl2.get()); stringItl2.set(Thread.currentThread().getName() + "Child"); System.out.println(Thread.currentThread().getName() + " get after set stringItl2 : " + stringItl2.get()); System.out.println(Thread.currentThread().getName() + " first get stringBufferItl : " + stringBufferItl.get().toString()); stringBufferItl.get().append(Thread.currentThread().getName()); System.out.println(Thread.currentThread().getName() + " get after set stringBufferItl : " + stringBufferItl.get().toString()); System.out.println(Thread.currentThread().getName() + " first get stringBufferIt2 : " + stringBufferItl2.get().toString()); stringBufferItl2.get().append(Thread.currentThread().getName()); System.out.println(Thread.currentThread().getName() + " get after set stringBufferItl2 : " + stringBufferItl2.get().toString()); } }.start(); } for(int i=0; i<2; i++){ new Thread(){ public void run(){ System.out.println(Thread.currentThread().getName() + " first get stringItl : " + stringItl.get()); stringItl.set(Thread.currentThread().getName() + "Child"); System.out.println(Thread.currentThread().getName() + " get after set stringItl : " + stringItl.get()); System.out.println(Thread.currentThread().getName() + " first get stringItl2 : " + stringItl2.get()); stringItl2.set(Thread.currentThread().getName() + "Child"); System.out.println(Thread.currentThread().getName() + " get after set stringItl2 : " + stringItl2.get()); System.out.println(Thread.currentThread().getName() + " first get stringBufferItl : " + stringBufferItl.get().toString()); stringBufferItl.set(new StringBuffer(Thread.currentThread().getName() + "Buffer")); System.out.println(Thread.currentThread().getName() + " get after set stringBufferItl : " + stringBufferItl.get().toString()); System.out.println(Thread.currentThread().getName() + " first get stringBufferIt2 : " + stringBufferItl2.get().toString()); stringBufferItl2.get().append(Thread.currentThread().getName()); System.out.println(Thread.currentThread().getName() + " get after set stringBufferItl2 : " + stringBufferItl2.get().toString()); } }.start(); } TimeUnit.SECONDS.sleep(2);//let children threads run first System.out.println(Thread.currentThread().getName() + " second get stringItl : " + stringItl.get()); System.out.println(Thread.currentThread().getName() + " first get stringItl2 : " + stringItl2.get()); System.out.println(Thread.currentThread().getName() + " second get stringBufferItl : " + stringBufferItl.get().toString()); System.out.println(Thread.currentThread().getName() + " first get stringBufferItl2 : " + stringBufferItl2.get().toString()); }}
Code running result:
main first get stringItl : Parentmain first get stringBufferItl : ParentBufferThread-0 first get stringItl : ParentThread-0 get after set stringItl : Thread-0ChildThread-0 initialize stringItl2 variable.Thread-0 first get stringItl2 : String2 initThread-0 get after set stringItl2 : Thread-0ChildThread-0 first get stringBufferItl : ParentBufferThread-0 get after set stringBufferItl : ParentBufferThread-0Thread-0 initialize stringBufferItl2 variable.Thread-0 first get stringBufferIt2 : StringBuffer2 initThread-0 get after set stringBufferItl2 : StringBuffer2 initThread-0Thread-1 first get stringItl : ParentThread-1 get after set stringItl : Thread-1ChildThread-1 initialize stringItl2 variable.Thread-1 first get stringItl2 : String2 initThread-1 get after set stringItl2 : Thread-1ChildThread-1 first get stringBufferItl : ParentBufferThread-0Thread-1 get after set stringBufferItl : ParentBufferThread-0Thread-1Thread-1 initialize stringBufferItl2 variable.Thread-1 first get stringBufferIt2 : StringBuffer2 initThread-1 get after set stringBufferItl2 : StringBuffer2 initThread-1Thread-3 first get stringItl : ParentThread-3 get after set stringItl : Thread-3ChildThread-3 initialize stringItl2 variable.Thread-3 first get stringItl2 : String2 initThread-3 get after set stringItl2 : Thread-3ChildThread-3 first get stringBufferItl : ParentBufferThread-0Thread-1Thread-3 get after set stringBufferItl : Thread-3BufferThread-3 initialize stringBufferItl2 variable.Thread-3 first get stringBufferIt2 : StringBuffer2 initThread-3 get after set stringBufferItl2 : StringBuffer2 initThread-3Thread-2 first get stringItl : ParentThread-2 get after set stringItl : Thread-2ChildThread-2 initialize stringItl2 variable.Thread-2 first get stringItl2 : String2 initThread-2 get after set stringItl2 : Thread-2ChildThread-2 first get stringBufferItl : ParentBufferThread-0Thread-1Thread-2 get after set stringBufferItl : Thread-2BufferThread-2 initialize stringBufferItl2 variable.Thread-2 first get stringBufferIt2 : StringBuffer2 initThread-2 get after set stringBufferItl2 : StringBuffer2 initThread-2main second get stringItl : Parentmain initialize stringItl2 variable.main first get stringItl2 : String2 initmain second get stringBufferItl : ParentBufferThread-0Thread-1main initialize stringBufferItl2 variable.main first get stringBufferItl2 : StringBuffer2 init
The running results show that:
If ThreadLocal stores immutable) objects, such as String, the value set by the main thread can be obtained by the get function. However, after the sub-thread calls the set function to set a new value, it has no effect on the main thread and does not affect other sub-threads. It is only visible to you. See the stringItl variable in the code example). If the main thread has not obtained (get) or set the ThreadLocal variable, and the sub-thread first obtains (get) or sets the ThreadLocal variable. Then the value belongs to only the sub-thread, the main thread and other child threads are not visible. Please refer to the stringIt2 variable in the code example ).
If ThreadLocal stores mutable objects, such as StringBuffer, the value set by the main thread can be obtained by the get function. However, after the subthread calls the set function to set a new value, it does not affect the main thread and other sub-threads. It is only visible to itself. However, if the Sub-thread first obtains the get and then modifies the attributes of the object, this modification is visible to the main thread and other sub-threads, that is, they share a reference. Please refer to the stringBufferItl variable in the code example); if the main thread has not obtained (get) or set the ThreadLocal variable, and the sub-thread first obtains (get) or sets the ThreadLocal variable. Then the value belongs to only the sub-thread, the main thread and other child threads are not visible. Please refer to the stringBufferItl2 variable in the code example ).
Therefore, the sub-thread can only modify Mutable) the object is visible to the main thread, that is, the modification can be passed to the main thread, but this is not a good practice and is not recommended, to protect the thread security, it is generally recommended to pass only Immuable) objects, that is, stateless objects.
For details about ThreadLocal, refer to the following two articles:
Http://www.ibm.com/developerworks/cn/java/j-threads/index3.html
Http://geekexplains.blogspot.sg/2009/02/threadlocal-inheritablethreadlocal-in.html
This article is from "the power comes from the sincere love !" Blog, please be sure to keep this source http://stevex.blog.51cto.com/4300375/1289499