Poison

ThreadLocal

Thread 类的源码中,可以看到以下声明:

1
2
3
4
5
6
7
8
public
class Thread implements Runnable {

/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;

}

可以看出,Thread 的实例持有了对 ThreadLocal.ThreadLocalMap 实例 threadLocals 的引用。

ThreadLocal#get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Returns the value in the current thread's copy of this
* thread-local variable. If the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the {@link #initialValue} method.
*
* @return the current thread's value of this thread-local
*/
public T get() {
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;
return result;
}
}
return setInitialValue();
}

/**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @return the map
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}

ThreadLocal 类的 get() 方法中,首先获取到当前线程 t,然后使用 getMap(Thread t) 方法获取到当前线程 t 中持有的 ThreadLocal.ThreadLocalMap 实例 threadLocals,然后再尝试从该 threadLocals 中获取当前 ThreadLocal 实例对应的值,如果 threadLocals 为空或者未获取到 ThreadLocal 实例对应的值,则执行以下的初始化逻辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Variant of set() to establish initialValue. Used instead
* of set() in case user has overridden the set() method.
*
* @return the initial value
*/
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);
return value;
}

protected T initialValue() {
return null;
}

/**
* Create the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @param firstValue value for the initial entry of the map
*/
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}

其中 initialValue() 是留给用户覆写的方法。注意,ThreadLocalMap 中的 keyThreadLocal 实例。

ThreadLocal#set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Sets the current thread's copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the {@link #initialValue}
* method to set the values of thread-locals.
*
* @param value the value to be stored in the current thread's copy of
* this thread-local.
*/
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

ThreadLocal 类的 set(T value) 方法实现与 get() 方法类似,此处不再赘述。

ThreadLocal#remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Removes the current thread's value for this thread-local
* variable. If this thread-local variable is subsequently
* {@linkplain #get read} by the current thread, its value will be
* reinitialized by invoking its {@link #initialValue} method,
* unless its value is {@linkplain #set set} by the current thread
* in the interim. This may result in multiple invocations of the
* {@code initialValue} method in the current thread.
*
* @since 1.5
*/
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}

ThreadLocal 类的 remove() 方法实现也很简单,核心就是获取出当前线程持有的 threadLocals 实例,然后在该实例上操作即可。通过以上三个操作 ThreadLocal 实例的方法,我们知道,其操作都依赖当前线程持有的 ThreadLocal.ThreadLocalMap 实例 threadLocals,且在 threadLocals 中,key 为 ThreadLocal 实例,即对应源码中的 this,value 为用户使用 ThreadLocal 来实际存储的对象的引用。

ThreadLocal.ThreadLocalMap

我们再看看 ThreadLocal 中用到的核心数据结构 ThreadLocalMap,该类作为 ThreadLocal 的静态内部类定义,如果没有看过 HashMap 实现原理的建议先看看 HashMap。回到 ThreadLocalMap,先看看 Entry 的定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 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;
}
}

/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;

/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
*/
private Entry[] table;

可以看出,ThreadLocalMapEntry 数组组成,初始容量为 16,且注释明确要求容量必须为 2 的 n 次幂,原因为当容量为 2 的 n 次幂时,定位元素所在数组下标的计算可以由 取余操作 优化为 与运算 实现,效率更高,关于 2 的 n 次幂带来的弊端可以参考 HashMap,此处不再赘述。仔细看 Entry 类的定义,其继承了 WeakReference 且将 Entrykey 作为弱引用,value 依然是强引用。再看看 ThreadLocalMap 的构造函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 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) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}

/**
* The next size value at which to resize.
*/
private int threshold; // Default to 0

/**
* Set the resize threshold to maintain at worst a 2/3 load factor.
*/
private void setThreshold(int len) {
threshold = len * 2 / 3;
}

