Java source code analysis and interview questions-ThreadLocal source code analysis

This series of related blog, Mu class reference column Java source code and system manufacturers interviewer succinctly Zhenti
below this column is GitHub address:
Source resolved: https://github.com/luanqiu/java8
article Demo: HTTPS: // GitHub. com / luanqiu / java8_demo
classmates can look at it if necessary)

The introduction
ThreadLocal provides a way to allow each thread to have its own unique data in a multi-threaded environment, and can be passed from top to bottom during the entire thread execution process.

1 Usage demo

Many students may not have used ThreadLocal. Let's demonstrate the usage of ThreadLocal first. The demo is as follows:

/**
 * ThreadLocal 中保存的数据是 Map
 */
static final ThreadLocal<Map<String, String>> context = new ThreadLocal<>();
 
@Test
public void testThread() {
  // 从上下文中拿出 Map
  Map<String, String> contextMap = context.get();
  if (CollectionUtils.isEmpty(contextMap)) {
    contextMap = Maps.newHashMap();
  }
 
  contextMap.put("key1", "value1");
  context.set(contextMap);
  log.info("key1,value1被放到上下文中");
	// 从上下文中拿出刚才放进去的数据
  getFromComtext();
}
 
private String getFromComtext() {
  String value1 = context.get().get("key1");
  log.info("从 ThreadLocal 中取出上下文,key1 对应的值为:{}", value1);
  return value1;
}
//运行结果:
demo.ninth.ThreadLocalDemo - key1,value1被放到上下文中
demo.ninth.ThreadLocalDemo - 从 ThreadLocal 中取出上下文,key1 对应的值为:value1

As can be seen from the running results, the value corresponding to key1 has been taken from the context.

The getFromComtext method does not accept any input parameters. Through the context.get (). get ("key1") line of code, the value of key1 is obtained from the context. Next, let's take a look at how the underlying ThreadLocal implements the context Passed.

Type 2 structure

2.1 generics

ThreadLocal is generic when defining a class, indicating that ThreadLocal can store data in any format. The source code is as follows:

public class ThreadLocal<T> {}

2.2 Key attributes

ThreadLocal has several key attributes, let's take a look at them one by one:

// threadLocalHashCode 表示当前 ThreadLocal 的 hashCode,用于计算当前 ThreadLocal 在 ThreadLocalMap 中的索引位置
private final int threadLocalHashCode = nextHashCode();
// 计算 ThreadLocal 的 hashCode 值(就是递增)
private static int nextHashCode() {
    return nextHashCode.getAndAdd(HASH_INCREMENT);
}
// static + AtomicInteger 保证了在一台机器中每个 ThreadLocal 的 threadLocalHashCode 是唯一的
// 被 static 修饰非常关键,因为一个线程在处理业务的过程中,ThreadLocalMap 是会被 set 多个 ThreadLocal 的,多个 ThreadLocal 就依靠 threadLocalHashCode 进行区分
private static AtomicInteger nextHashCode = new AtomicInteger();

There is another important attribute: ThreadLocalMap . When a thread has multiple ThreadLocals, a container is needed to manage multiple ThreadLocals. The role of ThreadLocalMap is to manage multiple ThreadLocals in a thread.

2.2.1 ThreadLocalMap

ThreadLocalMap itself is a simple Map structure, key is ThreadLocal, value is the value saved by ThreadLocal, the bottom layer is the data structure of the array, the source code is as follows:

static class ThreadLocalMap {
        // 数组中的每个节点值,WeakReference 是弱引用,当没有引用指向时,会直接被回收
        static class Entry extends WeakReference<ThreadLocal<?>> {
            // 当前 ThreadLocal 关联的值
            Object value;
            // WeakReference 的引用 referent 就是 ThreadLocal
            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }
        // 数组的初始化大小
        private static final int INITIAL_CAPACITY = 16;
        // 存储 ThreadLocal 的数组
        private Entry[] table;
        // 扩容的阈值,默认是数组大小的三分之二
        private int threshold;
}

