ThreadLocal解析

什么是ThreadLocal

1.ThreadLocal为线程提供了独有的局部变量,每个线程私有。在AndroidHandler消息机制中就使用到了ThreadLocal,而平时开发过程中,使用的场景并不是很多,主要可以实现:存储线程的信息如id,线程特有的变量信息等(线程私有避免了并发不安全问题)。源码中的定义:

1
2
3
4
5
6
7
8
/**
* This class provides thread-local variables. These variables differ from
* their normal counterparts in that each thread that accesses one (via its
* {@code get} or {@code set} method) has its own, independently initialized
* copy of the variable. {@code ThreadLocal} instances are typically private
* static fields in classes that wish to associate state with a thread (e.g.,
* a user ID or Transaction ID).
*/

2.ThreadLocal泛型添加,可以保存任意的对象:

1
2
3
public class ThreadLocal<T> {
//泛型T
}

内部的数据结构

1.ThreadLocal存储是以Key-Value的方式,但是不同于常用的map,内部实现了一个ThreadLocalMap

1
2
3
4
5
6
7
8
9
10
11
12
static class ThreadLocalMap {

static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;

Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
}

2.Entry中的Key是以弱引用的持有了ThreadLocal,而Value是强引用,看代码可能不太好理解,看看结构图:
tBQrh4.png

3.不同于普通map存储特点,ThreadLocalMap提供了set(T value)方法,具体看看这里怎样实现的:

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
public void set(T value) {
//step1
Thread t = Thread.currentThread();
//step2
ThreadLocalMap map = getMap(t);
if (map != null)
//不同的ThreadLocal对象,对应不同的key,也即 Key(ThreadLocal)-Value(传入的值) step3
map.set(this, value);
else
createMap(t, value); //step4
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}

//Thread类的成员变量
ThreadLocal.ThreadLocalMap threadLocals = null;

void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}

//step3中

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

//不同的ThreadLocal代表不同的key,通过set设置不同的value;一一对应保存在 Entry数组之中。后续取值时显然数组随机访问效率更高。
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();
}

set(T value)时其实就是拿到当前操作的线程Thread并为其内部的成员变量ThreadLocalMap赋值的过程。一个线程可以包含多个ThreadLocal对象的,而这些ThreadLocal-value对象被包装成Entry被存储到了ThreadLocalMap当中(本质还是数组)。

4.再来看看是如何取值的:

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
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
//当ThreadLocal调用get(),其实相当于为map传入了key,以key获得value
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
//还是为threadLocals赋值操作
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);
return value;
}

protected T initialValue() {
return null;
}

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

//关键注意点
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;
if (k == null)
expungeStaleEntry(i);
else
i = nextIndex(i, len);
e = tab[i];
}
return null;
}

5.table数组通过下标i取值,如果e为空,调用了getEntryAfterMiss()这个方法内主要将key为null对应的value清除掉。

为什么存在内存泄漏?

1.上述提到Entry用于存储键值对,但是key是弱引用TreadLocal,而value使用的是强引用,弱引用在下次GC的收集发生时会被回收导致key为空,此时value作为强引用还是存在的。TreadLocal在设计之初是考虑到内存泄漏问题的,包括set() remove() get()几个方法在被调用的时候,都对key为空的情况做了清理。那么key为什么不设计成强引用呢?假设key被设计成强引用,那么回忆之前那张引用图,堆中的TreadLocal对象就存在两个强引用指向。一个是栈中(ThreadLocal被new出来的),一个是key,如果开发者显式的断开引用threadLocal = null,此时key还是强引用指向堆中的ThreadLocal对象。那么堆中的对象是无法被回收的,只有等到线程生命周期结束,threadLocals才会被置为空,entry才会释放。那么可以认为entry已经造成内存泄漏了。
tDDa3n.png

2.当在使用线程池复用线程时,此时使用了ThreadLocal来保存数据的话,一定要做适当的释放操作,不仅可能带来泄漏问题。甚至造成业务数据的错乱。

这个功能是摆设,看看就好~~~