可以看出定位 key 所在数组下标采用了 与运算 实现,最后计算并设置了触发扩容的阈值,注意此处使用的负载因子为 2/3,原因可参考:Open Addressing。回到构造函数,其中不得不提的是 firstKey.threadLocalHashCode,我们看一下 ThreadLocal 中关于 threadLocalHashCode 的相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* ThreadLocals rely on per-thread linear-probe hash maps attached
* to each thread (Thread.threadLocals and
* inheritableThreadLocals). The ThreadLocal objects act as keys,
* searched via threadLocalHashCode. This is a custom hash code
* (useful only within ThreadLocalMaps) that eliminates collisions
* in the common case where consecutively constructed ThreadLocals
* are used by the same threads, while remaining well-behaved in
* less common cases.
*/
private final int threadLocalHashCode = nextHashCode();

/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
*/
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.
*/
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}

从注释中我们知道 ThreadLocalMap 采用线性探测处理 hash 冲突,每一个 ThreadLocal 实例的 threadLocalHashCode 都会使用线程安全的静态变量 nextHashCode 加上 0x61c88647 得到,那么这个累加的值是如何得到的呢?首先根据源码可以看出 threadLocalHashCode 字段定义的类型为 int,在 Java 中,基本类型是有符号的,那么可以知道 threadLocalHashCode 可以使用的范围为 [Integer.MIN_VALUE, Integer.MAX_VALUE],这一段范围值的长度为 232 = 4294967296。我们将这段范围进行黄金分割,分割后较长的段长度为:(232) / ((1 + sqrt(5)) / 2) = 2654435769,较短的段长度为 4294967296 - 2654435769 = 1640531527。而 Java 中 Integer.MAX_VALUE 的十进制表示为 2147483647,较长段长度 2654435769 因为大于了 Integer.MAX_VALUE 无法用 int 直接表示,较短的段长度为 1640531527,可以用 int 表示,我们看看较长段长度 2654435769 的二进制表示:

1
2
2654435769:
10011110 00110111 01111001 10111001

可以看出如果把 2654435769 用 int 表示,则转换为十进制为 -1640531527,你会发现,数值部分竟然与较短段长度一致,这也是黄金分割点的神奇之处。将数值部分 1640531527 转换为十六进制表示则为 0x61c88647,与源码中的值一致,猜测这就是作者选择 0x61c88647 作为 HASH_INCREMENT 值的原因。即每个 ThreadLocal 实例的 threadLocalHashCode 相差的值为整个 int 范围段进行黄金分割后较短部分的长度。那么为何要对整数域进行黄金分割并选用上述计算的值作为 HASH_INCREMENT 呢?实际上这是一种特殊的乘法散列,只不过常数使用的 HASH_INCREMENT,也被称作斐波拉契哈希,其能保证在整个整数域上均匀分布,可参考:Fibonacci Hashing。当然,也有另一种说法是说采用的是黄金分割点的另一种变体,参见:What is the meaning of 0x61C88647 constant in ThreadLocal.java - Stack Overflow

在 Kryo 的 ObjectIntMap 类中也可以看到类似的实现,只不过是根据 Long 进行的黄金分割:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/** Returns an index >= 0 and <= {@link #mask} for the specified {@code item}.
* <p>
* The default implementation uses Fibonacci hashing on the item's {@link Object#hashCode()}: the hashcode is multiplied by a
* long constant (2 to the 64th, divided by the golden ratio) then the uppermost bits are shifted into the lowest positions to
* obtain an index in the desired range. Multiplication by a long may be slower than int (eg on GWT) but greatly improves
* rehashing, allowing even very poor hashcodes, such as those that only differ in their upper bits, to be used without high
* collision rates. Fibonacci hashing has increased collision rates when all or most hashcodes are multiples of larger
* Fibonacci numbers (see <a href=
* "https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/">Malte
* Skarupke's blog post</a>).
* <p>
* This method can be overriden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are
* Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality
* hashcodes and don't need Fibonacci hashing: {@code return item.hashCode() & mask;} */
protected int place (K item) {
return (int)(item.hashCode() * 0x9E3779B97F4A7C15L >>> shift);
}

我们再看看 ThreadLocalMapset 方法实现,源码位于 ThreadLocal.java at jdk8-b120:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 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.

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();

if (k == key) {
e.value = value;
return;
}

if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}

tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}

/**
* Increment i modulo len.
*/
private static int nextIndex(int i, int len) {
return ((i + 1 < len) ? i + 1 : 0);
}

