先赞后看,养成习惯 🌹 欢迎微信关注
[Java编程之道]
,天天进步一点点,沉淀技术分享知识。java
前面在个人GitHub仓库 V-LoggingTool 中有简单的使用过ThreadLocal,主要用在了切面类中,功能上须要取到前置加强拦截到的用户信息暂存,执行到后置加强时从该ThreadLocal中取出用户信息并使用。git
今天我们就唠唠ThreadLocal的相关知识,了解一下他的数据结构、用法、原理等。我们层层深刻...github
看了网上很多关于ThreadLocal的讲解,源码比较简单可是对于Thread、ThreadLocal、ThreadLocalMap的关系讲的有点晦涩,尤为是那张亘古不变的ThreadLocal的内部结构图,额...我真的看了好久才明白是怎么回事。编程
ThreadLocal是一个本地线程副本变量工具类,主要用于将私有线程和该线程存放的副本对象作一个映射,各个线程之间的变量互不干扰。数组
官方说的仍是比较明白了,提炼关键字工具类
,在我看来ThreadLocal就是提供给每一个线程操做变量的工具类,作到了线程之间的变量隔离目的bash
接下来就是看图说话:微信
每一个线程都有其独有的Map结构,而Map中存有的是ThreadLocal为Key变量副本为Vaule的键值对,以此达到变量隔离的目的。数据结构
平时是怎么使用ThreadLocal的?less
package threadlocal;
/** * @Auther: Xianglei * @Company: Java编程之道 * @Date: 2020/7/2 21:44 * @Version 1.0 */
public class main {
private static ThreadLocal<String> sThreadLocal = new ThreadLocal<>();
public static void main(String args[]) {
sThreadLocal.set("这是在主线程中");
System.out.println("线程名字:" + Thread.currentThread().getName() + "---" + sThreadLocal.get());
//线程a
new Thread(new Runnable() {
@Override
public void run() {
sThreadLocal.set("这是在线程a中");
System.out.println("线程名字:" + Thread.currentThread().getName() + "---" + sThreadLocal.get());
}
}, "线程a").start();
//线程b
new Thread(new Runnable() {
@Override
public void run() {
sThreadLocal.set("这是在线程b中");
System.out.println("线程名字:" + Thread.currentThread().getName() + "---" + sThreadLocal.get());
}
}, "线程b").start();
//线程c
new Thread(() -> {
sThreadLocal.set("这是在线程c中");
System.out.println("线程名字:" + Thread.currentThread().getName() + "---" + sThreadLocal.get());
}, "线程c").start();
}
}
复制代码
输出结果以下ide
线程名字:main---这是在主线程中
线程名字:线程b---这是在线程b中
线程名字:线程a---这是在线程a中
线程名字:线程c---这是在线程c中
Process finished with exit code 0
复制代码
能够看出每一个线程各经过ThreadLocal对本身ThreadLocalMap中的数据存取并无出现脏读的现象。就是由于每一个线程内部已经存储了ThreadLocal为Key变量副本为Vaule的键值对。(隔离了)
可能你有点懵,ThreadLocal是怎么把变量复制到Thread的ThreadLocalMap中的?
我们接着唠...
当咱们初始化一个线程的时候其内部干去建立了一个ThreadLocalMap的Map容器
待用。
public class Thread implements Runnable {
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
}
复制代码
当ThreadLocalMap被建立加载的时候其静态内部类Entry也随之加载,完成初始化动做。
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
复制代码
到此,线程Thread内部的Map容器初始化完毕,那么它又是如何和ThreadLocal缠上关系,ThreadLocal又是如何管理键值对的关系。
咱们就其核心方法分析一下内部的逻辑,同时解答上述存在的疑问:
set()方法用于保存当前线程的副本变量值。
get()方法用于获取当前线程的副本变量值。
initialValue()为当前线程初始副本变量值。
remove()方法移除当前线程的副本变量值。
/** * 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 map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
复制代码
解说一下你就懂了:
当咱们在Thread内部调用set方法时:
调用当前方法的线程Thread
。线程内部
的ThreadLocalMap
容器。副本
给丢进去。没了...懂了吗,ThreadLocal(就认为是个维护线程内部变量的工具!)只是在Set的时候去操做了Thread内部的·ThreadLocalMap
将变量拷贝到了Thread内部的Map容器中,Key就是当前的ThreadLocal,Value就是变量的副本。
/** * 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)
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);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}
protected T initialValue() {
return null;
}
复制代码
清除Map中的KV
/** * Removes the current thread's value for this thread-local * variable. If this thread-local variable is subsequently * {@linkplain #get read} by the current thread, its value will be * reinitialized by invoking its {@link #initialValue} method, * unless its value is {@linkplain #set set} by the current thread * in the interim. This may result in multiple invocations of the * <tt>initialValue</tt> method in the current thread. * * @since 1.5 */
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
}
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
/** * 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) {
e.clear();
expungeStaleEntry(i);
return;
}
}
}
复制代码
下面再认识一下ThreadLocalMap
,一个真正存储(隔离)数据的东西。
ThreadLocalMap是ThreadLocal的内部类
,实现了一套本身的Map结构,我们看一下内部的继承关系就一目了然。
其Entry使用的是K-V方式来组织数据,Entry中key是ThreadLocal对象,且是一个弱引用(弱引用,生命周期只能存活到下次GC前
)。
对于弱引用
引起的问题咱们最后再说
。
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
复制代码
ThreadLocalMap的成员变量
static class ThreadLocalMap {
/** * 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. */
private Entry[] table;
/** * The number of entries in the table. */
private int size = 0;
/** * The next size value at which to resize. */
private int threshold; // Default to 0
}
复制代码
ThreaLocalMap中没有采用传统的调用ThreadLocal的hashcode方法(继承自object的hashcode),而是调用nexthashcode
,源码以下:
private final int threadLocalHashCode = nextHashCode();
private static AtomicInteger nextHashCode = new AtomicInteger();
//1640531527 可以让hash槽位分布至关均匀
private static final int HASH_INCREMENT = 0x61c88647;
private static int nextHashCode() {
return nextHashCode.getAndAdd(HASH_INCREMENT);
}
复制代码
和HashMap的最大的不一样在于,ThreadLocalMap解决Hash冲突的方式就是简单的步长加1或减1及线性探测,寻找下一个相邻的位置。
/** * Increment i modulo len. */
private static int nextIndex(int i, int len) {
return ((i + 1 < len) ? i + 1 : 0);
}
/** * Decrement i modulo len. */
private static int prevIndex(int i, int len) {
return ((i - 1 >= 0) ? i - 1 : len - 1);
}
复制代码
ThreadLocalMap采用线性探测的方式解决Hash冲突的效率很低,若有大量不一样的ThreadLocal对象放入map中时发送冲突。因此建议每一个线程只存一个变量(一个ThreadLocal)就不存在Hash冲突的问题,若是一个线程要保存set多个变量,就须要建立多个ThreadLocal,多个ThreadLocal放入Map中时会极大的增长Hash冲突的可能。
清楚意思吗?当你在一个线程须要保存多个变量时,你觉得是屡次set?你错了你得建立多个ThreadLocal,屡次set的达不到存储多个变量的目的。
sThreadLocal.set("这是在线程a中");
复制代码
看看官话,为何要用弱引用。
To help deal with very large and long-lived usages, the hash table entries use WeakReferences for keys.
为了处理很是大
和生命周期
很是长的线程,哈希表使用弱引用做为 key。
ThreadLocal在没有外部对象强引用时如Thread,发生GC时弱引用Key会被回收,而Value是强引用不会回收,若是建立ThreadLocal的线程一直持续运行如线程池中的线程,那么这个Entry对象中的value就有可能一直得不到回收,发生内存泄露。
key 若是使用强引用:引用的ThreadLocal的对象被回收了,可是ThreadLocalMap还持有ThreadLocal的强引用,若是没有手动删除,ThreadLocal不会被回收,致使Entry内存泄漏。
key 使用弱引用:引用的ThreadLocal的对象被回收了,因为ThreadLocalMap持有ThreadLocal的弱引用,即便没有手动删除,ThreadLocal也会被回收。value在下一次ThreadLocalMap调用set,get,remove的时候会被清除。
Java8中已经作了一些优化如,在ThreadLocal的get()、set()、remove()方法调用的时候会清除掉线程ThreadLocalMap中全部Entry中Key为null的Value,并将整个Entry设置为null,利于下次内存回收。
Java8中for循环遍历整个Entry数组,遇到key=null的就会替换从而避免内存泄露的问题。
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();
if (k == null) {
e.value = null;
tab[i] = null;
size--;
} else {
int h = k.threadLocalHashCode & (len - 1);
if (h != i) {
tab[i] = null;
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
return i;
}
复制代码
一般ThreadLocalMap的生命周期跟Thread(注意线程池中的Thread)同样长,若是没有手动删除对应key(线程使用结束归还给线程池了,其中的KV再也不被使用但又不会GC回收,可认为是内存泄漏),必定会致使内存泄漏,可是使用弱引用能够多一层保障:弱引用ThreadLocal会被GC回收,不会内存泄漏,对应的value在下一次ThreadLocalMap调用set,get,remove的时候会被清除
,Java8已经作了上面的代码优化。