ThreadLocal
创始人
2024-04-02 18:34:52
0

前言

ThreadLocal 是线程本地变量。当使用 ThreadLocal 维护变量时,ThreadLocal 为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程。


一、ThreadLocal 使用场景

主要有以下三种场景。

  • 作为数据副本,当某些数据是以线程为作用域并且不同线程有不同数据副本时,可以考虑 ThreadLocal。
  • 保存线程上下文信息,在任意需要的地方可以获取,避免显示传参。
  • 解决线程安全问题,避免某些情况需要考虑线程安全必须同步带来的性能损失。
  • 在进行对象跨层传递的时候,使用ThreadLocal可以避免多次传递,打破层次间的约束。
  • 线程间数据隔离
  • 进行事务操作,用于存储线程事务信息
  • 数据库连接,Session会话管理

二、ThreadLocal的原理和实现

  • 每个线程都有一个 ThreadLocalMap(ThreadLocal内部类),Map 中元素的键为ThreadLocal,而值对应线程的变量副本。Map是数组实现,使用线性探测解决hash冲突,需要手动调用set、get、remove防止内存泄漏。
  • ThreadLoal 变量,线程局部变量,同一个 ThreadLocal 所包含的对象,在不同的 Thread 中有不同的副本。ThreadLocal 变量通常被private static修饰。当一个线程结束时,它所使用的所有 ThreadLocal 相对的实例副本都可被回收。
  • 一个线程内可以存在多个 ThreadLocal 对象,所以其实是 ThreadLocal 内部维护了一个 Map ,这个 Map不是直接使用的 HashMap ,而是 ThreadLocal 实现的一个叫做 ThreadLocalMap 的静态内部类。而我们使用的get()、set() 方法其实都是调用了这个ThreadLocalMap类对应的 get()、set() 方法。

2.ThreadLocal怎么用?

代码如下(示例):

public class ThreadLocalTest02 {public static void main(String[] args) {ThreadLocal local = new ThreadLocal<>();IntStream.range(0, 10).forEach(i -> new Thread(() -> {local.set(Thread.currentThread().getName() + ":" + i);System.out.println("线程:" + Thread.currentThread().getName() + ",local:" + local.get());}).start());}
}输出结果:
线程:Thread-0,local:Thread-0:0
线程:Thread-1,local:Thread-1:1
线程:Thread-2,local:Thread-2:2
线程:Thread-3,local:Thread-3:3
线程:Thread-4,local:Thread-4:4
线程:Thread-5,local:Thread-5:5
线程:Thread-6,local:Thread-6:6
线程:Thread-7,local:Thread-7:7
线程:Thread-8,local:Thread-8:8
线程:Thread-9,local:Thread-9:9

从结果可以看到,每一个线程都有自己的local 值,这就是TheadLocal的基本使用 。

下面我们从源码的角度来分析一下,ThreadLocal的工作原理。


3.ThreadLocal源码分析

set 方法

/*** 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();//获取线程中变量 ThreadLocal.ThreadLocalMapThreadLocalMap map = getMap(t);//如果不为空,if (map != null)map.set(this, value);else//如果为空,初始化该线程对象的map变量,其中key 为当前的threadlocal 变量createMap(t, value);}/*** 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*/
//初始化线程内部变量 threadLocals ,key 为当前 threadlocalvoid 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);}static class Entry extends WeakReference> {/** The value associated with this ThreadLocal. */Object value;Entry(ThreadLocal k, Object v) {super(k);value = v;}}

汇总下,ThreadLocalMap 为 ThreadLocal 的一个静态内部类,里面定义了Entry 来保存数据。而且是继承的弱引用。在Entry内部使用ThreadLocal作为key,使用我们设置的value作为value。

对于每个线程内部有个ThreadLocal.ThreadLocalMap 变量,存取值的时候,也是从这个容器中来获取。

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

通过上面的分析,相信你对该方法已经有所理解了,首先获取当前线程,然后通过key threadlocal 获取 设置的value 。

4.避免ThreadLocal 内存泄漏问题

使用完ThreadLocal后,执行remove操作,避免出现内存溢出情况。

所以 如同 lock 的操作 最后要执行解锁操作一样,ThreadLocal使用完毕一定记得执行remove 方法,清除当前线程的数值。

如果不remove 当前线程对应的VALUE ,就会一直存在这个值。

使用了线程池,可以达到“线程复用”的效果。但是归还线程之前记得清除ThreadLocalMap,要不然再取出该线程的时候,ThreadLocal变量还会存在。这就不仅仅是内存泄露的问题了,整个业务逻辑都可能会出错。

相关内容

热门资讯

国信证券:企业年金政策将合规执... 证券之星消息,国信证券(002736)12月29日在投资者关系平台上答复投资者关心的问题。 投资者提...
灵宝综治中心调解一起跨度近两年... 大象新闻记者 许继彬 通讯员 王建敏 李婕霄 校园安全无小事,少年成长总关情。孩子在校园内的意外磕碰...
著名经济学家魏杰:“十五五”时... 封面新闻记者 欧阳宏宇 “推动制度型开放,形成高质量的开放新格局,是“十五五”时期开放的重点。”12...
子洲县市场监管局举办法律法规专... 12月24日,子洲县市场监督管理局举办法律法规专题培训会,邀请子洲县人民法院法官王斐、钟鹏程作专题授...
董毅智律师:小红书沦为诈骗“温... 12月23日,丽江市古城区文化和旅游局采取一项公开举措,向小红书平台发出公函,指出其未能有效履行平台...
衡水办学神话破灭!原因“政策”... 衡水办学神话破灭!原因“政策”! 撰文|@渤海公子 最近有一张图,很有意思。 它没被大肆转发,却在很...
欣旺达子公司涉买卖合同纠纷 动... 中证报中证网讯(记者 齐金钊)日前,锂电池行业头部企业欣旺达全资子公司欣旺达动力科技股份有限公司,因...
欣旺达被吉利系公司起诉索赔23... 12月26日,欣旺达发布公告,其子公司欣旺达动力被吉利控股集团旗下的核心三电企业威睿电动提起诉讼。威...
北斗定位厘米级精准 哈尔滨工程... 中新网哈尔滨12月29日电 (董昕瑶)28日,来自哈尔滨工程大学18个学院的1200余名师生铲雪塑形...
马杜罗:美国威胁委内瑞拉人民是... 新华社加拉加斯12月28日电(记者田睿 刘宇辰)委内瑞拉总统马杜罗28日表示,美国威胁委内瑞拉人民是...