可以看出,其在定位 tab 数组的索引时没有像 HashMap 一样引入防御式的哈希函数,因为当前实现的哈希值由以上我们分析的逻辑计算得出,不像 HashMap 受用户编写的 hashCode() 函数的质量影响。所以可以认为当前哈希值计算函数质量良好,此处直接使用了 key.threadLocalHashCode 进行槽位索引计算。对于冲突,采用的是步长为 1 的基于线性探测的解决方案,相比 HashMap 的单独链表法,具有更好的缓存性能。

注意其中的 k == null 判断,此时是在判断 Entry 实例的 key 即弱引用指向的对象是否已经为空,如果为空,说明已经被 GC,此时将调用 replaceStaleEntry 方法将 value 设置至索引 i 对应的槽位:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* 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).
// 以上注释表明为了避免 GC 释放连续的引用导致持续增量的 rehash, 所以我们清理整个 run
int slotToExpunge = staleSlot;
for (int i = prevIndex(staleSlot, len); // 从 staleSlot 左侧的槽开始向左侧扫描
(e = tab[i]) != null; // 当遇到空槽时停止扫描,这与方法注释上的 run 定义匹配,run 为左端空槽与右端空槽之前连续的 Entry 序列
i = prevIndex(i, len))
if (e.get() == null)
slotToExpunge = i; // 当 Entry 实例的 key 指向的 ThreadLocal 实例已经被 GC 时,此时 e.get() 为空,将当前索引 i 赋值给 slotToExpunge

// Find either the key or trailing null slot of run, whichever
// occurs first
// 以上注释表明遇到匹配的 key 或者 run 右侧的空槽时停止扫描
for (int i = nextIndex(staleSlot, len); // 从 staleSlot 右侧的槽开始向右侧扫描
(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.
// 以上注释表明如果我们找到匹配的 key, 那么我们需要将当前 i 指向的 Entry 实例与 staleSlot 指向的实例进行交换以保持哈希表的顺序,整个 run 中废弃的 Entry 实例将被统一使用 expungeStaleEntry 方法移除
if (k == key) { // 找到需要设置的 ThreadLocal 实例
e.value = value; // 将值设置至当前 Entry 实例的 value 引用

tab[i] = tab[staleSlot]; // 将 staleSlot 指向的废弃 Entry 赋值给当前的 tab[i]
tab[staleSlot] = e; // 将 tab[i] 被赋值前的 Entry 实例赋值给 tab[staleSlot]

// 至此 tab[staleSlot] 上的 Entry 为存在 value 值的 Entry, tab[i] 指向废弃的 Entry

// Start expunge at preceding stale entry if it exists
if (slotToExpunge == staleSlot) // 如果 slotToExpunge 与 staleSlot 相等则说明 staleSlot 左侧没有废弃的 Entry 实例,此时需要从 i 指向的 Entry 开始清理,因为我们从 staleSlot 向右扫描至 i 发现了需要设置的 Entry, 且该 Entry 已经被交换至了 staleSlot, 所以此时需要将 i 赋值给 slotToExpunge
slotToExpunge = i;
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
return; // 注意此处,如果我们找到匹配的 key, 执行完上述逻辑后方法从此处返回
}

// 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.
// 如果在之前左侧扫描的过程中没有发现废弃的 Entry, 即 slotToExpunge 与 staleSlot 相等时,当 k 为空时,此时记录下首个废弃的 Entry, 即记录下右侧扫描过程中的首个废弃的 Entry
if (k == null && slotToExpunge == staleSlot)
slotToExpunge = i; // 注意此处 slotToExpunge 被赋值后就不会再满足 slotToExpunge == staleSlot 的条件了,即仅将首个废弃的 Entry 的索引赋值给 slotToExpunge
}

// If key not found, put new entry in stale slot
// 执行至此处说明 staleSlot 右侧连续的 Entry 实例中的 key 均不是需要设置的 key, 此时在 staleSlot 索引上创建新的 Entry
tab[staleSlot].value = null; // 解除强引用,帮助 GC
tab[staleSlot] = new Entry(key, value); // 新建 Entry 实例并设置至 tab[staleSlot]

// If there are any other stale entries in run, expunge them
// 如果 slotToExpunge 不等于 staleSlot, 则说明 staleSlot 左侧存在废弃的 Entry, 此时从 slotToExpunge 开始清理
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}

