The ThreadLocal data is stored in one thread and does not require lock synchronization. Theoretically ThreadLocal can be
So that a variable has a copy on each thread. ThreadLocal is commonly used to mask private variables of a thread, such as "and
or other resources. Also, it is used to maintain counters, statistics, or ID generation for each thread
Manager
See the Get method definitions commonly used by threadlocal:
Public T get () {
Thread T = Thread.CurrentThread ();
Threadlocalmap map = getmap (t);
if (map! = null) {
Threadlocalmap.entry E = Map.getentry (this);
if (E! = null) {
&N Bsp @SuppressWarnings ("unchecked")
T result = (t) e.value;
return result;
}
}
return setinitialvalue ();
}
Threadlocalmap Getmap (Thread t) {
return t.threadlocals;
}
You can know that each thread has a threadlocal.threadlocalmap threadlocals inside, and the maintenance of this map is performed by Threadlocal. And it's initialized to be lazily.
The generation of the current nexthashcode for each threadlocal , and the generation of its index within the THREADLOCALMAP internal array, can be seen in the source code.
Java's threadlocal