The use of InheritableThreadLocal and the most simple and easy-to-understand source code analysis

The basic use and source code of ThreadLocal

Knowledge points about ThreadLocal need to read another blog:
ThreadLocal from simple use and source code

Basic use of InheritableThreadLocal

After understanding ThreadLocal, let's look at the following example: You
insert image description here
will find a drawback of ThreadLocal: parent and child threads cannot share data
Then let's modify the example: use InheritableThreadLocal to solve this problem perfectly
insert image description here

Source code analysis:

Here, try to stop and think:
(1) How does it allow the child thread to get the parameters set by the parent thread?
(2) Where is it placed? How to take it out?
You have to look at these two questions to understand better

1: How to put it?

Here we must pay great attention to distinguishing the child thread created by the parent thread, which is the parent and which is the child.
The first step is to tell everyone directly where the parameter value is stored:
insert image description here
here it can be simply understood that the location is different, but we all know ThreadLocal Value is stored in the corresponding Thread, so what is really important depends on how to set these two values ​​​​in the Thread initialization creation part

Let's start with thread initialization:
insert image description here
insert image description here
insert image description here

⚠️ Note that it is very important to initialize the default true here, and this step will be judged in the next step

insert image description here
A little further down, there is another piece of logic:
insert image description here
Judgment: If the inheritableThreadLocals of the current parent thread is set, then through the createInheritedMap method, return a ThreadLocalMap and store it in the child thread class currently being created. Why do you do this? Because in this way, the inheritableThreadLocals value of the parent thread that is creating the child thread can be assigned to the inheritableThreadLocals of the child thread being created. In this round, it is necessary to distinguish who is the current creator and who is being created. Continue to see
createInheritedMap Method:
insert image description here
It is found that this method creates a new ThreadLocalMap and returns it. The source code says that the returned map is associated with the parent thread.

How to get it?

This is relatively simple, inheritableThreadLocals rewrites a method:
insert image description here

What makes it return is not t.threadLocal, but t.inheritableThreadLocals, and this getMap method is precisely to obtain the ThreadLocalMap method of the corresponding thread, further get the Value from the map, and then return, so far the whole show ends
insert image description here
insert image description here

Pit used with thread pool ⚠️

According to the above source code analysis, it is not difficult for us to draw a conclusion: it must be an initialized child thread to inherit the inheritableThreadLocals variable of the parent thread, then if it is a thread pool, because it is the reason for multiplexing threads, there is no init, so naturally there is no The method of reassignment will lead to the inconsistency between the parameters obtained from the child thread and the parameters stuffed into the InheritableThreadLocal by the parent thread

Guess you like

Origin blog.csdn.net/whiteBearClimb/article/details/123063900