ThreadLocal 简单分析

本文详细解析了ThreadLocal的工作原理,包括其如何实现线程变量的私有化,解决并发环境下的变量隔离问题。同时,探讨了ThreadLocalMap的结构及解决Hash冲突的方法,并警示了可能的内存泄露风险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

ThreadLocal 从字面上理解“本地线程”???其实不是!ThreadLocal 的作用主要是用来让线程间的变量私有化,也就是说当前Thread中的变量只能在当前的Thread中使用 ,Treadlocal 在搞并发的情况下 可以无状态的使用 适合在没个Thread 需要依赖单独的变量情况下使用

Threadlocal 是如何保证 变量只在当前线程可用的?

每个Thread线程内部都有一个Map。

Map里面存储线程本地对象(key)和线程的变量副本(value)

但是,Thread内部的Map是由ThreadLocal维护的,由ThreadLocal负责向map获取和设置线程的变量值。

所以对于不同的线程,每次获取副本值时,别的线程并不能获取到当前线程的副本值,形成了副本的隔离,互不干扰。

Threadlocalmap 是Thread的一个成员变量 

ThreadLocal中的几个主要方法如下:

  1. get()方法用于获取当前线程的副本变量值。
  2. set()方法用于保存当前线程的副本变量值。
  3. initialValue()为当前线程初始副本变量值。
  4. remove()方法移除当前前程的副本变量值。

源代码如下

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

 

既然TheadLoal 管理一个map那么一定会出现hash 冲突 那么Hash冲突怎么解决?

和HashMap的最大的不同在于,ThreadLocalMap结构非常简单,没有next引用,也就是说ThreadLocalMap中解决Hash冲突的方式并非链表的方式,而是采用线性探测的方式,所谓线性探测,就是根据初始key的hashcode值确定元素在table数组中的位置,如果发现这个位置上已经有其他key值的元素被占用,则利用固定的算法寻找一定步长的下个位置,依次判断,直至找到能够存放的位置。

ThreadLocalMap解决Hash冲突的方式就是简单的步长加1或减1,寻找下一个相邻的位置。


ThreadLocal 的坑! 内存泄露!

由于ThreadLocalMap的key是弱引用,而Value是强引用。这就导致了一个问题,ThreadLocal在没有外部对象强引用时,发生GC时弱引用Key会被回收,而Value不会回收,如果创建ThreadLocal的线程一直持续运行,那么这个Entry对象中的value就有可能一直得不到回收,发生内存泄露。

既然Key是弱引用,那么我们要做的事,就是在调用ThreadLocal的get()、set()方法时完成后再调用remove方法,将Entry节点和Map的引用关系移除,这样整个Entry对象在GC Roots分析后就变成不可达了,下次GC的时候就可以被回收。

如果使用ThreadLocal的set方法之后,没有显示的调用remove方法,就有可能发生内存泄露,所以养成良好的编程习惯十分重要,使用完ThreadLocal之后,记得调用remove方法。

 

 

ThreadLocal是Java中的一个线程本地变量,它提供了一种简单的解决多线程并发访问共享变量的方案。每个ThreadLocal对象维护了一个独立的变量副本,每个线程都可以访问自己的变量副本,从而避免了多线程之间对同一变量的竞争。 ThreadLocal的原理可以简单概括为以下几个步骤: 1. 每个Thread对象内部都有一个ThreadLocalMap对象,ThreadLocalMap是ThreadLocal的核心数据结构,用于存储每个ThreadLocal对象对应的变量副本。 2. 在ThreadLocal对象中调用set()方法时,ThreadLocal首先获取当前线程的ThreadLocalMap对象,然后将当前ThreadLocal对象作为key,将要存储的变量副本作为value,存储到ThreadLocalMap中。 3. 在ThreadLocal对象中调用get()方法时,ThreadLocal首先获取当前线程的ThreadLocalMap对象,然后以当前ThreadLocal对象作为key获取对应的变量副本,从而实现了多线程之间的变量隔离。 4. 在ThreadLocal对象中调用remove()方法时,ThreadLocal首先获取当前线程的ThreadLocalMap对象,然后将当前ThreadLocal对象从ThreadLocalMap中删除。 需要注意的是,由于ThreadLocalMap是存储在线程中的,因此需要注意内存泄漏的问题。如果在ThreadLocal对象使用结束后没有调用remove()方法,会导致ThreadLocal对象一直存在于ThreadLocalMap中,从而引起内存泄漏。因此,在使用ThreadLocal对象时,需要注意及时调用remove()方法,以避免内存泄漏问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值