ThreadLocal 详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThreadLocal 详解相关的知识,希望对你有一定的参考价值。

参考技术A ThreadLocal 是一个线程的内部存储类,对于被存储的对象,在不同的线程读取的变量是独立的。

实现原理是:对每一个线程都有一个ThreadLocalMap,ThreadLocal维护每个ThreadLocalMap中的值
ThreadLocalMap 内部是一个[]Enter, 不同的ThreadLocal都是存储在线程的同一个ThreadLocalMap中的,只是下标位置不同,
同一个ThreadLocal在不同线程的ThreadLocalMap中的下标值即索引值是相同的。

ThreadLocal 最常用的示例:

在主线程初始化ThreadLocal实例,在各个线程调用set、get,设置、获取存储在各个线程中的值

查看源码

当调用set函数时,会去获取当前线程的ThreadLocalMap对象,该对象是在Thread.java中申明,默认值为null。
当map为null时,则调用createMap,为threadLocals对象赋值,不为null,在调用ThreadLocalMap中的set函数,将值保存到数组中

当调用get方法时,获取当前线程的ThreadLocalMap对象,如果map不为null,则获取map持有的Entry对象,再返回该Entry对象持有的value值。
如果map为null或者获取的Enter对象为null,则会调用setInitialValue,而initialValue的返回值是null。
当map为null时,会调用createMap方法,实例化ThreadLocalMap

上面的set、get都会调用getMap方法,来获取当前线程的ThreadLocalMap实例

threadLocals 是在Thread.java中声明的,默认值为null,也就是说每个线程中都有这个对象,只是默认是null。

在set、get中都会对当前线程的ThreadLocalMap对象判断,当为null时,会调用createMap对ThreadLocalMap对象threadLocals赋值,

getEntry 函数就是获取key对应的节点Entry
在getEntry、set函数中可以看到value存储在[]Entry中的下标位置是由 key.threadLocalHashCode & (len-1)计算得出的。
就是ThreadLocal中的threadLocalHashCode 对[]Entry长度取模
getEntry,通过下标获取e,如果不为null而且再次校验key相等,则返回e
set时,e不为null,而且key相等,代表已存在,则替换e.value,
key不相等,代表不存在,而添加

当Entry[] 中存入的值数量已达到数组长度的3/4;
则会调用resize函数,调整Entry[]的长度,
将新数组长度*2,遍历老数组,
重新获取下标h,判断h处是否有值,无值填充,有值则重新获取h,再填充

ThreadLocalMap 的内部类

Entry继承WeakReference,说明ThreadLocal内部存储的类型都是采取弱引用累心存储,当GC时,则会被回收。
这样保证当线程执行完时,当前线程中存储在ThreadLocalMap中的对象会被回收,不会在此处出现内存泄漏

value是调用ThreadLocal保存的值,

ThreadLocal详解

ThreadLocal详解(二)

上一篇文章讲了ThreadLocal的get、set、resize等方法的源码,但是对于一些单独的方法例如cleanSomeSlots、expungeStaleEntry并没有讲述,这一节就是要讲述这些源码,在讲之前首先提几个ThreadLocal的注意事项,一般来说就是ThreadLocal如果在使用不当的情况下会出现内存泄漏的问题,其原因就在于如果在线程清除时没有清除ThreadLocal中的变量,因为ThreadLocal中的key是当前线程的一个弱引用,此时当当前线程结束后,该key就变为null了,此时ThreadLocalMap中就出现了key为null,但是value仍然有值的情况,此时就出现了内存泄露。

改进


为了解决因使用不当可能存在的内存泄漏问题,Doug Lea大师还是做了一些改进来尽可能的避免出现内存泄露,就是方法cleanSomeSlots、expungeStaleEntry、replaceStaleEntry,现在就来看看这三个方法做了哪些措施避免出现内存泄露问题。

replaceStaleEntry


原本准备先说cleanSomeSlots方法,但是在set操作源码中对于key的处理中,有replaceStaleEntry源码,因此先说replaceStaleEntry方法,先回到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();
        

上述for循环插入新值的操作中,最后的if判断是当value存在值,但是key为null时进行的value替换操作,这就是在hash碰撞时,遇到脏key的操作,进入到方法replaceStaleEntry中。

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

首先看第一个for循环方法。

int slotToExpunge = staleSlot;
for (int i = prevIndex(staleSlot, len);
       (e = tab[i]) != null;
       i = prevIndex(i, len))
       if (e.get() == null)
            slotToExpunge = i;

这个for循环上面有段注解,为:

// 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).

大致意思就是检查当前脏key的前后元素,这里认为脏key也是成聚集在一起的,如果检查到脏key,则前置slotToExpunge,最终检查至没有未遇到脏key为止。

同时,在下一段代码中,该for循环的操作,就是填充这些脏key了,可以看看源码。

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

这里做了一个事情,就是向后环形查找过程中发现key相同的entry就覆盖并且和脏entry进行交换。

if (slotToExpunge == staleSlot)
                        slotToExpunge = i;
 cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);

上述代码,如果没有查找到脏key,就以当前位置作为起点执行cleanSomeSlots方法。

expungeStaleEntry


在看cleanSomeSlots方法前先看expungeStaleEntry方法,这个方法就比较容易理解了,就是清除脏key。

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

将当前节点key与value均置为null,便于当前位置放置新的entry,同时该方法还会做一个操作就是往后寻找直到tab[i]为null为止,如果在搜索过程中继续遇到脏key,则继续执行清除操作。

cleanSomeSlots


现在回头来看cleanSomeSlots方法。

/**
         * Heuristically scan some cells looking for stale entries.
         * This is invoked when either a new element is added, or
         * another stale one has been expunged. It performs a
         * logarithmic number of scans, as a balance between no
         * scanning (fast but retains garbage) and a number of scans
         * proportional to number of elements, that would find all
         * garbage but would cause some insertions to take O(n) time.
         *
         * @param i a position known NOT to hold a stale entry. The
         * scan starts at the element after i.
         *
         * @param n scan control: @code log2(n) cells are scanned,
         * unless a stale entry is found, in which case
         * @code log2(table.length)-1 additional cells are scanned.
         * When called from insertions, this parameter is the number
         * of elements, but when from replaceStaleEntry, it is the
         * table length. (Note: all this could be changed to be either
         * more or less aggressive by weighting n instead of just
         * using straight log n. But this version is simple, fast, and
         * seems to work well.)
         *
         * @return true if any stale entries have been removed.
         */
        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);
            return removed;
        

cleanSomeSlots操作就比较简单了,cleanSomeSlots中n这个参数就是当前tab的长度,然后while循环中进行n >>>= 1,就是一个log2(n),就行log2(n)次循环后,当前清理脏key就结束了。

综上


threadLocal这一套清理脏key还是挺复杂的,另外也和我们平常认识的有些不同,平常在用时一直以为此处没有脏key的处理操作,但现在知道前人对threadLocal还是做了许多优化,而且对以上脏key的处理操作中,并非是能将所有脏key都清理掉,而是在性能和脏key处理上做了一定的折中的选择,因此在自己平时使用时仍然要避免脏key的出现。

以上是关于ThreadLocal 详解的主要内容,如果未能解决你的问题,请参考以下文章

ThreadLocal用法详解和原理

ThreadLocal类详解

ThreadLocal 详解

深入浅出多线程编程实战ThreadLocal详解(介绍使用原理应用场景)

深入浅出多线程编程实战ThreadLocal详解(介绍使用原理应用场景)

详解ThreadLocal原理及内存泄漏