本文已收录至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...


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