From the source code, it can be seen that ThreadLocalMap is actually a simple Map structure. The bottom layer is an array, which has an initial size and a capacity expansion threshold. The elements of the array are Entry, the key of Entry is the reference of ThreadLocal, and the value is the value of ThreadLocal.

3 How does ThreadLocal achieve data isolation between threads

ThreadLocal is thread-safe, we can use it with confidence, mainly because ThreadLocalMap is the attribute of the thread, we look at the source code of the thread Thread, as follows:
Insert picture description here
From the above figure, we can see that ThreadLocals.ThreadLocalMap and InheritableThreadLocals.ThreadLocalMap are respectively threaded Attribute, so each thread's ThreadLocals are isolated and exclusive.

When the parent thread creates a child thread, it will copy the value of inheritableThreadLocals, but will not copy the value of threadLocals. The source code is as follows:
Insert picture description here
from the above figure, we can see that when the thread is created, the value of the inheritableThreadLocals attribute of the parent thread will be copy.

4 set method

The main function of the set method is to set the value in the current ThreadLocal. If the generic type of the current ThreadLocal is Map, then it is to set the map in the current ThreadLocal. The source code is as follows:

// set 操作每个线程都是串行的,不会有线程安全的问题
public void set(T value) {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    // 当前 thradLocal 之前有设置值,直接设置,否则初始化
    if (map != null)
        map.set(this, value);
    // 初始化ThreadLocalMap
    else
        createMap(t, value);
}

The code logic is relatively clear. Let's take a look at the source code of ThreadLocalMap.set as follows:

private void set(ThreadLocal<?> key, Object value) {
    Entry[] tab = table;
    int len = tab.length;
    // 计算 key 在数组中的下标,其实就是 ThreadLocal 的 hashCode 和数组大小-1取余
    int i = key.threadLocalHashCode & (len-1);
 
    // 整体策略:查看 i 索引位置有没有值,有值的话,索引位置 + 1,直到找到没有值的位置
    // 这种解决 hash 冲突的策略,也导致了其在 get 时查找策略有所不同,体现在 getEntryAfterMiss 中
    for (Entry e = tab[i];
         e != null;
         // nextIndex 就是让在不超过数组长度的基础上,把数组的索引位置 + 1
         e = tab[i = nextIndex(i, len)]) {
        ThreadLocal<?> k = e.get();
        // 找到内存地址一样的 ThreadLocal,直接替换
        if (k == key) {
            e.value = value;
            return;
        }
        // 当前 key 是 null,说明 ThreadLocal 被清理了,直接替换掉
        if (k == null) {
            replaceStaleEntry(key, value, i);
            return;
        }
    }
    // 当前 i 位置是无值的,可以被当前 thradLocal 使用
    tab[i] = new Entry(key, value);
    int sz = ++size;
    // 当数组大小大于等于扩容阈值(数组大小的三分之二)时,进行扩容
    if (!cleanSomeSlots(i, sz) && sz >= threshold)
        rehash();
}

We pay attention to the following source code:

  1. It is used as the hashCode of ThreadLocal by increasing AtomicInteger;
  2. The formula for calculating the index position of the array is: hashCode takes the size of the modulo array. Since hashCode keeps increasing, so different hashCode will most likely calculate the index position of the same array (but do n’t worry, in actual projects, ThreadLocal are all Very few, basically no conflict);
  3. If the index position i calculated by hashCode already has a value, it will start from i and continue to search backward through +1 until it finds an empty index position, and put the current ThreadLocal as the key.

Fortunately, when using ThreadLocal in daily work, often only 1 ~ 2 ThreadLocals are used, and the probability of calculating duplicate arrays by hash is not very high.

The strategy for solving the conflict of array element positions during set also has an impact on the get method. Then let's take a look at the get method together.

5 get method

The get method is mainly to get the current stored value of ThreadLocal from ThreadLocalMap, the source code is as follows:

