关于后端:线程私有变量ThreadLocal详解

37次阅读

共计 6038 个字符,预计需要花费 16 分钟才能阅读完成。

本文已收录至 Github,举荐浏览 👉 Java 随想录

微信公众号:Java 随想录

CSDN:码农 BookSea

烈火试真金,顺境试强人。——塞内加

什么是 ThreadLocal

首先看下 ThreadLocal 的应用示例:

public class ThreadLocalTest {private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
 
    public static void main(String[] args) {Thread thread1 = new Thread(() -> {threadLocal.set("本地变量 1");
            print("thread1");
            System.out.println("线程 1 的本地变量的值为:"+threadLocal.get());
        });
 
        Thread thread2 = new Thread(() -> {threadLocal.set("本地变量 2");
            print("thread2");
            System.out.println("线程 2 的本地变量的值为:"+threadLocal.get());
        });
 
        thread1.start();
        thread2.start();}
 
    public static void print(String s){System.out.println(s+":"+threadLocal.get());
     
    }

执行后果如下

咱们从 Thread 类讲起,在 Thread 类中有保护两个 ThreadLocal.ThreadLocalMap 对象,别离是:threadLocalsinheritableThreadLocals

/* ThreadLocal values pertaining to this thread. This map is maintained
 * by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;

/*
 * InheritableThreadLocal values pertaining to this thread. This map is
 * maintained by the InheritableThreadLocal class.
 */
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;

初始它们都为 null,只有在调用 ThreadLocal 类的 set 或 get 时才创立它们。ThreadLocalMap 能够了解为线程公有的 HashMap。

ThreadLoalMap 是 ThreadLocal 中的一个动态外部类,相似 HashMap 的数据结构,但并没有实现 Map 接口。

ThreadLoalMap 中初始化了一个 大小 16 的 Entry 数组,Entry 对象用来保留每一个 key-value 键值对。key 是 ThreadLocal 对象。

Entry 用来保留数据,而且还是继承的弱援用。在 Entry 外部应用 ThreadLocal 作为 key,应用咱们设置的 value 作为 value。

ThreadLocal 原理

set()办法

当咱们调用 ThreadLocal 的 set() 办法时理论是调用了以后线程的 ThreadLocalMap 的 set() 办法。ThreadLocal 的 set() 办法中,会进一步调用 Thread.currentThread() 取得以后线程对象,而后获取到以后线程对象的 ThreadLocal,判断是不是为空,为空就先调用creadMap() 创立再 set(value) 创立 ThreadLocalMap 对象并增加变量。不为空就间接set(value)

这种保障线程平安的形式称为 线程关闭。线程只能看到本人的 ThreadLocal 变量。线程之间是相互隔离的。

get()办法

其中 get() 办法 用来获取与以后线程关联的 ThreadLocal 的值,如果以后线程没有该 ThreadLocal 的值,则调用 initialValue 函数 获取初始值返回,所以个别咱们应用时须要继承该函数,给出初始值(不重写的话默认返回 Null)。

次要有以下几步:

