1. get() and set() methods
-
get()
-
set()
Here we can see that both methods use the getMap method to obtain the ThreadLocalMap object. Therefore, we can speculate that this method is responsible for InheritableThreadLocal being able to access values from the parent thread.
2. getMap() method
Next, let's compare the getMap() method in InheritableThreadLocal and ThreadLocal.
-
InheritableThreadLocal
-
ThreadLocal
We can see that one obtains inheritableThreadLocals, while the other obtains threadLocals. The inheritableThreadLocals will be inherited by the child thread, allowing the child thread to access the threadLocal values of the parent thread (acting as a shared variable between threads).