当前位置: 首页 > news >正文

ThreadLocal源码分析

前言

ThreadLocal 在 Java 中是一个非常有用的工具类,它提供了线程局部变量的功能。这意味着每个使用该变量的线程都会有一个该变量值的副本,该副本独立于其他线程的变量副本。通过这种方式,ThreadLocal 解决了多线程环境中数据共享和隔离的问题,避免了在并发环境下因数据共享而导致的复杂同步问题。
将ThreadLocal定义成静态变量,还可以实现无侵入地透传参数,在传递一些业务无关参数场景非常好用.

代表和图片下方的文字配合食用!!!

源码分析

ThreadLocal 其实更像是一个工具类,用来处理传入到工具类里面的数据。而要传进去的对象,则存储在Thread类里面,是Thread类的成员变量,自然就是线程私有的了。

创建ThreadLocal

这其实没什么,就单纯创建一个ThreadLocal 对象,没有更多的操作。如下,创建了一个用来存储字符串的ThreadLocal对象。

public static ThreadLocal<String> threadLocal=new ThreadLocal<>();

往ThreadLocal 存数据

    /*** 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);elsecreateMap(t, value);}

存数据的时候,先获取到当前运行的线程对象,然后通过getMap方法获取ThreadLocalMap对象,然后将数据存储到ThreadLocalMap对象里中。

   /*** Get the map associated with a ThreadLocal. Overridden in* InheritableThreadLocal.** @param  t the current thread* @return the map*/ThreadLocalMap getMap(Thread t) {return t.threadLocals;}

通过getMap方法可以看出,这个ThreadLocalMap对象是Thread类的成员变量,意味着线程独有,因此是线程安全的。

/*** 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];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();}

ThreadLocalMap不为空的时候调用该方法,从注释也可以看出这个方法将值和它的key(ThreadLocal<?>对象)联系起来。 首先会通过ThreadLocal<?>对象去计算下标,然后从该下标开始一直往后遍历数组,如果节点不为空,则判断传入的ThreadLocal对象和数组节点存的是否是同一个,是同一个就更新value,如果不是同一个,则判断数组节点存的Entry的key是否为空,如果为空则调用replaceStaleEntry方法替换过期的Entry(内存泄漏再细说);如果数组节点存的ThreadLocal对象里面没有值或找到了一个空的节点,就创建一个Entry存到该节点。存完之后会判断是否需要扩容,如果需要则扩容为原容量的两倍。

      /*** Create the map associated with a ThreadLocal. Overridden in* InheritableThreadLocal.** @param t the current thread* @param firstValue value for the initial entry of the map*/void createMap(Thread t, T firstValue) {t.threadLocals = new ThreadLocalMap(this, firstValue);}
  /*** Construct a new map initially containing (firstKey, firstValue).* ThreadLocalMaps are constructed lazily, so we only create* one when we have at least one entry to put in it.*/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为空,则创建ThreadLocalMap对象,并创建Entry对象直接存到算出来的下标节点。

也就是说,数据实际上存储在ThreadLocalMap里面,这个ThreadLocalMap存储着当前线程的全部ThreadLocal数据。

从ThreadLocal 取数据

/*** 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();}

获取ThreadLocalMap操作和上面存数据一样.

        /*** Get the entry associated with key.  This method* itself handles only the fast path: a direct hit of existing* key. It otherwise relays to getEntryAfterMiss.  This is* designed to maximize performance for direct hits, in part* by making this method readily inlinable.** @param  key the thread local object* @return the entry associated with key, or null if no such*/private Entry getEntry(ThreadLocal<?> key) {int i = key.threadLocalHashCode & (table.length - 1);Entry e = table[i];if (e != null && e.get() == key)return e;elsereturn getEntryAfterMiss(key, i, e);}

如果ThreadLocalMap不为空,则通过ThreadLocal对象去计算数组下标,如果节点有数据则判断Entry的key对象和传入的是否是同一个,同一个则返回.

        /*** 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) {ThreadLocal<?> k = e.get();if (k == key)return e;if (k == null)expungeStaleEntry(i);elsei = 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);}

否则一直沿着数组往下去对比,一直到获取到数据或者节点为空.然后返回.

如果ThreadLocalMap.Entry e = map.getEntry(this);返回的Entry不为空,则直接返回它的值.

    /*** 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();Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null)map.set(this, value);elsecreateMap(t, value);return value;}/*** Returns the current thread's "initial value" for this* thread-local variable.  This method will be invoked the first* time a thread accesses the variable with the {@link #get}* method, unless the thread previously invoked the {@link #set}* method, in which case the {@code initialValue} method will not* be invoked for the thread.  Normally, this method is invoked at* most once per thread, but it may be invoked again in case of* subsequent invocations of {@link #remove} followed by {@link #get}.** <p>This implementation simply returns {@code null}; if the* programmer desires thread-local variables to have an initial* value other than {@code null}, {@code ThreadLocal} must be* subclassed, and this method overridden.  Typically, an* anonymous inner class will be used.** @return the initial value for this thread-local*/protected T initialValue() {return null;}

