深入剖析ThreadLocal

ThreadLocal 是什么?

ThreadLocal 是一个线程的工具类,主要用于存储一些线程的共享变量,各个线程之间互不影响,在多线程及高并发环境下,可以实现无状态的存储及调用

ThreadLocal的原理

好久以前。我一直以为ThreadLocal 可能就是一个Map,以Thead ID为key,然后往里面设置Value即可,但实际上JDK里的ThreadLocal 却没有这样子实现。细想,这么实现其实会带来两个问题

  1. 多线程下,频繁对Map 进行读取及设值,必然要加锁,那效率可想而知。
  2. 线程退出后,如果不对Map进行清除,那值一直会存在,随着数量的增多,必然造成内存泄露。

那JDK中ThreadLocal 是怎么实现的呢?

我们从ThreadLocal.set(T value)方法开始进入看看

    /**
     * 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 这个对象设值,那这个对象哪里来的呢?我们接着跟踪看看

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

原来这个ThreadLocalMap 居然是 Thread的一个局部变量,且该 变量的初始值为空

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

我们再来详细看看ThreadLocalMap 这个类

 原来ThreadLocalMap 居然是ThreadLocal下的一个静态内部类。主要的成员是

       /**
         * The table, resized as necessary.
         * table.length MUST always be a power of two.
         */
        private Entry[] table;   //用来保存存进去的Value

而Entry 有点类似于Map Entry 也是一个键值对。我们来看看 Entry的定义

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

Entry 继承了弱引用(为什么要弱引用呢,下面会解释,先hold 住),很明显 k 就是 ThreadLocal 对象,而Value 就是我们即将要保存的共享变量。

到了这里,我们基本上对ThreadLocal的存储有一个清晰的认识了,首先ThreadLocal的值不是保存在一个Map中的,他是保存在当前Thread的ThreadLocaoMap 上的Entry数组上了,其中Entry继承弱引用,而Value 就保存在Entry的value 局部变量上。

我们来思考一下,这么实现带来了哪些好处。

  1. value 保存在了线程自己的局部变量上,就避免了多线程对同一个存储对象频繁进行操作的冲突,提高了效率
  2. 其次当线程被销毁时,Entry.value 失去了强引用,必然也会被回收,降低了内存泄露的风险。

接下来我们继续分析一下ThreadLocal.set(T value)方法

当要设值的时候其实调用的是ThreadLocalMap 的set(T value)方法,我们看看它的实现

 /**
         * 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) {
                    //已经存在的key 直接更新value
                    e.value = value;
                    return;
                }

                if (k == null) {
                    //k 为空有可能是原来存储的被回收了,要清除,防止内存泄露
                    // 同时将k设置为key value 设置为value
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }

            //获得了存储下标,新生成一个Entry 存进去
            tab[i] = new Entry(key, value);
            int sz = ++size;
            //扩容,及清除一些key null的entry
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }

我们再来看看两个蛮有意思的方法

      /**
         *  寻找一些过期的key ,将其替换成要设置的key 及value
         *   
         */
        private void replaceStaleEntry(ThreadLocal<?> key, Object value,
                                       int staleSlot) {
            Entry[] tab = table;
            int len = tab.length;
            Entry e;

            // 往前寻找过期的slot
            int slotToExpunge = staleSlot;
            for (int i = prevIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = prevIndex(i, len))
                if (e.get() == null)
                    slotToExpunge = i;

            // 往后搜索为空的key
            for (int i = nextIndex(staleSlot, len);
                 (e = tab[i]) != null;
                 i = nextIndex(i, len)) {
                ThreadLocal<?> k = e.get();

                // 找到了key 将value 替换
                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;
                }

                // 用于后面清除过期的slot
                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);

            //  清除一些过期的slot
            if (slotToExpunge != staleSlot)
                cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
        }
   // 清除一些过期的slot 类型与二分法
   private boolean cleanSomeSlots(int i, int n) {
            boolean removed = false;
            Entry[] tab = table;
            int len = tab.length;
            do {
                i = nextIndex(i, len);
                Entry e = tab[i];
                if (e != null && e.get() == null) {
                    n = len;
                    removed = true;
                    i = expungeStaleEntry(i); //清除
                }
            } while ( (n >>>= 1) != 0);  // n = n / 2
            return removed;
        }

以上就是对ThreadLocalMap.set(T value)的解读。接下来我们看看ThreadLocal.get()方法

    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.getEntry(ThreadLocal<> key )

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

我们再来看看ThreadLocal的remove 方法

先找到ThreadLocalMap 对象

   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) {
                    // 找到了key 对ThreadLocal弱引用 进行claar
                    // 这里为什么不顺手把value也置为null呢???
                    e.clear();
                    expungeStaleEntry(i);
                    return;
                }
            }
        }

很显然,清除操作只是对ThreadLocalMap的Entry对象中的弱引用clear而已。没有对value置空。这一点在使用中是要引起注意的。

至于为什么不对value置为null,我的理解是这样的。在ThreadLocal.set(T value) 及ThreadLocal.get()两个方法,都会触发清除一些过期的Entry。当中就会对value 置为null..所以这里没有重复操作。可见下列方法

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

一些思考

为什么Entry 要继承WeakReference(当没有强应用时,若发生gc弱引用一定会被回收)?

盗用一张图

从图中可以看出。Key 对ThreadLocal的应用是弱引用。假设该引用是强引用,那么当我们new 的ThreadLocal对象的引用被置为null时,堆中真正的ThreaLocal 依然不会被回收,造成内存泄露,因为我们的key保持了对ThreadLocal的强引用。所以强引用不合理,当为弱应用时,发生GC时ThreadLocal在堆中的东西将会被回收。

为什么不将vlaue 也设置为弱引用

将value 设置为弱引用,当该value没有了强引用,则value会被回收。这时候我们通过ThreadLocal.get()出来的东西是空的,没办法满足存储共享变量的目的。 

线程池使用ThreadLocal 有什么需要注意的

  1. 最好再remove之前将value 置为null,解除引用,因为ThreadLocal的定时清除,很有可能会有漏网之鱼
  2. 结束任务记得remove.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值