ThreadLocal 一般被用作变量在单个线程中的副本。每个线程都持有同一个变量的副本,并且可以对这个副本进行修改而不影响其他线程对此变量的使用。
我们使用 ThreadLocal 时,一般都是采用匿名内部类的形式,通过覆盖 initValue() 方法进行副本的初始化。在使用过程中可以对副本进行删改查操作,分别对应 remove()、set()、get() 方法。
在介绍 ThreadLocal 的关键方法前,我们先了解一下它底层的关键实现–ThreadLocalMap,它虽然名字上和集合框架中的 Map 很相似,实现细节上却有很大差别,传统的 map 出现哈希碰撞时采用链地址法,而 ThreadLocalMap 采用的是开放地址法(每个 ThreadLocal 对象都有一个全局唯一的哈希码,且根据哈希码计算出的槽位只是作为参考,实际将元素放置的槽位为距离理论槽位(含)最近的一个空槽位),因此它的底层结构是一个数组,而非数组+链表,集合框架中默认的负载因子为 0.75,而 ThreadLocalMap 的负载因子为 2/3(实际触发扩容时,折算出的负载因子值为 0.5)。
哈希碰撞常见的解决方法有:链地址法(拉链法),开放地址法,rehash 法。
1. ThreadLocalMap
1.1 属性及构造
// 构成 ThreadLocalMap 的元素节点,它继承了弱引用,
// 将 ThreadLocal 类型的 k 作为弱引用,
// 采用这种类型的引用提升了内存的利用率。
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.
*/
// 底层的数据结构--数组,长度总是 2 的整数次幂
private Entry[] table;
/**
* The number of entries in the table.
*/
// table 中的 entry 数量
private int size = 0;
/**
* The next size value at which to resize.
*/
// 触发扩容的阈值
private int threshold; // Default to 0
/**
* 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 初始容量设为默认值 16
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
// 根据初始容量设置阈值,负载因子为 2/3,与集合框架中的不同
setThreshold(INITIAL_CAPACITY);
}
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
/**
* 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) {
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) {
@SuppressWarnings("unchecked")
ThreadLocal<Object> key = (ThreadLocal<Object>) e.get();
if (key != null) {
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.2 getEntry()
private Entry getEntry(ThreadLocal<?> key) {
int i = key.threadLocalHashCode & (table.length - 1);
Entry e = table[i];
// 如果首节点即为要查找的 entry,直接返回
if (e != null && e.get() == key)
return e;
// 否则,遍历数组进行查找
else
return getEntryAfterMiss(key, i, e);
}
getEntryAfterMiss()
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
Entry[] tab = table;
int len = tab.length;
while (e != null) {
ThreadLocal<?> k = e.get();
if (k == key)
return e;
// 如果 key 为空,说明槽位上的 entry 已过期,进行清除操作
if (k == null)
expungeStaleEntry(i);
else
i = nextIndex(i, len);
e = tab[i];
}
return null;
}
expungeStaleEntry()
// 清除 staleSlot 槽位的元素,同时向后遍历直至遇到最近的一个空槽位为止,
// 在遍历期间遇到过期的槽位将其清理。
// 返回的是距离 staleSlot 最近的一个空槽位的索引。
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();
// 如果 key为空,直接清除
if (k == null) {
e.value = null;
tab[i] = null;
size--;
}
// key 不为空,rehash
else {
// key 的哈希码对数组的最大索引值取模,得到理论的槽位
int h = k.threadLocalHashCode & (len - 1);
// 重新计算出来的槽位与当前值不一致,
// 说明需要进行数据迁移。
if (h != i) {
// 将当前槽位上的元素清空
tab[i] = null;
// Unlike Knuth 6.4 Algorithm R, we must scan until
// null because multiple entries could have been stale.
// 将原当前槽位上的 entry 放到距离新计算出的槽位最近的一个空槽位上
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
// 返回距离最近的空槽位
return i;
}
1.3 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;
// 先根据哈希码计算出 entry 的理论槽位
int i = key.threadLocalHashCode & (len-1);
// 从 i 处开始依次遍历数组,找到 key 所在的位置并且覆盖。
// 如果在找到 key 所在的位置之前,遇到了过期的槽位(Stale Entry),
// 直接覆盖,不管与 key 对应的 entry 是否已存在。
// 过期的槽位指的是:entry 不为空,但 entry 中的 key 为空。
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;
}
}
// 如果在遇到空的槽位之前没有匹配上 key 对应的 entry 或者空的 entry,
// 会将 set() 操作的 entry 放置在空的槽位上。
tab[i] = new Entry(key, value);
int sz = ++size;
// 放置之后,清除 i 槽位之后的过期的 entry,
// 并判断是否需要扩容(rehash)
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
replaceStaleEntry()
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
// 从前向后遍历数组,直到遇到第一个空槽位为止,
// 如果在此之前遇到了匹配上的 entry,
// 将 staleSlot 槽位上旧的 entry 覆盖到当前槽位(后面会统一进行清除),
// 并将要 set 的最新的 entry 覆盖到 staleSlot 槽位。
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;
// expungeStaleEntry() 方法返回的是
// 在 slotToExpunge 槽位之后距离最近的一个空槽位的索引值
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
// 如果遍历结束仍未找到 key 对应的 entry,
// 直接构造新的 entry 覆盖到 staleSlot 槽位
tab[staleSlot].value = null;
tab[staleSlot] = new Entry(key, value);
// If there are any other stale entries in run, expunge them
// 如果除了 staleSlot 槽位,还有其他过期的槽位,进行清除操作
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}
cleanSomeSlots()
// 此方法会从 i 槽位之后的位置开始,清除过期的 entry,
// 如果循环过程中有清除的操作,返回真
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;
}
rehash()
private void rehash() {
// 先清除过期的 entry 来收缩数组的尺寸,
// 收缩后再判断是否需要扩容
expungeStaleEntries();
// Use lower threshold for doubling to avoid hysteresis
// 负载因子默认为 2/3,触发实际扩容的条件下,
// 负载因子采用的是 2/3 * 3/4 = 1/2。
// 上面的注释告诉我们,使用低的阈值触发扩容是为了避免滞后性。
// 滞后性指的是什么?
if (size >= threshold - threshold / 4)
resize();
}
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);
}
}
/**
* Double the capacity of the table.
*/
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 {
// 根据哈希码计算出 entry 的理论槽位 h,
// 实际是将 entry 放置到距离 h 最近(含)的一个空槽位
int h = k.threadLocalHashCode & (newLen - 1);
while (newTab[h] != null)
h = nextIndex(h, newLen);
newTab[h] = e;
count++;
}
}
}
// 重置阈值、元素数量、数组
setThreshold(newLen);
size = count;
table = newTab;
}
1.4 remove()
private void remove(ThreadLocal<?> key) {
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)]) {
if (e.get() == key) {
// 清除引用关系
e.clear();
// 清除从 i 槽位到下一个空槽位之间的过期的 entry
expungeStaleEntry(i);
return;
}
}
}
2. threadLocalHashCode
/**
* 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.
*/
// 每次创建 ThreadLocal 对象时,都会生成一个全局唯一的不可变的哈希码
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);
}
3. initValue()
// 默认的初始值为 null,此方法一般被子类覆盖
protected T initialValue() {
return null;
}
4. get()
public T get() {
Thread t = Thread.currentThread();
// ThreadLocalMap 对象被当前线程持有
ThreadLocalMap map = getMap(t);
// 如果已经初始化,从 ThreadLocalMap 查找当前
// ThreadLocal 对象对应的 Entry
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
// 如果还未初始化,进行初始化
return setInitialValue();
}
getMap()
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
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);
return value;
}
// 初始化 ThreadLocalMap
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
5. set()
// setInitialValue() 方法逻辑一样
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
6. remove()
public void remove() {
// 得到与当前线程绑定的 ThreadLocalMap,进行删除操作
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}
7. 总结
- 在使用 ThreadLocal 记录线程本地变量时,本地变量的集合会被绑定到当前线程中(Thread.threadLocals);
- 被绑定在线程中的这些本地变量,放置在 ThreadLocalMap 容器中,key 的类型为 ThreadLocal<?>,value 为泛型类型的值;
- 在向 ThreadLocalMap 插入元素时,会根据 ThreadLocal<?> 类型的 key 的哈希码计算理论槽位,实际将元素放置的槽位为距离理论槽位(含)最近的一个空槽位;
- ThreadLocalMap 底层为数组结构,不存在哈希碰撞。