preface

ThreadLocal has been used a lot in recent projects, so take a closer look at this class for future review.

ThreadLocals and inheritableThreadLocals

This class is attached to Thread, so let’s first discuss two variables in the Thread class source code below

  ThreadLocal.ThreadLocalMap threadLocals = null;  
  ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
Copy the code

#inheritableThreadLocals

We’ll look at what inheritableThreadLocals does. Since inheritableThreadLocals is initialized when the thread is created, we’ll look at the init method created by the thread

if(parent.inheritableThreadLocals ! = null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);Copy the code

As shown above, when each thread is initialized, the thread copies the inheritableThreadLocals data from the parent thread of the current thread to the inheritableThreadLocals data of its own thread. Therefore, data can be passed to child threads by storing it in inheritableThreadLocals. Parent and child threads do not affect operations on inheritableThreadLocals.

threadLocals

Let’s look at threadLocals which is a thread local variable store in Java that will be initialized on the first operation (set/get) as follows

public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if(map ! = null) map.set(this, value);elsecreateMap(t, value); } //get initializes an object if no map is found and puts a null value public Tget() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if(map ! = null) { ThreadLocalMap.Entry e = map.getEntry(this);if(e ! = null) { @SuppressWarnings("unchecked")
                T result = (T)e.value;
                returnresult; }}return setInitialValue();
}
private T setInitialValue() {
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if(map ! = null) map.set(this, value);else
            createMap(t, value);
        returnvalue; Void createMap(Thread t, t firstValue) {t.htreadlocals = new ThreadLocalMap(this, firstValue); } // Create an entry array where the entry key can only be a reference to the ThreadLocal class instance and the value is the set value. ThreadLocalMap(ThreadLocal<? > firstKey, Object firstValue) { table = new Entry[INITIAL_CAPACITY]; int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1); Table [I] = new Entry(firstKey, firstValue); size = 1;setThreshold(INITIAL_CAPACITY); // Calculate expansion threshold}Copy the code

ThreadLocals is a map of thread variables that is attached to the current thread, not accessed by other threads, and is unique to the current thread.

conclusion

ThreadLocals stores thread variables unique to the current thread and accessible only to the current thread. InheritableThreadLocals stores thread variables that can be inherited, and the parent thread variables and child thread variables are also independent of each other after the thread is generated

reference

Blog.csdn.net/ni357103403…