一、ThreadLocal简述
JAVA对象在线程间是共享的,某些场景下,我们期望对象在线程间隔离,即一个对象只对一个线程可见,其他线程无法访问。ThreadLocal提供了线程内局部变量的机制,这个变量在线程的生命周期中起作用。
二、ThreadLocal使用
下面是官方文档中的ThreadLocal的api:
代码示例
public class ThreadLocalTest {
private static final ThreadLocal<Integer> stringThreadLocal = new ThreadLocal<>();
public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(4);
for (int i = 0; i < 4; i ++) {
new Thread(new MyThread(barrier)).start();
}
}
static class MyThread implements Runnable {
private CyclicBarrier barrier;
public MyThread(CyclicBarrier barrier) {
this.barrier = barrier;
}
@Override
public void run() {
try {
barrier.await();
for (int i = 0; i < 100; i++) {
Integer value = stringThreadLocal.get();
if (value == null) {
value = 0;
}
Integer sum = value + i;
stringThreadLocal.set(sum);
}
System.out.println(Thread.currentThread().getName() + " sum is " + stringThreadLocal.get());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
运行结果
Thread-1 sum is 4950
Thread-0 sum is 4950
Thread-3 sum is 4950
Thread-2 sum is 4950
**/
从运行结果可以看出,每个线程的变量是独有的,线程之间不会相互影响。
三、自己实现ThreadLocal的想法
最初的想法就是维护一个线程到值得映射map,如下:
public class MyThreadLocal<T> {
private Map<Thread, T> keyValueMap = new HashMap<>();
public synchronized void set(T value) {
Thread thread = Thread.currentThread();
keyValueMap.put(thread, value);
}
public synchronized T get() {
Thread thread = Thread.currentThread();
return keyValueMap.get(thread);
}
public synchronized void remove() {
Thread thread = Thread.currentThread();
keyValueMap.remove(thread);
}
}
自己实现存在的问题:
1、synchronized 导致并发性能较差
带着问题,我们看下ThreadLocal是怎么实现的。
四、ThreadLocal源码分析
先来看set方法,如下:
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
可以看到,set方法将set值的任务委托给了ThreadLocalMap,而ThreadLocalMap居然是Thread中的变量,这跟假想的变量保存在ThreadLocal中是不一样的,稍后对ThreadLocalMap进行深入分析。set方法的逻辑也很清晰,如果ThreadLocalMap不为空则直接设置值,否则创建新的ThreadLocalMap。接下来继续看get方法
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();
}
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;
}
这里get方法也是将操作委托给了ThreadLocalMap,通过最终获得ThreadLocalMap.Entry来获取最终的值。提一下setInitialValue()方法,用来设置初始值,而initialValue()方法是protected,子类继承ThreadLocal可以实现初始值的设置。
下面来看remove方法,同样是交给ThreadLocalMap来处理。
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}
五、ThreadLocalMap源码分析
ThreadLocal的实现重点在ThreadLocalMap中的实现。先看ThreadLocalMap的存储位置,它是存在Thread对象中的,而非存在ThreadLocal对象中。我理解这样做是为了避免多线程竞争,因为放在Thread对象中就相当于线程私有了,处理的时候不需要加锁,这样就避免了我们前面自己实现的代码中加锁导致效率低的问题。由于ThreadLocal本身的设计就是变量不与其他线程共享,不需要其他线程访问本对象的变量,放在Thread对象中不会有问题。
继续看ThreadLocalMap源码。ThreadLocalMap是ThreadLocal的一个静态内部类。
1、Entry数据结构
ThreadLocalMap维护了一个Entry类型的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;
}
}
/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
*/
private Entry[] table;
Entry是一个以ThreadLocal为key,Object为value的键值对,需要注意的是,threadLocal是弱引用,当外部的threadLocal被置为null时,threadLocal会被回收。
2、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.
ThreadLocal.ThreadLocalMap.Entry[] tab = table;
int len = tab.length;
// 定位Entry存放的位置
int i = key.threadLocalHashCode & (len-1);
// 处理hash冲突的情况,这里采用的是开放地址法
for (ThreadLocal.ThreadLocalMap.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;
}
}
// 新建entry并插入
tab[i] = new ThreadLocal.ThreadLocalMap.Entry(key, value);
int sz = ++size;
// 清除脏数据,扩容
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
3、get源码
private ThreadLocal.ThreadLocalMap.Entry getEntry(ThreadLocal<?> key) {
// 确定entry位置
int i = key.threadLocalHashCode & (table.length - 1);
ThreadLocal.ThreadLocalMap.Entry e = table[i];
// 命中
if (e != null && e.get() == key)
return e;
else
// 存在hash冲突,继续查找
return getEntryAfterMiss(key, i, e);
}
private ThreadLocal.ThreadLocalMap.Entry getEntryAfterMiss(ThreadLocal<?> key, int i, ThreadLocal.ThreadLocalMap.Entry e) {
ThreadLocal.ThreadLocalMap.Entry[] tab = table;
int len = tab.length;
while (e != null) {
ThreadLocal<?> k = e.get();
//找到entry
if (k == key)
return e;
// 脏数据处理
if (k == null)
expungeStaleEntry(i);
else
//遍历
i = nextIndex(i, len);
e = tab[i];
}
return null;
}
4、remove源码
private void remove(ThreadLocal<?> key) {
ThreadLocal.ThreadLocalMap.Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);
for (ThreadLocal.ThreadLocalMap.Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
if (e.get() == key) {
//清空key
e.clear();
//清空value
expungeStaleEntry(i);
return;
}
}
}
六、内存泄漏问题分析
先来看下ThreadLocal,ThreadLcoalMap,Entry之间的关系图
图中实线表示强引用,虚线表示弱引用。如果threadLocal外部强引用被设置为null,内存中的threadLocal对象会被回收(虚引用不影响回收),但是value值的强引用仍然存在,所以value不会被回收,存在内存泄漏。当然线程如果执行结束,threadLocalMap、Entry都会被回收掉,但是实际开发中我们为了线程复用,大都用线程池维护线程,内存泄漏的问题依然会产生。当然,TheadLocal的实现已经对这种情况进行了处理。简单来说就是在set、get、remove等操作的时候,会将key=null && value != null的entry的value设置为null,清理掉脏数据。