1. Class definition
public class ThreadLocal<T>
Copy the code
You can see this in the class definition
- ThreadLocal is a generic class
2. Field attributes
// These field attributes are used in the inner class ThreadLocalMap
private final int threadLocalHashCode = nextHashCode();
private static AtomicInteger nextHashCode = new AtomicInteger();
private static final int HASH_INCREMENT = 0x61c88647;
Copy the code
3. Construction method
public ThreadLocal(a) {}Copy the code
Method 4.
The get method
// Get a copy of the current thread in ThreadLocal
public T get(a) {
// Get the current thread
Thread t = Thread.currentThread();
// Get the ThreadLocalMap object in the current thread with getMap
ThreadLocalMap map = getMap(t);
if(map ! =null) {
// If ThreadLocalMap is not null
// Get the Entry object in ThreadLocalMap
Threadlocalmap. Entry is a key-value object
// Key is a ThreadLocal object and value is a copy saved by the current thread
ThreadLocalMap.Entry e = map.getEntry(this);
if(e ! =null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
returnresult; }}// If ThreadLocalMap object in the current thread is null
// Call the setInitialValue method to set the initialization value. Adds a copy to the current thread
return setInitialValue();
}
Copy the code
SetInitialValue method
// Set the initial value
private T setInitialValue(a) {
// Get the initial value
T value = initialValue();
// Get the current thread
Thread t = Thread.currentThread();
// Get the ThreadLocalMap object of the current thread
ThreadLocalMap map = getMap(t);
if(map ! =null)
// If ThreadLocalMap of the current thread is not null
// Set the value directly
map.set(this, value);
else
// If ThreadLocalMap of the current thread is null
// Create the ThreadLocalMap object and set the value
createMap(t, value);
return value;
}
Copy the code
Set method
/ / set the value
// Essentially sets the value and the current ThreadLocal object into the current thread's ThreadLocalMap
public void set(T value) {
// Get the current thread
Thread t = Thread.currentThread();
// Get the ThreadLocalMap object of the current thread
ThreadLocalMap map = getMap(t);
if(map ! =null)
// If ThreadLocalMap of the current thread is not null
// Set the value directly
map.set(this, value);
else
// If ThreadLocalMap of the current thread is null
// Create the ThreadLocalMap object and set the value
createMap(t, value);
}
Copy the code
The remove method
// Remove the current thread's ThreadLocal
public void remove(a) {
// Get the ThreadLocalMap object of the current thread
ThreadLocalMap m = getMap(Thread.currentThread());
if(m ! =null)
// Remove the current ThreadLocal
m.remove(this);
}
Copy the code
GetMap method
// Get the ThreadLocalMap object for the specified thread
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
Copy the code
CreateMap method
// Create a new ThreadLocalMap object for the specified thread
// Add the current ThreadLocal object and firstValue
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
Copy the code
5. Summary
The essence of a ThreadLocal is a ThreadLocalMap object that operates on the current thread. The Entry in a ThreadLocalMap object stores the current ThreadLocal object as a key and the copy value of the current thread as a value.
The values in ThreadLocal are actually stored in Thread objects, and ThreadLocal simply acts as a proxy object for Thread to manipulate the values in Threa.
ThreadLocalMap is essentially a Map object, similar to a HashMap, but we won’t cover it here, if you’re interested