This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

An overview of the

Each Thread maintains a ThreadLocalMap whose key is the ThreadLocal instance itself and whose value is the original value Object.

  • Each ThreadLocal thread has an internal map (ThreadLocalMap)
  • The Map stores the ThradLocal object (Key) and the thread’s variable copy (Value).
  • The Map inside the Thread is maintained by the ThreadLocal, which gets and sets the Thread’s variable values from the Map.
  • For different threads, each time they obtain the copy value, other threads cannot obtain the copy value of the current site, forming the isolation of copies and mutual interference.

ThreadLocal inner methods and inner classes

Implementation of internal methods

Method statement describe
ThreadLocal() Create a ThreadLocal
public void set() Sets the variable bound to the current thread
public T get() Gets the variable bound to the current thread
public void remove() Removes a variable bound to the current thread

ThreadLocal provides four methods, which are then analyzed one by one.

1.set()

/** * Sets the value of ThreadLocal for the current thread **@parameterValue Specifies the value to be stored in the current thread's corresponding ThreadLocal */
public void set(T value) {
    // Get the current thread
    Thread t = Thread.currentThread();
    // Get the ThradLocalMap object maintained in the current thread object
    ThreadLocalMap map = getMap(t);
    if(map ! =null)
        //set sets the entry for this entity, with the current ThreadLocal as the key
        map.set(this, value);
    else
        //1. The current thread has no ThreadLocalMap object
        //2. Call createMap to initialize the ThreadLocalMap object
        //3. Store the current thread and value as the first entry in ThreadLocalMap
        createMap(t, value);
}

/** * Set ThreadLocalMap for the current thread@parameterT Current thread */
ThreadLocalMap getMap(Thread t) {
    return t.threadLocals;
}

/** * create the current Thread's corresponding ThreadLocalMap * this is where the threadLocals maintained by Thread are initialized **@param t the current thread
 * @paramFirstValue Stores the value of the first entry in the map */
void createMap(Thread t, T firstValue) {
    t.threadLocals = new ThreadLocalMap(this, firstValue);
}
Copy the code

// Each Thread maintains an instance of ThreadLocalMap

Set () executes the process

  1. First get the current program and get a map based on the current thread
  2. If the obtained Map is not empty, the parameter is set to the Map. (ThreadLocal as key)
  3. If the map is empty, the ThreadLocalMap maintained in the Thread is initialized by creating a map for the Thread and setting an initial value

2.get()

/ * * * returns the current thread holds the ThreadLocal as the key of the corresponding value * if you don't have the key, by calling the SuppliedThreadLocal. The initialValue () to initialize * /
public T get(a) {
    // Get the current thread
    Thread t = Thread.currentThread();
    // Get the ThradLocalMap maintained by the current thread
    ThreadLocalMap map = getMap(t);
    if(map ! =null) {
        // Call getEntry to get the storage entity with the current ThradLocal as the key
        ThreadLocalMap.Entry e = map.getEntry(this);
        if(e ! =null) {
            @SuppressWarnings("unchecked")
            //get value
            T result = (T)e.value;
            returnresult; }}// The map does not exist or the map does exist but has no entry associated with the current ThreadLocal
    // Initialize the map
    return setInitialValue();
}
/** * Initialize ThreadLocalMap */
private T setInitialValue(a) {
    // Gets the initialized value, which can be overridden by a subclass, but returns null if not overridden
    T value = initialValue();
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if(map ! =null)
        map.set(this, value);
    else
        createMap(t, value);
    return value;
}
Copy the code

Get () executes the process

  1. Gets the ThreadLocalMap of the current thread
  2. map ! = null? Get the initialValue for Entry: initialValue() in the map, and then initialize the map
  3. If map.getentry (this) is not null, return value