public T get() {
    // 因为 threadLocal 属于线程的属性,所以需要先把当前线程拿出来
    Thread t = Thread.currentThread();
    // 从线程中拿到 ThreadLocalMap
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        // 从 map 中拿到 entry,由于 ThreadLocalMap 在 set 时的 hash 冲突的策略不同,导致拿的时候逻辑也不太一样
        ThreadLocalMap.Entry e = map.getEntry(this);
        // 如果不为空,读取当前 ThreadLocal 中保存的值
        if (e != null) {
            @SuppressWarnings("unchecked")
            T result = (T)e.value;
            return result;
        }
    }
    // 否则给当前线程的 ThreadLocal 初始化,并返回初始值 null
    return setInitialValue();
}

Then we look at the getEntry method of ThreadLocalMap, the source code is as follows:

// 得到当前 thradLocal 对应的值,值的类型是由 thradLocal 的泛型决定的
// 由于 thradLocalMap set 时解决数组索引位置冲突的逻辑,导致 thradLocalMap get 时的逻辑也是对应的
// 首先尝试根据 hashcode 取模数组大小-1 = 索引位置 i 寻找,找不到的话,自旋把 i+1,直到找到索引位置不为空为止
private Entry getEntry(ThreadLocal<?> key) {
    // 计算索引位置:ThreadLocal 的 hashCode 取模数组大小-1
    int i = key.threadLocalHashCode & (table.length - 1);
    Entry e = table[i];
    // e 不为空,并且 e 的 ThreadLocal 的内存地址和 key 相同,直接返回,否则就是没有找到,继续通过 getEntryAfterMiss 方法找
    if (e != null && e.get() == key)
        return e;
    else
    // 这个取数据的逻辑,是因为 set 时数组索引位置冲突造成的  
        return getEntryAfterMiss(key, i, e);
}
// 自旋 i+1,直到找到为止
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
    Entry[] tab = table;
    int len = tab.length;
    // 在大量使用不同 key 的 ThreadLocal 时,其实还蛮耗性能的
    while (e != null) {
        ThreadLocal<?> k = e.get();
        // 内存地址一样,表示找到了
        if (k == key)
            return e;
        // 删除没用的 key
        if (k == null)
            expungeStaleEntry(i);
        // 继续使索引位置 + 1
        else
            i = nextIndex(i, len);
        e = tab[i];
    }
    return null;
}

The comments in the get logic source code have been written very clearly, so we will not repeat them.

6 Expansion

When the number of ThreadLocal in ThreadLocalMap exceeds the threshold, ThreadLocalMap will start to expand. Let us look at the logic of expansion:

//扩容
private void resize() {
    // 拿出旧的数组
    Entry[] oldTab = table;
    int oldLen = oldTab.length;
    // 新数组的大小为老数组的两倍
    int newLen = oldLen * 2;
    // 初始化新数组
    Entry[] newTab = new Entry[newLen];
    int count = 0;
    // 老数组的值拷贝到新数组上
    for (int j = 0; j < oldLen; ++j) {
        Entry e = oldTab[j];
        if (e != null) {
            ThreadLocal<?> k = e.get();
            if (k == null) {
                e.value = null; // Help the GC
            } else {
                // 计算 ThreadLocal 在新数组中的位置
                int h = k.threadLocalHashCode & (newLen - 1);
                // 如果索引 h 的位置值不为空,往后+1,直到找到值为空的索引位置
                while (newTab[h] != null)
                    h = nextIndex(h, newLen);
                // 给新数组赋值
                newTab[h] = e;
                count++;
            }
        }
    }
    // 给新数组初始化下次扩容阈值,为数组长度的三分之二
    setThreshold(newLen);
    size = count;
    table = newTab;
}

The source code annotations are also relatively clear, we pay attention to two points:

  1. After expansion, the size of the array is twice that of the original array;
  2. There is absolutely no thread safety problem when expanding the capacity, because ThreadLocalMap is an attribute of the thread, a thread can only operate on the ThreadLocalMap at the same time, because the same thread executing business logic must be serial, then the operation ThreadLocalMap must also be serial .

7 Summary

ThreadLocal is a very important API. We often use it when writing a middleware, such as the transfer of context in the process engine, the transfer of call chain ID, etc., which is very useful, but there are many pits.

Published 40 original articles · won praise 1 · views 5356

Guess you like

Origin blog.csdn.net/aha_jasper/article/details/105609429