如果返回的Entry为空,或者一开始获取的map就为空,则走初始化值的方法.如果ThreadLocalMap为空则会创建一个ThreadLocalMap对象,然后往里存入一个Entry,用传入的ThreadLocal对象作为key,value为null.
如果ThreadLocalMap不为空则直接往里存入一个Entry,用传入的ThreadLocal对象作为key,value为null.
最后返回null.

内存泄露分析

        /*** 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;}}

我们看一下ThreadLocalMap的静态内部类Entry.该类继承了WeakReference类.这是弱引用类.看Entry 类的构造方法,它用ThreadLocal作为参数去调用父类WeakReference的构造方法.也就是说Entry的key,也就是指向ThreadLocal对象的是一个弱引用.而Entry指向value的引用没经过特殊处理,的是一个强引用.

在这里插入图片描述
如上图,在不手动调用remove方法清空数据的情况下,如果线程对象被销毁回收了,ThreadLocalMap对象没有引用指向,整个ThreadLocalMap对象也会被回收.
不过现在一般都是使用线程池技术,这意味线程创建之后大概率长时间存在,那么Thread这条引用链一直都不会断.
如果外部指向key的ThreadLocal引用是一个局部作用域引用,在超出作用域之后,这条引用链就会断掉;而entry指向key的引用是弱引用,在下一次垃圾回收的时候就会将key回收掉.

      /*** 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);(e = tab[i]) != null;i = prevIndex(i, len))if (e.get() == null)slotToExpunge = i;// Find either the key or trailing null slot of run, whichever// occurs firstfor (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) {e.value = value;tab[i] = tab[staleSlot];tab[staleSlot] = e;// Start expunge at preceding stale entry if it existsif (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 slottab[staleSlot].value = null;tab[staleSlot] = new Entry(key, value);// If there are any other stale entries in run, expunge themif (slotToExpunge != staleSlot)cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);}

在key被回收掉之后,提回到之前set值和get值的时候各有一个方法.如上图(看最后几行)的set方法在判断到存的entry的key为空的时候,会将entry指向value的引用断开,这样value就没有引用指向了,后续垃圾回收会将value回收掉.

/*** 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 staleSlottab[staleSlot].value = null;tab[staleSlot] = null;size--;// Rehash until we encounter nullEntry 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;}

调用get方法的时候遇到key为空也会有类似的清空value的操作.如上图(看前几行).其实也不难理解,key都为空了,意味着这个entry永远都不会有线程再去获取它存的value,也就没有意义了,所以要断开value的引用来是否内存.

前面都是基于ThreadLocal定义成局部变量的情况,但实际上外部指向key的ThreadLocal引用大概率会被定义成静态字段,这意味着这个ThreadLocal引用几乎贯穿整个应用程序的始终.采用线程池技术,外部ThreadLocal定义成静态变量,再加上忘记手动remove,那么这个value永远都不会释放.这就造成了内存泄漏.
甚至可能不只是内存泄漏,还会获取到脏数据.因为线程是复用的,下次使用的时候直接get,就会获取到上一次执行所存的数据,形成读到脏数据的情况.

使用小tip

1.尽量定义成静态变量,如果定义成局部变量的话就失去了参数透传的特性,还不如直接用对应的数据类型.
2.使用了之后必须要手动remove

相关文章:

  • Spring-Boot基础--yaml
  • Qcom平台通过Hexagon IDE 测试程序性能指导
  • Puppeteer动态代理实战:提升数据抓取效率
  • Qt: 窗口停靠框架
  • PostgreSQL创建表和自增序列
  • [数据分析]脑图像处理工具
  • Qt 实战(6)事件 | 6.3、自定义事件
  • 自定义注解 + Redis 实现业务的幂等性
  • juicefs部署实践
  • 任意空间平面点云旋转投影至水平面—罗德里格旋转公式
  • 【简洁明了】调节大模型的prompt的方法【带案例】
  • 一款IM即时通讯聊天系统源码,包含app和后台源码
  • 【Hive SQL 每日一题】找出各个商品销售额的中位数
  • C语言 ——— 实用调试技巧(Visual Studio)
  • 业务系统核心模块资料访问性能优化实战
  • 时间复杂度分析经典问题——最大子序列和
  • 「面试题」如何实现一个圣杯布局?
  • go append函数以及写入
  • go语言学习初探(一)
  • Java超时控制的实现
  • java多线程
  • java概述
  • JSONP原理
  • mysql中InnoDB引擎中页的概念
  • Python socket服务器端、客户端传送信息
  • Rancher如何对接Ceph-RBD块存储
  • Shell编程
  • SpringBoot 实战 (三) | 配置文件详解
  • SwizzleMethod 黑魔法
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 给Prometheus造假数据的方法
  • 异常机制详解
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​​​​​​​​​​​​​​Γ函数
  • # 计算机视觉入门
  • # 消息中间件 RocketMQ 高级功能和源码分析(七)
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #Datawhale AI夏令营第4期#AIGC文生图方向复盘
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (1)STL算法之遍历容器
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (强烈推荐)移动端音视频从零到上手(上)
  • (区间dp) (经典例题) 石子合并
  • (学习日记)2024.01.19
  • **CI中自动类加载的用法总结
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .net core docker部署教程和细节问题
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .net和php怎么连接,php和apache之间如何连接
  • .NET设计模式(8):适配器模式(Adapter Pattern)