ThreadLocal
程序员文章站
2022-05-14 18:00:44
...
ThreadLocal
一、总结
1.版本 jdk 1.8.0_131 java SE
2.ThreadLocal
- 应用场景:在不同的线程存储不同的上下文信息的场合中
- 实现原理:ThreadLocal只是提供一个thread-local变量,这个变量于当前线程所独有, 每一个线程都有一个隶属与当前线程的thread-local变量
3.ThreadLocal.ThreadLocalMap
- Thread类中持有ThreadLocalMap的对象引用
- ThreadLocalMap 的数据结构是数组 Entry[]
- 数组初始化大小 16,扩展 *2 ,扩展因子:length * 2 / 3
- Entry 继承 WeakReference ,包含 key 与 value 两个属性
- key:不是ThreadLocal本身,而是ThreadLocal的弱引用;value:存入ThreadLocal中的泛型T
- ThreadLocalMap提供了一种为ThreadLocal定制的高效实现,并且自带一种基于弱引用的垃圾清理机制
二、源码及分析
/** * This class provides thread-local variables. These variables differ from * their normal counterparts in that each thread that accesses one (via its * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized * copy of the variable. <tt>ThreadLocal</tt> instances are typically private * static fields in classes that wish to associate state with a thread (e.g., * a user ID or Transaction ID). * * <p>For example, the class below generates unique identifiers local to each * thread. * A thread's id is * assigned the first time it invokes <tt>UniqueThreadIdGenerator.getCurrentThreadId()</tt> and remains unchanged on subsequent calls. * <pre> * import java.util.concurrent.atomic.AtomicInteger; * * public class UniqueThreadIdGenerator { * * private static final AtomicInteger uniqueId = new AtomicInteger(0); * * private static final ThreadLocal < Integer > uniqueNum = * new ThreadLocal < Integer > () { * @Override protected Integer initialValue() { * return uniqueId.getAndIncrement(); * } * }; * * public static int getCurrentThreadId() { * return uniqueId.get(); * } * } // UniqueThreadIdGenerator * </pre> * <p>Each thread holds an implicit reference to its copy of a thread-local * variable as long as the thread is alive and the <tt>ThreadLocal</tt> * instance is accessible; after a thread goes away, all of its copies of * thread-local instances are subject to garbage collection (unless other * references to these copies exist). * * @author Josh Bloch and Doug Lea * @version 1.42, 06/23/06 * @since 1.2 */ public class ThreadLocal<T> {
从类的注释中得知:
1.ThreadLocal 类提供了线程本地变量;与普通的变量不同,隶属于每个线程,每个线程可以独立初始化此变量的副本
2.在使用时通常定义为全局的变量 private static ThreadLocal<T> ,通常用来标识每个线程中某属性的一种状态
3.每个线程在首次调用时分配一个本地唯一的标识ID
4.在线程的生命周期内都会拥有一个对本地变量的引用;线程运行结束,所引用的变量会被GC回收
/** * The difference between successively generated hash codes - turns * implicit sequential thread-local IDs into near-optimally spread * multiplicative hash values for power-of-two-sized tables. */ private static final int HASH_INCREMENT = 0x61c88647;
1.两个连续生成的 hash codes 的差值。为了让 hash code 能更好地分布在尺寸为 2 的 N 次方的数组里。
2.源码中使用此常量的调用位置
// 在 ThreadLocal 内部的静态类 ThreadLocalMap 中 // 定位当前 hashCode 在数组中的下标时需计算当前线程的 hashCode 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); } // 计算当前线程的 hashCode private final int threadLocalHashCode = nextHashCode(); /** * The next hash code to be given out. Updated atomically. Starts at * zero. */ // 原子性操作,线程安全,初始化值为0 private static AtomicInteger nextHashCode = new AtomicInteger(); /** * The difference between successively generated hash codes - turns * implicit sequential thread-local IDs into near-optimally spread * multiplicative hash values for power-of-two-sized tables. */ private static final int HASH_INCREMENT = 0x61c88647; /** * Returns the next hash code. */ // 当前的hashCode值加上HASH_INCREMENT private static int nextHashCode() { return nextHashCode.getAndAdd(HASH_INCREMENT); }
1.计算数据在数组中的位置,
通常 index % size ,即通过当前数的数值与数组的大小取余
此处 index & (size - 1 )
原因:数组的 INITIAL_CAPACITY初始化大小为 16 ,即 大小为 2 ^ N 次幂,2 ^ N - 1 ,二进制中 N -1 位均为 1 ,即取 index 的低位中的 N-1 位
举例:3 % 16 = 3 <==>
0011 & 1111 = 0011 = 3
位运算比取模效率高很多。
因为对2^n取模,转为二进制,只要不是第n+1位,对结果的贡献显然都是0,会影响结果的只能是第n+1位。
2^1 ==> 0010 ;
2^2 ==> 0100 ;
举例:二进制取模运算的大致步骤
11010110110000(13744)%10011(19)=111(7)
将除数向右移位与被除数位数相同,若被除数大于除数,相减;直到被除数不大于除数;
将除数向右以为与被除数位数相同,若被除数小于除数,则向右少移动一位;重复上述步骤
(1)
11010110110000
10011000000000
--------------
00111110110000
(2)
111110110000
100110000000
------------
011000110000
(3)
11000110000
10011000000
-----------
00101110000
(4)
101110000
100110000
---------
001000000
(5)
1000000
0100110
-------
0011010
(6)
11010
10011
-----
00111
一般的二进制取余运算过程中需要进行移位运算、除数与被除数的大小判断等。
1011100(192)%10000(16)=1100(12)
(1)
1011100
1000000
-------
0011100
(2)
11100
10000
-----
01100
如果 a > 2^N 需要进行至少两次运算
总结:
对于a与2^N取余的运算,等于a&(2^N-1)
2.为何选用 0x61c88647 作为每次生成 hashCode 的间距
// 目的:查看一串连续的数字存放在大小为16的数组中,是否均匀分布,是否会发生碰撞 public static void main(String[] args) { method(); } private static void method(){ final int HASH_INCREMENT = 0x61c88647 ; for(int i = 0 ; i < 16 ; i++){ int nextHash = i * HASH_INCREMENT + HASH_INCREMENT ; System.out.print(nextHash & (16 - 1)); System.out.print(" "); } } // 输出结果如下: // 7 14 5 12 3 10 1 8 15 6 13 4 11 2 9 0
1.从输出结果看无重复数值出现:
以此值作为 Map 的KEY 可以减少存入时数据时出现的 HASH 碰撞。
2.
黄金比例: (Math.sqrt(5) - 1))
这个数的选取与斐波那契散列有关,0x61c88647对应的十进制为1640531527。斐波那契散列的乘数可以用(long) ((1L << 31) * (Math.sqrt(5) - 1))可以得到2654435769,如果把这个值给转为带符号的int,则会得到-1640531527。换句话说
(1L << 32) - (long) ((1L << 31) * (Math.sqrt(5) - 1))得到的结果就是1640531527也就是0x61c88647
3.AtomicInteger
// 构造方法 /** * Creates a thread local variable. */ public ThreadLocal() { }
// 初始化 threadLocal 变量的值 /** * Returns the current thread's "initial value" for this * thread-local variable. This method will be invoked the first * time a thread accesses the variable with the {@link #get} * method, unless the thread previously invoked the {@link #set} * method, in which case the <tt>initialValue</tt> method will not * be invoked for the thread. Normally, this method is invoked at * most once per thread, but it may be invoked again in case of * subsequent invocations of {@link #remove} followed by {@link #get}. * * <p>This implementation simply returns <tt>null</tt>; if the * programmer desires thread-local variables to have an initial * value other than <tt>null</tt>, <tt>ThreadLocal</tt> must be * subclassed, and this method overridden. Typically, an * anonymous inner class will be used. * * @return the initial value for this thread-local */ protected T initialValue() { return null; }
从方法的注释中得出:
1.返回隶属于当前线程的 thread-local 变量的初始化的值
2.此方法会通过调用get方法来触发;除非在调用get前,调用了set赋值,则不会触发
3.通常每个线程只会执行一次此方法,但是当线程调用remove方法后再次调用get 方法时可能再次执行此方法
4.如果想将变量的初始化赋值为非NULL的值,则需要新增类来继承ThreadLocal,在子类中覆盖此方法来实现赋值;通常,可以通过建立匿名内部类的方式来实现此功能;
/** * ThreadLocalMap is a customized hash map suitable only for * maintaining thread local values. No operations are exported * outside of the ThreadLocal class. The class is package private to * allow declaration of fields in class Thread. To help deal with * very large and long-lived usages, the hash table entries use * WeakReferences for keys. However, since reference queues are not * used, stale entries are guaranteed to be removed only when * the table starts running out of space. */ static class ThreadLocalMap { }
1.ThreadLocalMap 的作用仅仅是用来保存线程本地变量的值,是一个自适应的 hash map
2.ThreadLocalMap 的所有操作均不会对 ThreadLocal 之外的类提供
3.ThreadLocalMap 可以在Thread类中包装私有的属性
// 在 Thread 类中含有ThreadLocalMap的声明的对象实例 /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null;
4.ThreadLocalMap 使用弱引用作为KEY,来解决很大且长时间存活的方法;然而,即使队列的引用已经不被使用了,过期的 entries 仅当table空间不够时才会被移除
/** * The entries in this hash map extend WeakReference, using * its main ref field as the key (which is always a * ThreadLocal object). Note that null keys (i.e. entry.get() * == null) mean that the key is no longer referenced, so the * entry can be expunged from table. Such entries are referred to * as "stale entries" in the code that follows. */ static class Entry extends WeakReference<ThreadLocal> { /** The value associated with this ThreadLocal. */ Object value; Entry(ThreadLocal k, Object v) { super(k); value = v; } }
1.ThreadLocalMap 中定义的静态内部类 Entry 继承 WeekReference
弱引用,使用ThreadLocal 的对象的引用作为 key
2.如果key为null说明此时的key不再被引用,此时这个Entry 可以被从表中删除
/** * The initial capacity -- MUST be a power of two. */ // ThreadLocalMap 中的 Entry[] 的初始化容量是16 private static final int INITIAL_CAPACITY = 16; /** * The table, resized as necessary. * table.length MUST always be a power of two. */ // 数组的长度必须是2的整数次幂 private Entry[] table; /** * The number of entries in the table. */ // Entry[] 中 Entry 的数量,而不是数组的长度 private int size = 0; /** * The next size value at which to resize. */ // 类似 HashMap 中的负载因子,达到此临界值即扩容,而不是达到数组的最大长度 private int threshold; // Default to 0 /** * Set the resize threshold to maintain at worst a 2/3 load factor. */ // 扩容的临界值 len * 2 / 3 private void setThreshold(int len) { threshold = len * 2 / 3; }
/** * Increment i modulo len. */ private static int nextIndex(int i, int len) { return ((i + 1 < len) ? i + 1 : 0); } /** * Decrement i modulo len. */ private static int prevIndex(int i, int len) { return ((i - 1 >= 0) ? i - 1 : len - 1); }
Entry[] 是一个环状的结构
1.求当前 i 的下一个位置
如果 i+1 小于 len ,返回 i+1
如果 i+1 等于 len ,返回 0,即
假设 len = 16 ,数组中的位置依次是0~15,当前i=15,15+1=16=len,即回到了环的起始位置,那15的下一个位置就是0
2.求当前 i 的上一个位置
如果 i-1 大于等于 0 ,返回 i - 1
如果 i-1 小于 0 ,返回 len - 1,即
假设 len = 16 ,数组中的位置依次是0~15,当前i=0,0-11<0<len,即回到了环的结尾位置上,结尾的位置就是len-1,=16-1=15
/** * Construct a new map initially containing (firstKey, firstValue). * ThreadLocalMaps are constructed lazily, so we only create * one when we have at least one entry to put in it. */ ThreadLocalMap(ThreadLocal firstKey, Object firstValue) { // 实例化数组,初始化大小16 table = new Entry[INITIAL_CAPACITY]; // 获取当前ThreadLocal 的 hashCode 并与 2^N -1 做与操作 // 目的:计算在数组中的存放位置,等价于 hashcode % length // 但运算效率 & 操作要优于 % int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1); // 构造匿名Entry 并放入数组得下标为i的位置上 table[i] = new Entry(firstKey, firstValue); // 初始化数组中entry 的数量为 1 size = 1; // 扩容的临界值值为 16*2/3 = 10 setThreshold(INITIAL_CAPACITY); }
1.初始化构造一个新的 ThreadLocalMap 包含第一组Entry<key,value>
2.ThreadLocalMap 懒加载构造器,只有当有至少一个entry需要存放的时候才会去构造
/** * Construct a new map including all Inheritable ThreadLocals * from given parent map. Called only by createInheritedMap. * * @param parentMap the map associated with parent thread. */ private ThreadLocalMap(ThreadLocalMap parentMap) { // 获取给定的parentMap中的数组 Entry[] parentTable = parentMap.table; // 获取数组的容量 int len = parentTable.length; // 设置扩容的临界值 setThreshold(len); // 创建新的数组 table = new Entry[len]; // 遍历原有数组,将数据放入新的数组中 for (int j = 0; j < len; j++) { Entry e = parentTable[j]; if (e != null) { ThreadLocal key = e.get(); if (key != null) { // 此处获取的是子类中定义的value Object value = key.childValue(e.value); Entry c = new Entry(key, value); int h = key.threadLocalHashCode & (len - 1); // 寻址,若当前位置不为空,则继续寻找相邻位置是否有元素 while (table[h] != null) h = nextIndex(h, len); table[h] = c; size++; } } } }
1.创建一个包含所有所有从给定的 parentMap 中继承的 TreadLocal 的新的Map
2.仅仅当需要创建继承关系的Map时调用
/** * Get the entry associated with key. This method * itself handles only the fast path: a direct hit of existing * key. It otherwise relays to getEntryAfterMiss. This is * designed to maximize performance for direct hits, in part * by making this method readily inlinable. * * @param key the thread local object * @return the entry associated with key, or null if no such */ private Entry getEntry(ThreadLocal key) { int i = key.threadLocalHashCode & (table.length - 1); Entry e = table[i]; if (e != null && e.get() == key) return e; else return getEntryAfterMiss(key, i, e); }
1.通过 key 检索绑定的 Entry
2.方法本身值处理快速路径匹配,即直接命中匹配的key
3.否则会关联到 getEntryAfterMiss 方法上,这样设计的目的加大直接命中key 的可能,在某种程度上此方法为一个快速的内联方法
-- 解释:数据尽可能均匀的分布到大小为 2^N 的数组中,但如果出现 hash 碰撞,即 i = hash & (2^N -1) 处已经有值了,那么继续向下寻址,判断当前是否有值,若有,继续向下查询,直到此时i的数据为null,将新增的数据放到 i 的位置上
-- 查询时同样,如果 i 位置就是所需要的KEY,那么直接返回,如果不是,从 i 位置向后检索
/** * Version of getEntry method for use when key is not found in * its direct hash slot. * * @param key the thread local object * @param i the table index for key's hash code * @param e the entry at table[i] * @return the entry associated with key, or null if no such */ private Entry getEntryAfterMiss(ThreadLocal key, int i, Entry e) { Entry[] tab = table; int len = tab.length; while (e != null) { // e 非空,获取 Entry 的对应的 KEY // 此处再次判断 当前的 k 是否与查询的 k 相等 // 目的是,当 i 不断后移时,用每次从 i 位置上取到的值与 k 比较 // 而不是重复判断 ThreadLocal k = e.get(); if (k == key) return e; if (k == null) // 如果 k == null 在删除 expungeStaleEntry(i); else // i 想后移动一位 i = nextIndex(i, len); e = tab[i]; } return null; }
1.此方法的设计目的是在 getEntry 时,如果未能在 hash 区域内直接命中 key 时的补充
2.从 i 位置开始,向后移动,获取每处 i 对应的值,如果值不为空,那么进行比较,如果为空,那么清除
/** * Expunge a stale entry by rehashing any possibly colliding entries * lying between staleSlot and the next null slot. This also expunges * any other stale entries encountered before the trailing null. See * Knuth, Section 6.4 * * @param staleSlot index of slot known to have null key * @return the index of the next null slot after staleSlot * (all between staleSlot and this slot will have been checked * for expunging). */ private int expungeStaleEntry(int staleSlot) { Entry[] tab = table; int len = tab.length; // 删除 entry[] 数组中 staleSlot 位置的元素 // expunge entry at staleSlot tab[staleSlot].value = null; tab[staleSlot] = null; size--; // Rehash until we encounter null Entry e; int i; // 遍历 i 之后的位置的元素 for (i = nextIndex(staleSlot, len); (e = tab[i]) != null; i = nextIndex(i, len)) { ThreadLocal k = e.get(); // 如果 k 为 null,则设置数组 i 位置为 null if (k == null) { e.value = null; tab[i] = null; size--; } else { // 重新计算 当前 key 的 hash值,通过新的 hash 值,计算在数组中的存放位置 int h = k.threadLocalHashCode & (len - 1); // 如果位置一样,说明,此时 key 就应该放在 i 处 // 如果位置不一样,说明当存放 key 时,发生了 hash 碰撞 // 导致 key 不能存放到制定的 h 位置上 // 将 i 位置设置为 null // 判断 h 处是否有数据,无则放入 h 位置 // 有则从 h 位置向后寻址 if (h != i) { tab[i] = null; // Unlike Knuth 6.4 Algorithm R, we must scan until // null because multiple entries could have been stale. while (tab[h] != null) h = nextIndex(h, len); tab[h] = e; } } } return i; }
1.通过再次hash可能存在碰撞可能的 entry 数组 ,删除位于 staleSlot 与下一个可能为NULL的元素之间的所有
2.删除在遇到 null 元素之前的已经不再使用的引用
/** * Set the value associated with key. * * @param key the thread local object * @param value the value to be set */ private void set(ThreadLocal key, Object value) { // We don't use a fast path as with get() because it is at // least as common to use set() to create new entries as // it is to replace existing ones, in which case, a fast // path would fail more often than not. // 此处和get()方法一样不使用快速路径 // 因为快速路径至少共同的去调用调用set去创建 entry 同时去替换存在的对象,在这种情况下,更容易出现错误 ????????? Entry[] tab = table; int len = tab.length; int i = key.threadLocalHashCode & (len-1); for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) { ThreadLocal k = e.get(); // 如果找到了匹配的key,则直接将value替换为最新的 if (k == key) { e.value = value; return; } // 如果 key 为 null if (k == null) { replaceStaleEntry(key, value, i); return; } } tab[i] = new Entry(key, value); int sz = ++size; if (!cleanSomeSlots(i, sz) && sz >= threshold) rehash(); }
1.存入关联的 key 与 value
/** * Replace a stale entry encountered during a set operation * with an entry for the specified key. The value passed in * the value parameter is stored in the entry, whether or not * an entry already exists for the specified key. * * As a side effect, this method expunges all stale entries in the * "run" containing the stale entry. (A run is a sequence of entries * between two null slots.) * * @param key the key * @param value the value to be associated with key * @param staleSlot index of the first stale entry encountered while * searching for key. */ private void replaceStaleEntry(ThreadLocal key, Object value, int staleSlot) { Entry[] tab = table; int len = tab.length; Entry e; // Back up to check for prior stale entry in current run. // We clean out whole runs at a time to avoid continual // incremental rehashing due to garbage collector freeing // up refs in bunches (i.e., whenever the collector runs). int slotToExpunge = staleSlot; for (int i = prevIndex(staleSlot, len); (e = tab[i]) != null; i = prevIndex(i, len)) if (e.get() == null) slotToExpunge = i; // Find either the key or trailing null slot of run, whichever // occurs first for (int i = nextIndex(staleSlot, len); (e = tab[i]) != null; i = nextIndex(i, len)) { ThreadLocal k = e.get(); // If we find key, then we need to swap it // with the stale entry to maintain hash table order. // The newly stale slot, or any other stale slot // encountered above it, can then be sent to expungeStaleEntry // to remove or rehash all of the other entries in run. if (k == key) { e.value = value; tab[i] = tab[staleSlot]; tab[staleSlot] = e; // Start expunge at preceding stale entry if it exists if (slotToExpunge == staleSlot) slotToExpunge = i; cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); return; } // If we didn't find stale entry on backward scan, the // first stale entry seen while scanning for key is the // first still present in the run. if (k == null && slotToExpunge == staleSlot) slotToExpunge = i; } // If key not found, put new entry in stale slot tab[staleSlot].value = null; tab[staleSlot] = new Entry(key, value); // If there are any other stale entries in run, expunge them if (slotToExpunge != staleSlot) cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); }
1.通过一个特殊的key值在一系列的操作中替换掉一个已经过时的引用
2.无论这个特殊的key值是否在Map中存在,传入的value都会存入到 Entry 中
3.此方法将会删除两个null值之间所有的无效的引用
博文参考:
ThreadLocal 和神奇的 0x61c88647
ThreadLocal源码解读