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);
        else
            createMap(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;
            else
                return 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);
                else
                    i = 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);
        else
            createMap(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 first
            for (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 exists
                    if (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 slot
            tab[staleSlot].value = null;
            tab[staleSlot] = new Entry(key, value);

            // If there are any other stale entries in run, expunge them
            if (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 staleSlot
            tab[staleSlot].value = null;
            tab[staleSlot] = null;
            size--;

            // Rehash until we encounter null
            Entry 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值