ThreadLocal从名字上看好像是一个Thread,其实并不是,它是Therad的局部变量的维护类。作用是让变量私有化(为每个Thread提供变量的副本),以此来实现线程间变量的隔离。比如有一个变量count,在多线程并发时操作count++会出现线程安全问题。但是通过ThreadLocal count,就可以为每个线程创建只属于当前线程的count副本,各自操作各自的副本,不会影响到其他线程。我们先有个概念,具体还是看源码(JDK1.8)。
简单用法
public static void main(String[] args) {
ThreadLocal<String> a = new ThreadLocal<String>();
a.set("1");
a.set("2");
System.out.println(a.get());
}
//输出结果是2。貌似“1”被覆盖了。
先看一下set(T value)方法。
/**
* 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
ThreadLocalMap map = getMap(t);
//map为空就创建,不为空就set
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
//给t.threadLocals赋值成ThreadLocalMap实例。
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
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);
}
/**
* Set the resize threshold to maintain at worst a 2/3 load factor.
*/
private void setThreshold(int len) {
threshold = len * 2 / 3;
}
/* 当前线程对于的ThreadLocalMap实例,在ThreadL类中*/
ThreadLocal.ThreadLocalMap threadLocals = null;
这段代码逻辑比较简单,主要看ThreadLocalMap,它是TreadLocal的内部类,虽然没有实现Map接口,但看它的几个主要属性:Entry[] table、size、threshold、INITIAL_CAPACITY,和JAVA.util.HashMap极其类似。关于这些属性更详尽的解释可以看一下这篇深入讲解HashMap的工作原理 。class注释中也提到它是一个为存放本地线程值而定制的hash map。它的key就是ThreadLocal当前实例this,值就是set的参数值。既然是hash map,就有可能出现hash冲突的问题,再复习一下解决hash冲突的常见方法
ThreadLocalMap用的是开放地址方法,如果当前位置有值,就继续寻找下一个位置,注意table[len-1]的下一个位置是table[0],就像是一个环形数组,所以也叫闭散列法。如果一直都找不到空位置就会出现死循环,发生内存溢出。当然有扩容机制,一般不会找不到空位置的。
/**
* ThreadLocalMap is a customized hash map suitable only for
* maintaining thread local values. No operations are exported
* outside of the ThreadLocal class. The class is package private to
* allow declaration of fields in class Thread. To help deal with
* very large and long-lived usages, the hash table entries use
* WeakReferences for keys. However, since reference queues are not
* used, stale entries are guaranteed to be removed only when
* the table starts running out of space.
*
*/
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;
}
}
/**
* The table, resized as necessary.
* table.length MUST always be a power of two.
* table.length必须是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.
* table扩容的阈值,初始threshold = length * 2 / 3,当size>threshold*3/4时就扩容
*/
private int threshold;
/**
* The initial capacity -- MUST be a power of two.
* table 的默认容量
*/
private static final int INITIAL_CAPACITY = 16;
/**
* 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) {
Entry[] tab = table;
int len = tab.length;
//计算key的角标index。就是用key的threadLocalHashCode & (len-1)等效于key.threadLocalHashCode%len
//只是&要比%效率高,它们之所以可以等效,因为len是2的n次幂。
//threadLocalHashCode并不影响读懂这块代码,放在后面详说
int i = key.threadLocalHashCode & (len-1);
//开放地址方法,循环tab
for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
ThreadLocal<?> k = e.get();
//key相同,更新value
if (k == key) {
e.value = value;
return;
}
//key为空,说明ThreadLocal实例被回收了,用新key-value替代
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}
//table[i]=null 新建一个Entity,++size
tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}
//整理table
private void rehash() {
//删除table[]陈旧元素
expungeStaleEntries();
//size依然大于3/4 threshold,扩容
if (size >= threshold - threshold / 4)
resize();
}
/**
* Expunge all stale entries in the table.
* 删除table[]所有key==null的entity
*/
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.
* 扩容为原数组的2倍
*/
private void resize() {
Entry[] oldTab = table;
int oldLen = oldTab.length;
int newLen = oldLen * 2;
//创建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 {
//计算新数组index
int h = k.threadLocalHashCode & (newLen - 1);
while (newTab[h] != null)
h = nextIndex(h, newLen);
newTab[h] = e;
count++;
}
}
}
setThreshold(newLen);
size = count;
table = newTab;
}
//返回当前线程对应的ThreadLocalMap
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
}
看了set方法,get方法就
/**
* 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();
}
/**
* 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();//null
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}
//默认值null
protected T initialValue() {
return null;
}
总体来讲,ThreadLocal源码比较好理解。ThreadLocalMap虽然在ThreadLocal中定义,但是被Thread.threadLocals引用。这样保证了一个Thread拥有独立的ThreadLocalMap,做到和其他线程隔离。而ThreadLocalMap的key就是ThreadLocal实例,value就是线程变量。
再看一下最开始的源码。
public static void main(String[] args) {
ThreadLocal<String> a =new ThreadLocal<String>();
a.set("1");
a.set("2");
System.out.println(a.get());
}
//输出结果是2。貌似“1”被覆盖了。
//确实是被覆盖了,Thread.threadLocals的key是a,值当然只能有一个,get到的值也是最后一个value
//单线程的内部实现类似这样
ThreadLocal<String> a =new ThreadLocal<String>();
Map map = new HashMap();
map.put(a,"1");
map.put(a,"2");
System.out.println(map.get(a));
上文说到ThreadLocalMap解决hash冲突的方法是开放地址。但对threadLocalHashCode没有详细说明,下面补充说明一下它。
//计算数组下标
int i = key.threadLocalHashCode & (len-1);
private final int threadLocalHashCode = nextHashCode();
/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
* 线程安全的原子类,发出下一个hash code
*/
private static AtomicInteger nextHashCode = new AtomicInteger();
/**
* getAndAdd(v)返回的结果是nextHashCode,但是nextHashCode+=HASH_INCREMENT;
*/
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}
/**
* 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;
因为nextHashCode被static修饰,所以每次new ThreadLocal()都会自增HASH_INCREMENT,其值和斐波那契散列(Fibonacci)有关,主要目的是为了让哈希码能均匀的分布在2的n次方的数组里。这也是为什么table的容量是2的n次方的一个原因。
它的应用场景主要有
最近在与前端对接的接口中用到了ThreadLocal。大概流程是,前端在请求后端接口时在header带上toekn,拦截器通过token获取到用户信息,通过ThreadLocal保存。主要代码如下:
//接口请求时先走filter
public boolean checkUserLogin(String token){
UserDTO user = getUserByToken(token);
ContextUtil.setUserId(user.getId());
}
public class ContextUtil {
private static ThreadLocal<String> userIdHolder = new ThreadLocal();
//存储userid
public static void setUserId(String userId) {
userIdHolder.set(userId);
}
public static String getUserId() {
return (String)userIdHolder.get();
}
}
//实际调用接口
void invokeInterface(){
String userId = ContextUtil.getUserId();
.....
}
每一次接口请求都是一个线程,在校验接口合法后把userid存入ThreadLocal,以备后续之用。
我们通过源码,对ThreadLocal的原理和应用作了深入讲解。当然本人能力一般,水平有限,难免有些谬误。还请各位多担待,欢迎指正。有反馈才有进步。