根据代码中的注释我们知道,该方法除了设置值至对应的 Slot 外,还会清除废弃的 Entry 实例。接着我们看看在上面方法中调用的 cleanSomeSlots(expungeStaleEntry(slotToExpunge), len); 中的两个方法,先看 expungeStaleEntry(int staleSlot) 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 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;

// expunge entry at staleSlot
tab[staleSlot].value = null;
tab[staleSlot] = null;
size--;

// Rehash until we encounter null
Entry e;
int i;
for (i = nextIndex(staleSlot, len);
(e = tab[i]) != null; // 遇到空槽时停下
i = nextIndex(i, len)) {
ThreadLocal<?> k = e.get();
if (k == null) { // 如果 k 指向的 ThreadLocal 实例已经被 GC
e.value = null; // 解除 value 的强引用
tab[i] = null; // 当前槽设置为空
size--; // size 减 1
} else {
int h = k.threadLocalHashCode & (len - 1); // 重新计算槽索引
if (h != i) { // 如果重新计算的槽索引不为 i, 则需要将 tab[i] 存放至 tab[h], 且需要处理 tab[h] 冲突的情况
tab[i] = null; // 因为准备将 tab[i] 存储至 tab[h], 且 tab[i] 已经赋值给 e, 所以此处将 tab[i] 设置为空

// Unlike Knuth 6.4 Algorithm R, we must scan until
// null because multiple entries could have been stale.
while (tab[h] != null) // 当 tab[h] 不为空时,说明冲突,继续向后探测直到 tab[h] 为空
h = nextIndex(h, len);
tab[h] = e; // 将原 tab[i] 赋值给 tab[h]
}
}
}
return i; // 返回空槽的索引
}

可见 expungeStaleEntry(int staleSlot) 方法的实现还是比较简单的,再看看 cleanSomeSlots(int i, int n) 方法的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Heuristically scan some cells looking for stale entries.
* This is invoked when either a new element is added, or
* another stale one has been expunged. It performs a
* logarithmic number of scans, as a balance between no
* scanning (fast but retains garbage) and a number of scans
* proportional to number of elements, that would find all
* garbage but would cause some insertions to take O(n) time.
*
* @param i a position known NOT to hold a stale entry. The
* scan starts at the element after i.
*
* @param n scan control: {@code log2(n)} cells are scanned,
* unless a stale entry is found, in which case
* {@code log2(table.length)-1} additional cells are scanned.
* When called from insertions, this parameter is the number
* of elements, but when from replaceStaleEntry, it is the
* table length. (Note: all this could be changed to be either
* more or less aggressive by weighting n instead of just
* using straight log n. But this version is simple, fast, and
* seems to work well.)
*
* @return true if any stale entries have been removed.
*/
private boolean cleanSomeSlots(int i, int n) {
boolean removed = false;
Entry[] tab = table;
int len = tab.length;
do {
i = nextIndex(i, len);
Entry e = tab[i];
if (e != null && e.get() == null) {
n = len;
removed = true;
i = expungeStaleEntry(i);
}
} while ( (n >>>= 1) != 0);
return removed;
}

该方法比较简单,且注释非常清楚,启发式扫描,在不扫描与元素数量成比例扫描之前取得平衡。此时我们回到 set 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 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.

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();

if (k == key) {
e.value = value;
return;
}

if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}

tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}

