Inheritablethreadlocal for Java multithreading programming
Function of inheritablethreadlocal: when we need to use the value in the parent thread in the child thread, we can use inheritablethreadlocal like ThreadLocal.
First, let's take a look at the JDK source code of inheritablethreadlocal:
This code is the complete source code of inheritablethreadlocal (long comments are deleted).
First, we can see that it inherits the ThreadLocal class, and then provides:
Protected t childvalue (t parentvalue) {} method, which is the key of inheritablethreadlocal. It provides this method to return the value in the parent thread. If you need to add a value on the parent thread, you can override the childvalue method.
Run output:
Main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 main thread median: 1508210392057 in thread a: 1508210392057 Modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a In thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a: 1508210392057 modify the inheritance value in thread a
Is there a question why a child thread can get the data of the parent thread?
As like as two peas, we can see that InheritableThreadLocal rewrote the getMap method and the createMap method. In the last section, when we talked about ThreadLocal, we knew that the value of ThreadLocal was stored in a variable called ThreadLocals, but now we return a InheritableThreadLocals, which is exactly the same as ThreadLocals, but the name is changed. So why on earth do you use ThreadLocal in a new thread Can the get () method still get values?
When we look at the childvalue method, we can guess that we may have done some tricks and things worth passing when the thread is created.
When we open the source code of thread class, we can find:
ThreadLocal. ThreadLocalMap inheritableThreadLocals = null;
Therefore, when we create a child thread, there will be an inheritablethreadlocal variable, which is the same as that of threadlocals. Look down:
The focus is on the following code:
Continue:
With this code, we first get the value of the parent thread (that is, the thread currently executing), and then use the for loop to put the values in the parent thread into our newly created value one by one.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.