JAVA并发--ThreadLocal
最近看同事代码看到ThreadLocal,感到十分模糊,因此认真学习下,正巧最近听业内大神说最好能够写一些自己的技术博客,一方面可以总结自己所学查漏补缺,一方面和更多朋友交流,因此再次拾起自己的博客,准备好好运营下,就把文章分享到这里。
ThreadLocal概览
ThreadLocal类是java.lang包中的一个类,而且从jdk1.2版本就已经存在,其采用了空间换时间的思想,为每一个线程保存一份不同变量的副本,它独立于变量的初始化副本,因为访问某个变量(通过其 get 或 set 方法)的每个线程都有自己的局部变量,从而巧妙的避开了线程安全问题。ThreadLocal 实例通常是类中的 private static 字段,它们希望将状态与某一个线程(例如,用户 ID 或事务 ID)相关联。
ThreadLocal广泛应用于spring,struts等框架内,
如struts2中
public class ActionContext implements Serializable { static ThreadLocal actionContext = new ThreadLocal();}
以此保存request,session等变量的线程安全,并在一次request的请求中有效,一次请求结束,线程回收并销毁request,session等信息
比如在spring中:同样持有request的容器类RequestContextHolder也使用了ThreadLocal来存放此类变量
abstract class RequestContextHolder : private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal<RequestAttributes>("Request attributes"); private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder = new NamedInheritableThreadLocal<RequestAttributes>("Request context");
再来看看同事代码中的应用
private static final ThreadLocal<FootTracerInfo> traceInfo = new ThreadLocal<FootTracerInfo>(); public static FootTracerInfo getTraceInfo() { return traceInfo.get(); } public static void setTraceInfo(FootTracerInfo httpClientInfo) { traceInfo.set(httpClientInfo); } public static void destroyTraceInfo() { traceInfo.remove(); }
上面的例子清晰的展示了如何使用ThreadLocal来构建我们自己的程序,FootTracerInfo对象是一个与request相关的信息封装体,它主要封装request的一些公共信息如ip,session,token等,此外还需要封装一部分业务相关信息,并在服务端发送给实时消息系统kafka,因此这肯定是个并发的多线程场景,并且此对象只在一次request的作用域内有效。使用ThreadLocal来解决这个问题十分巧妙,每个request对应一个线程,只能访问本线程对应的变量副本,保证其信息的线程安全。
ThreadLocal源码分析:
成员变量:
private final int threadLocalHashCode = nextHashCode(); private static AtomicInteger nextHashCode = new AtomicInteger(); private static final int HASH_INCREMENT = 0x61c88647; /** * Returns the next hash code. */ private static int nextHashCode() { return nextHashCode.getAndAdd(HASH_INCREMENT); }
我们看到ThreadLocal有一个成员变量,而且是private final修饰,threadLocalHashCode,它由静态变量nextHashCode经过
nextHashCode.getAndAdd(HASH_INCREMENT);运算获取,nexHashCode是AtomicInteger类型,因此运算是原子进行的。也即是说每个ThreadLocal实例有一个唯一的成员变量可以作为其唯一标识,而此变量从0开始以0x61c88647的大小递增。
0x61c88647 这个魔数如何产生的呢?
private static final int INITIAL_CAPACITY = 16; private Entry[] table; 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); }
ThreadLocalMap是ThreadLocal中的一个内部类,firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);此运算确定ThreadLocal对象在Entry数组中位置,INITIAL_CAPACITY为初始长度=16,每次扩容都增长为原来的2倍,即它的长度始终是2的n次方,上述算法中使用0x61c88647
可以让hash的结果在2的n次方内尽可能均匀分布,减少冲突的概率。
至于为什么如此,大家可以自行研究。
/** * 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 { int h = k.threadLocalHashCode & (newLen - 1); while (newTab[h] != null) h = nextIndex(h, newLen); newTab[h] = e; count++; } } } setThreshold(newLen); size = count; table = newTab; }
方法摘要:
T |
get() 返回此线程局部变量的当前线程副本中的值。 |
protected T |
initialValue() 返回此线程局部变量的当前线程的“初始值”。 |
void |
remove() 移除此线程局部变量当前线程的值。 |
void |
set(T value) 将此线程局部变量的当前线程副本中的值设置为指定值。 |
1. 先来看看get方法:
ThreadLocalMap map =Thread.currentThread().threadLocals
首先获取了线程本身的变量ThreadLocalMap的实例,查看Thread类源码发现threadLocals是其成员变量
ThreadLocal.ThreadLocalMap threadLocals = null; public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t);//获取当前线程对应的成员变量threadLocals if (map != null) { //非空,即已经初始化过 ThreadLocalMap.Entry e = map.getEntry(this);//获取当前线程对应entry if (e != null) return (T)e.value; } return setInitialValue();//如果为空,则初始化 } ThreadLocalMap getMap(Thread t) { return t.threadLocals; } private T setInitialValue() { T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t);//再次获取当前线程对应threadLocals变量 if (map != null) map.set(this, value); else createMap(t, value); //创建此map return value; } void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); //初始化threadLocals }
我们上面跟踪源码get方法发现,它获取了当前线程,并以此去获取了一个ThreadLocalMap对象,这是Thread类里面的一个成员变量,每一个线程都含有一个ThreadLocalMap实例
ThreadLocal.ThreadLocalMap threadLocals = null; //Thred类的成员变量
2. ThreadLocal.ThreadLocalMap类
从上面的代码中,我们发现每一个线程包含一个ThreadLocalMap的实例,现在再来看看这个内部类是什么。我们在上面有看过在ThreadLocalMap的构造方法中,有一个变量
table = new Entry[INITIAL_CAPACITY]; 这是一个Entry对象数组
private Entry[] table; //ThreadLocalMap底层存储结构为数组,类型为Entry
Entry对象key为ThreadLocal类型,value为需要保存的变量副本
static class Entry extends WeakReference<ThreadLocal> { //继承了弱引用 /** The value associated with this ThreadLocal. */ Object value;//value即是每个线程存储的变量副本,通过ThreadLocal实例为key获取 Entry(ThreadLocal k, Object v) { super(k); value = v; } }
如何获取变量副本?这里的逻辑相对比较复杂,
1 getEntry---->根据hashcode定位获取entry数组中对应位置的entry对象e---->
这里有三种情况:a):e为null,返回null;b):e!=null 且 e.get()==key 即我们要找的entry正是其hashcode定位的位置 ;
因为我们使用的魔数0x61c88647,及数组扩容规律,entry对象在数组中尽可能的分散开,所以以上2种可能是较大概率事件,但扔无法排除第三种情况
c):e!=null,且e.get()!=key,这时调用了处理函数getEntryAfterMiss(key, i, e) (当然第一种情况也会调用此函数,但此时逻辑比较清晰,因此单独分开来算)
2 在getEntryAfterMiss函数中,循环调用nextIndex函数,来查找entry数组的下一个角标位置是否为要找的key,若是则返回,不是则继续直到下一个entry为null,则跳出循环返回null
在循环中还会出现另一种情况,即entry非空,但entry.key为null,此是因为ThreadLocal为弱引用,因而当ThreadLocal没有其他强引用时,其会被gc回收,造成entry.key==null
3 当entry.key==null时:将会调用expungeStaleEntry函数,此函数会循环调用nextIndex清除entry.key==null的entry对象,并重新定位那些key非nulll的entry对象并迁移,直到entry为null停止循环
private Entry getEntry(ThreadLocal key) { int i = key.threadLocalHashCode & (table.length - 1); //根据ThreadLocal唯一标识hashcode码,获取hash槽 Entry e = table[i]; if (e != null && e.get() == key) //非空并且对应entry的key正好是要找的ThreadLocal实例 则返回, return e; else return getEntryAfterMiss(key, i, e); //处理非直接命中的情形 } /** * 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) { //如果根据hashcode定位到的entry为null,则说明不存在,直接返回 ThreadLocal k = e.get(); if (k == key) return e; //获取到对应key则返回 if (k == null) expungeStaleEntry(i); //如果key为null则说明ThreadLocal已经被gc回收,需要移除此entry else i = nextIndex(i, len); e = tab[i]; } return null; } /** * Increment i modulo len. */ private static int nextIndex(int i, int len) { return ((i + 1 < len) ? i + 1 : 0); //一个位置 } /** * 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; //移除已被gc回收的entry 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) {//发现key被gc回收的同样移除 e.value = null; tab[i] = null; size--; } else {//否则重新设置此entry的位置,并将原位置移除 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. while (tab[h] != null)//向下遍历 直到全部迁移 h = nextIndex(h, len); tab[h] = e; } } } return i; }
2 上面我们看了getEntry的代码,下面看下set函数
set函数与get想发操作,但情形相同,有三种情况
1 根据hashcode计算到的角标位置entry==null,新建一个entry对象放于此位置
2 如果entry!=null,则向下循环查找,直到找到对应entry,重新设置值,或者找到entry==null,则按1步骤新建一个放于此位置
3 如果2中发现某个entry!=null,且entry.key==null,则此entry已被gc回收,调用replaceStaleEntry函数替换掉此entry
replaceStaleEntry函数的逻辑我们这里不再细说
/** * 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];//初始i为hashcode算法定位位置 e != null;//如果非null,则判断key是否为当前threadlocal实例,若是更新value,不是则循环调用nextIndex e = tab[i = nextIndex(i, len)]) { ThreadLocal k = e.get(); if (k == key) { e.value = value; return; } if (k == null) {//若entry的key已经被gc回收,则移除并替换为要设置的threadlocal replaceStaleEntry(key, value, i); return; } } tab[i] = new Entry(key, value); //若未找到则新建一个entry,并插入 int sz = ++size; if (!cleanSomeSlots(i, sz) && sz >= threshold) rehash(); }
/**
* 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); //向前退回 直到第一个为null的entry位置
(e = tab[i]) != null;
i = prevIndex(i, len))
if (e.get() == null)
slotToExpunge = i; //记录for循环中最后一个需要移除的位置,也即数组中最前一个
// 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) { //如果找到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); //没有找到,则将需要移除的元素置null,并创建当前entry对象,添加到此位置
// If there are any other stale entries in run, expunge them
if (slotToExpunge != staleSlot) //移除其他的已经废弃元素
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}
3 再次回到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); }
从上可以看到,首先获取当前线程的ThreadLocalMap变量,然后调用其set方法,或者map为null,则需要先创建此map对象。
4 remove函数
public void remove() { ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this); }
直接调用的ThreadLocalMap的remove方法
/** * Remove the entry for key. */ 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) {//找到此entry,则清除此entry对象 e.clear();//将key的弱引用置为null expungeStaleEntry(i);// return; } } }
private int expungeStaleEntry(int staleSlot) {
Entry[] tab = table;
int len = tab.length;
// expunge entry at staleSlot
tab[staleSlot].value = null; //将value置为null
tab[staleSlot] = null;//将数组中指定位置元素置为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) { //去除废弃元素
e.value = null;
tab[i] = null;
size--;
} else {
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.
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
return i;
}