可知,如果哈希表中之前不存在相同的 key 且没有废弃的 Entry 实例可以用于存放当前需要设置的 key,那么此时需要创建新的 Entry 实例并设置至 tab[i],设置并更新 size 之后将会调用 cleanSomeSlots 方法清理部分槽位,如果连一个槽位也没有得到清理,那么此时将比较 sz 是否大于等于 threshold,即如果当前 size 已经达到阈值,则调用 rehash() 尝试进行重新哈希:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Re-pack and/or re-size the table. First scan the entire
* table removing stale entries. If this doesn't sufficiently
* shrink the size of the table, double the table size.
*/
private void rehash() {
expungeStaleEntries(); // 清除 tab 数组中所有废弃的 Entry 实例

// Use lower threshold for doubling to avoid hysteresis
if (size >= threshold - threshold / 4)
// 注意此处 size 为清除所有废弃 Entry 实例的个数,此处与 threshold - threshold / 4 进行比较
// 在构造函数中我们知道 threshold = 2/3 * capacity, 那么此时的比较可以简化为 size >= capacity/2
resize();
}

/**
* Double the capacity of the table.
*/
private void resize() {
Entry[] oldTab = table;
int oldLen = oldTab.length;
int newLen = oldLen * 2; // 双倍扩容,保证长度为 2 的 n 次幂
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) {
// 当前的 Entry 实例 e 为废弃的实例,无需迁移至新数组,所以仅解除了强引用以帮助 GC
e.value = null; // Help the GC
} else {
int h = k.threadLocalHashCode & (newLen - 1);
while (newTab[h] != null) // 处理冲突
h = nextIndex(h, newLen);
newTab[h] = e; // 找到空槽位,赋值
count++; // 维护 count
}
}
}

setThreshold(newLen);
size = count;
table = newTab;
}

/**
* Expunge all stale entries in the table.
*/
private void expungeStaleEntries() {
Entry[] tab = table;
int len = tab.length;
for (int j = 0; j < len; j++) {
Entry e = tab[j];
if (e != null && e.get() == null)
expungeStaleEntry(j);
}
}

以上几个方法的实现都比较简单,注释直接写在源码中了,此处不再赘述。

关于该类的使用场景,可以看几个例子:

  • DataSourceTransactionManager.java at v5.3.15 中,会将获取到的 JDBC 连接进行包装并绑定至当前线程,核心代码如下:

    1
    2
    3
    4
    // Bind the connection holder to the thread.
    if (txObject.isNewConnectionHolder()) {
    TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder());
    }

    TransactionSynchronizationManager 的部分源码位于 TransactionSynchronizationManager.java at v5.3.15:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    private static final ThreadLocal<Map<Object, Object>> resources =
    new NamedThreadLocal<>("Transactional resources");

    /**
    * Bind the given resource for the given key to the current thread.
    * @param key the key to bind the value to (usually the resource factory)
    * @param value the value to bind (usually the active resource object)
    * @throws IllegalStateException if there is already a value bound to the thread
    * @see ResourceTransactionManager#getResourceFactory()
    */
    public static void bindResource(Object key, Object value) throws IllegalStateException {
    Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
    Assert.notNull(value, "Value must not be null");
    Map<Object, Object> map = resources.get();
    // set ThreadLocal Map if none found
    if (map == null) {
    map = new HashMap<>();
    resources.set(map);
    }
    Object oldValue = map.put(actualKey, value);
    // Transparently suppress a ResourceHolder that was marked as void...
    if (oldValue instanceof ResourceHolder && ((ResourceHolder) oldValue).isVoid()) {
    oldValue = null;
    }
    if (oldValue != null) {
    throw new IllegalStateException(
    "Already value [" + oldValue + "] for key [" + actualKey + "] bound to thread");
    }
    }
  • StringCoding 中,使用 ThreadLocal 保证了字符串编码器与解码器的线程安全。在 HikariCP 连接池的核心类 ConcurrentBag.java 中也有对 ThreadLocal 的使用。

  • 在 Dubbo 的 ThreadLocalKryoFactory 类中,使用 ThreadLocal 为每个线程创建 Kryo 实例以规避 Kryo 实例被多线程操作的线程安全问题,因为 Kryo 实例不是线程安全的,参见:Thread safety.

Reference

ThreadLocal (Java Platform SE 8 )
Golden ratio - Wikipedia
Why should Java ThreadLocal variables be static - Stack Overflow
Fibonacci Hashing: The Optimization that the World Forgot (or: a Better Alternative to Integer Modulo) | Probably Dance
Why does ThreadLocalMap.Entry extend WeakReference? - Stack Overflow