  1. 获取以后的 Thread 对象,通过 getMap 获取 Thread 内的 ThreadLocalMap
  2. 如果 map 曾经存在,以以后的 ThreadLocal 为键,获取 Entry 对象,并从从 Entry 中取出值
  3. 否则,调用 setInitialValue 进行初始化。
/**
 * 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();}

咱们能够重写initialValue(),设置初始值。

    private static final ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {return Integer.valueOf(0);
        }
    }

remove()办法

最初一个须要探索的就是 remove 办法,它用于在 map 中移除一个不必的 Entry。也是先计算出 hash 值,若是第一次没有命中,就循环直到 null,在此过程中也会调用 expungeStaleEntry 革除空 key 节点。代码如下:

public void remove() {ThreadLocalMap m = getMap(Thread.currentThread());
         if (m != null)
             m.remove(this);
}


/**
 * 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 中应用的 key 为 ThreadLocal 的弱援用,弱援用的特点是,如果这个对象只存在弱援用,那么在下一次垃圾回收的时候必然会被清理掉。

所以如果 ThreadLocal 没有被内部强援用的状况下,在垃圾回收的时候会被清理掉的,这样一来 ThreadLocalMap 中应用这个 ThreadLocal 的 key 也会被清理掉。然而,value 是强援用,不会被清理,这样一来就会呈现 key 为 null 的 value。呈现内存透露的问题。

在执行 ThreadLocal 的 set、remove、rehash 等办法时,它都会扫描 key 为 null 的 Entry,如果发现某个 Entry 的 key 为 null,则代表它所对应的 value 也没有作用了,所以它就会把对应的 value 置为 null,这样,value 对象就能够被失常回收了。然而假如 ThreadLocal 曾经不被应用了,那么实际上 set、remove、rehash 办法也不会被调用,与此同时,如果这个线程又始终存活、不终止的话,那么方才的那个调用链就始终存在,也就导致了 value 的内存透露

ThreadLocal 的 Hash 算法

ThreadLocalMap相似 HashMap,它有本人的 Hash 算法。

private final int threadLocalHashCode = nextHashCode();

private static final int HASH_INCREMENT = 0x61c88647;
  
private static int nextHashCode() {return nextHashCode.getAndAdd(HASH_INCREMENT);
}
    
public final int getAndAdd(int delta) {return unsafe.getAndAddInt(this, valueOffset, delta);
}

HASH_INCREMENT这个数字被称为 斐波那契数 也叫 黄金分割数 ,带来的益处就是 hash 散布十分平均

每当创立一个 ThreadLocal 对象,这个ThreadLocal.nextHashCode 这个值就会增长 0x61c88647

讲到 Hash 就会波及到 Hash 抵触,跟 HashMap 通过 链地址法 不同的是,ThreadLocal 是通过 线性探测法 / 凋谢地址法 来解决 hash 抵触。

ThreadLocal 1.7 和 1.8 的区别

ThreadLocal 1.7 版本的时候,entry 对象的 key 是 Thread。

1.8 版本 entry 的 key 是 ThreadLocal。

1.8 版本的益处:当 Thread 销毁的时候,ThreadLocalMap 也会随之销毁,缩小内存的应用。因为 ThreadLocalMap 是在 Thread 外面的,所以只有 Thread 隐没了,那 ThreadLocalMap 就不复存在了。

ThreadLocal 的问题

ThreadLocal 内存泄露问题

在 ThreadLocalMap 中的 Entry 的 key 是对 ThreadLocal 的 WeakReference 弱援用,而 value 是强援用。当 ThreadLocalMap 的某 ThreadLocal 对象只被弱援用,GC 产生时该对象会被清理,此时 key 为 null,但 value 为强援用不会被清理。此时 value 将拜访不到也不被清理掉就可能会导致内存透露。

留神构造函数里的第一行代码 super(k),这意味着 ThreadLocal 对象是一个弱援用

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

因而咱们应用完 ThreadLocal 后最好手动调用 remove() 办法。但其实在 ThreadLocalMap 的实现中以及思考到这种状况,因而在调用 set()get()remove() 办法时,会清理 key 为 null 的记录。

为什么应用弱援用而不是强援用?

为什么采纳了弱援用的实现而不是强援用呢?

正文上有这么一段话:为了帮助解决数据比拟大并且生命周期比拟长的场景,hash table 的条目应用了 WeakReference 作为 key

所以,弱援用反而是为了解决内存存储问题而专门应用的。

实际上,采纳弱援用反而多了一层保障,ThreadLocal 被清理后 key 为 null,对应的 value 在下一次 ThreadLocalMap 调用 set、get,就算遗记调用 remove 办法,弱援用比强援用能够多一层保障。

所以,内存泄露的根本原因是是否手动革除操作,而不是弱援用。

ThreadLocal 父子线程继承

异步场景下无奈给子线程共享父线程的线程正本数据,能够通过 InheritableThreadLocal 类解决这个问题。

它的原理就是子线程是通过在父线程中调用 new Thread() 创立的,在 Thread 的构造方法中调用了 Thread 的 init 办法,在 init 办法中父线程数据会复制到子线程(ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);)。

代码示例:

public class InheritableThreadLocalDemo {public static void main(String[] args) {ThreadLocal<String> threadLocal = new ThreadLocal<>();
        ThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
        threadLocal.set("父类数据:threadLocal");
        inheritableThreadLocal.set("父类数据:inheritableThreadLocal");

        new Thread(new Runnable() {
            @Override
            public void run() {System.out.println("子线程获取父类 threadLocal 数据:" + threadLocal.get());
                System.out.println("子线程获取父类 inheritableThreadLocal 数据:" +inheritableThreadLocal.get());
            }
        }).start();}
}

然而咱们做异步解决都是应用线程池,线程池会复用线程会导致问题呈现。咱们能够应用阿里巴巴的 TTL 解决这个问题。

https://github.com/alibaba/tr…


如果本篇博客有任何谬误和倡议,欢送给我留言斧正。文章继续更新,能够关注公众号第一工夫浏览。

正文完
 0