JAVA并发编程--5 理解ThreadLocal

前言:多线程中,如果每个线程都想储存一份变量数据,从而使得改数据在一个线程的生存时间内都可以被访问并修改,当线程结束后释放掉改数据;如场景:当需要获取到当前线程中对应的用户信息,以此方便记录用户的操作日志;那么java 中有什么可以方便我们进行这种业务的实现呢?

1 ThreadLocal 背景:
ThreadLocal,线程局部变量副本,每个线程都拥有自己的变量副本,使得我们可以在高并发的时候做到,线程之间副本数据的隔离性;在高并发场景下,可以实现无状态的调用,特别适用于各个线程依赖不通的变量值完成操作的场景。

2 ThreadLocal 使用:
2.1 声明ThreadLocal :

 final static ThreadLocal<String> threadLocal1 = new ThreadLocal<>();

2.2 使用时进行set:

   threadLocal1.set("张三");

2.3 使用完成进remove:

  threadLocal1.remove();

3 ThreadLocal 原理:
ThreadLocal 为什么可以实现不同线程之间的数据隔离性呢,既然ThreadLocal 常用的只有set(),get(),remove(),那么来看下这3个方法都做了什么事情:

3,1 set():
ThreadLocal


 public void set(T value) {
 		// 获取当前线程
        Thread t = Thread.currentThread();
        // 获取当前线程的 ThreadLocalMap  属性
        ThreadLocalMap map = getMap(t);
        if (map != null)
        	// 如果当前线程的ThreadLocalMap  不为空则直接进行对map 进行key-value 的设置
            map.set(this, value);
        else
           // 如果当前线程的ThreadLocalMap 为空,则进行初始化,并进行进行key-value 的设置
            createMap(t, value);
    }

第一次先进行初始化并设置entry,createMap(t, value):

void createMap(Thread t, T firstValue) {
		// 初始一个ThreadLocalMap 并把他赋值到 Thread 的threadLocals 属性中
		// Thread  类中的 ThreadLocal.ThreadLocalMap threadLocals = null 属性;
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }
 

通过上面的set(T value) 和 createMap(t, value) 可以得到,当主线程中 使用 threadLocal1.set(“张三”); 时,会先判断当前主线程的 的ThreadLocalMap 属性是否为空,如果为空则创建一个新的ThreadLocalMap 并且赋值给当前主线程中的threadLocals 属性;在来看下当主线程的 的ThreadLocalMap 属性为空时 是怎么创建 ThreadLocalMap entry 对象,已经怎么将 threadLocal1.set(“张三”) 进行存入的:

new ThreadLocalMap(this, firstValue):
创建ThreadLocalMap,注意此处传入的key 是 ThreadLocal 也即 我们定义的 final static ThreadLocal<String> threadLocal1 = new ThreadLocal<>() ;threadLocal1.set("张三"); 时的 threadLocal1 对象

 ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
			//  初始化entry 的长度  private static final int INITIAL_CAPACITY = 16;
            table = new Entry[INITIAL_CAPACITY];
            // 计算当前的ThreadLocal 所在数组下边位置
            int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
            // 当前数组位置赋值
            table[i] = new Entry(firstKey, firstValue);
            // 初始化 table 的size 为1
            size = 1;
            //  设置负载因子    threshold = len * 2 / 3;
            setThreshold(INITIAL_CAPACITY);
        }  

可以看到 这里先初始化了 table 数组,而table 数组是ThreadLocalMap 类中的一个属性 private Entry[] table; 这个数组中的每个元素都是 entry 对象 即key- value; 这里的key 就是 threadLocal1.set("张三");的 threadLocal1 对象,获取改对象的hashcode 值并且与数组长度-1 做 与运算 得出threadLocal1 在数组的一个下标,然后 通过 new Entry(firstKey, firstValue) 构建一个entry 对象 并放入到数组中;

通过以上初始化ThreadLocalMap 对象可以看出,这个 ThreadLocalMap 对象是和主线程绑定的,因为其创建好后 会给赋予到 ThreadLocal.ThreadLocalMap threadLocals 属性中;接下来看下 Entry 对象的构建

new Entry(firstKey, firstValue):

static class Entry extends WeakReference<ThreadLocal<?>> {
            /** The value associated with this ThreadLocal. */
            Object value;
			
            Entry(ThreadLocal<?> k, Object v) {
                super(k);
                value = v;
            }
        }

super(k):

public class WeakReference<T> extends Reference<T> {

    /**
     * Creates a new weak reference that refers to the given object.  The new
     * reference is not registered with any queue.
     *
     * @param referent object the new weak reference will refer to
     */
    public WeakReference(T referent) {
        super(referent);
    }

    /**
     * Creates a new weak reference that refers to the given object and is
     * registered with the given queue.
     *
     * @param referent object the new weak reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     */
    public WeakReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }

}

map.set(this, value):
如果已经进行过ThreadLocalMap的初始化,则直接进行设置 :这里传入的key 就是 threadLocal1.set("张三");的 threadLocal1 对象

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.
			// 获取到当前 ThreadLocalMap的entry 数组
            Entry[] tab = table;
            //  获取到当前 ThreadLocalMap的entry 数组 长度
            int len = tab.length;
            //  计算当前ThreadLocal 所在 ThreadLocalMap的entry的数组下标
            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) {
                	// 如果改key 已经过期,则替换和清除过期 的key
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }
			// 如果当前数组中没有则新建entry
            tab[i] = new Entry(key, value);
            int sz = ++size;
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }

从 map.set(this, value) 可以看出,这里直接将 threadLocal1.set(“张三”);的 threadLocal1 对象 进行hash 然后 与运算 得出 table 下标的位置,而这个 table 就是 当前主线程 下 ThreadLocal.ThreadLocalMap threadLocals 属性中 中的table 属性;最后进行线性探索 找到在table 中存入下标,将 threadLocal1 作为key ,"张三’ 作为value 构建entry 对象 然后进行存储

3,2 get():

public T get() {
		// 获取当前线程
        Thread t = Thread.currentThread();
        // 获取当前现成的ThreadLocalMap 属性
        ThreadLocalMap map = getMap(t);
        if (map != null) {
        	// 获取 ThreadLocalMap的对应数组下的entry
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                @SuppressWarnings("unchecked")
                // 获取对应的value 并返回
                T result = (T)e.value;
                return result;
            }
        }
        // 如果ThreadLocalMap 为空则对到当前线程的ThreadLocalMap 初始化化并返回null
        return setInitialValue();
    }

getMap(t):

 ThreadLocalMap getMap(Thread t) {
		// 防护当前先到的threadLocals属性
        return t.threadLocals;
    }

map.getEntry(this):

 private Entry getEntry(ThreadLocal<?> key) {
 			// 更加当前的ThreadLocal 计算所在当前线程ThreadLocalMap entry 数组的位置
            int i = key.threadLocalHashCode & (table.length - 1);
            // 获取entry
            Entry e = table[i];
            if (e != null && e.get() == key)
            	// 再次判断如果key 相同则至直接返回entry
                return e;
            else
            	// 如果找不到或者 key 不相同,则遍历向下办理尝试去获取entry
                return getEntryAfterMiss(key, i, e);
        }

getEntryAfterMiss:

  private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
            Entry[] tab = table;
            int len = tab.length;

            while (e != null) {
            	// 获取key
                ThreadLocal<?> k = e.get();
                if (k == key)
                    return e;
                if (k == null)
                    //  清除过期key
                    expungeStaleEntry(i);
                else
                    i = nextIndex(i, len);
                e = tab[i];
            }
            return null;
        }

如果ThreadLocalMap 为空则对到当前线程的ThreadLocalMap 初始化化并返回null,setInitialValue:

private T setInitialValue() {
		// 初始化value 为null protected T initialValue() {
        // return null;
      // }
        T value = initialValue();
       
        Thread t = Thread.currentThread();
         // 获取当前线程的ThreadLocalMap
        ThreadLocalMap map = getMap(t);
        if (map != null)
        	// ThreadLocalMap 不为null 则设置对应的key - value
            map.set(this, value);
        else
        	// ThreadLocalMap 为null 初始化 ThreadLocalMap
            createMap(t, value);
        return value;
    }

通过上面的 get() 我们可以知晓 ,在主线程中threadLocal1.get() 时 ,先根据当前主线程获取其下面的 ThreadLocalMap 属性,然后以threadLocal1 对象作为key 去遍历 ThreadLocalMap 中的table 数组,进行key的比对,然后获取其对应的value 值;

3.3 remove():

public void remove() {
		// 获取当前线程的 ThreadLocalMap 
         ThreadLocalMap m = getMap(Thread.currentThread());
         if (m != null)
         	// 移除当前的线程的ThreadLocal entry
             m.remove(this);
     }

remove() 获取当前主线中的 ThreadLocalMap 属性,然后 通过ThreadLocalMap 进行移除:注意这里 m.remove(this) 的this 传人的是 threadLocal1.set("张三");的 threadLocal1 对象

/**
* Remove the entry for key.
   */
  private void remove(ThreadLocal<?> key) {
  		// 获取主线程的 ThreadLocalMap  的 table 数组
      Entry[] tab = table;
      int len = tab.length;
      // 根据 threadLocal1 对象的key 获取下标,遍历数组获取threadLocal1 对应的entry 对象
      // 调用e.clear() 将引用置空 public void clear() { this.referent = null;  }
      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;
          }
      }
  }

tip:thread local map底层的Entry数组只会扩容,不会缩容;

通过上面 对ThreadLocal 源码的解读 可以发现,我们在进行 final static ThreadLocal threadLocal1 = new ThreadLocal<>();
然后使用threadLocal1 进行get ,set 和remove 时,实际上对是在对 主线程 Thread 下的ThreadLocalMap 中的table[] 数组在操作,即时我们 定义了多个 final static ThreadLocal threadLocalxxx = new ThreadLocal<>(); 如果在同一个方法中进行 threadLocal1,threadLocal2,threadLocal3, get ,set 和remove 方法是,实际上操作的是 主线程 Thread 下同一个 ThreadLocalMap 中的table[] 数组

4 ThreadLocal 为什么会造成内存泄露:
内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果。
在这里插入图片描述

ThreadLocalMap 是Thread 中维护的一个属性,其生命周期是跟着Thread 的,如果一个Thread 从创建,使用,到销毁 耗时很短,在Thread 销毁时占用的空间都会被释放,则没有问题;但是Thread 是一种宝贵的资源不可能被频繁的创建和销毁,尤其现在程序中大多数资源都使用了线程池技术,这就导致了一个Thread 的生命周期会变得很长,而Thread 的ThreadLocalMap 又是一个只能扩容不会缩容的容器,那么如果ThreadLocalMap 中的key和value 的引用一直存在,就导致对应的数据堆中无法被jvm 进行回收,从而使的内存被一点点耗尽。
所以基于此就需要对ThreadLocalMap 中无效的key-value 及时的清除,来确保没有强引用一直存在,从而使得jvm及时的进行垃圾回收;
那么基于此我们就要做到:
(1)将ThreadLocal 定义为 final static 避免ThreadLocal 被频繁的创建,创建对象少了jvm 的压力也随之减轻,声明为static,这样就一直存在ThreadLocal的强引用,也就能保证任何时候都能通过ThreadLocal的弱引用访问到Entry的value值,进而清除掉 ;
(2)每次使用完ThreadLocal都调用它的remove()方法清除数据,清除对象的强引用方便jvm 回收;

5 总结:
5.1 ThreadLocal 是通过维护每个Thread 中的ThreadLocalMap来实现不同Thread 可以获取到各自ThreadLocalMap的数据从而实现数据的隔离性;
5.2 :ThreadLocalMap 底层的Entry数组只会扩容,不会缩容;
5.4 :ThreadLocal 在使用完毕后及时调用remove() 方法让其释放掉对应的key和value;

6 扩展:
6.1 既然 ThreadLocal 是通过维护每个Thread 中的ThreadLocalMap来实现数隔离,那么为什么不将ThreadLocalMap 直接放入到Thread 类中而是放到ThreadLocal 中?
将ThreadLocalMap定义在Thread类内部看起来更符合逻辑,但是ThreadLocalMap并不需要Thread对象来操作,所以定义在Thread类内只会增加一些不必要的开销。定义在ThreadLocal类中的原因是ThreadLocal类负责ThreadLocalMap的创建,仅当线程中设置第一个ThreadLocal时,才为当前线程创建ThreadLocalMap,之后所有其他ThreadLocal变量将使用一个ThreadLocalMap。
总的来说就是,ThreadLocalMap不是必需品,定义在Thread中增加了成本,定义在ThreadLocal中按需创建。

6.2 ThreadLocalMap 中的key 为什么要使用弱引用:
ThreadLocalMap 中的key 使用弱引用是为了避免内存泄露:

ublic class Test {
    public static void main(String[] args) {
        ThreadLocal threadLocal = new ThreadLocal();
        threadLocal.set(new Object());
        threadLocal = null;
    }

创建一个ThreadLocal对象,并设置一个Object对象,然后将其置空,如果key 不是弱引用的话,ThreadLocalMap中的key 就会有一个强引用指向ThreadLocal,如果不调用ThreadLocal 的remove()方法,GC 的时候发现key一直有强引用存在从而无法访问,但时间上改ThreadLocal 程序已经使用完了,永远也不可能在被程序用到,但是它依然还在占用空间;
当Key是弱引用时,threadLocal由于外部没有强引用了,GC可以将其回收,ThreadLocal通过key.get()==null可以判断Key已经被回收了,当前Entry是一个废弃的过期节点,因此ThreadLocal可以自发的清理这些过期节点,来避免「内存泄漏」。

6.3 既然每个Thread 都维护自己的变量数据为什么不直接使用 object:value 作为属性,而是要使用ThreadLocalMap 存储?
使用ThreadLocalMap 是因为一个线程中我们可以填充多个ThreadLocal ,如:

public class ThreadLocalTest {
    final static ThreadLocal<String> threadLocal1 = new ThreadLocal<>();
    final static ThreadLocal<String> threadLocal2 = new ThreadLocal<>();
    final static ThreadLocal<String> threadLocal3 = new ThreadLocal<>();


    public static void main(String[] args) throws NoSuchFieldException {
        Thread t = Thread.currentThread();
        threadLocal1.set("张三");
        threadLocal2.set("李四");
        threadLocal3.set("王五");

        Class c = t.getClass();
        Field field = c.getDeclaredField("threadLocals");
        field.setAccessible(true);
        String fieldName = field.getName();
        Object fieldValue = null;
        try {
            fieldValue = field.get(t);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        // 下面一行打上断点,此时可以看threadLocals 属性中的值
        threadLocal1.remove();
        threadLocal2.remove();
        threadLocal3.remove();
        try {
            fieldValue = field.get(t);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        int a =10;

    }


}

在进行remove();之前我们可以看到ThreadLocalMap 存储了张三,李四 和王五:
在这里插入图片描述
可以看到threadLocals 中存放的是 ThreadLocalMap ,而ThreadLocalMap 存放的是 table[] 数组,table[] 数组中的每个key 就是 threadLocal1,threadLocal2,threadLocal3 对象,value 对应 threadLocal1,threadLocal2,threadLocal3的 各个值;

在进行 remove();之后可以发现 ThreadLocalMap 中没有了张三,李四 和王五:
在这里插入图片描述
6.4 线性探索:
解决ThreadLocal ,hash冲突的一种策略,虽然ThreadLocal使用了很牛逼的办法来生成hashcode,但是还是不可避免会产生hash碰撞,当出现碰撞时,就找到当前ThreadLocal 的下标并向后探索进行插入或者查找:
set:

public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

map.set(this, value);:

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

通过ThreadLocal 获取hashcode并获取下标,当 k == null 时 replaceStaleEntry ;当key != null 并且 k != key 则向后遍历;如果循环结束都没有插入,则 new Entry(key, value) 并放入到 i 的下标处;

 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 循环向后遍历时,如果发现 entry中key 和 ThreadLocal key 相同,则直接设置value 并将该位置的entry 与 staleSlot的entry进行替换;
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.Entry e = map.getEntry(this):

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

如果通过下标发现e为null 或者key 不相同 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;
        }

通过for 循环向后遍历,如果发现key 相同则直接返回;

参考:
ThreadLocal为什么会导致内存泄漏?

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值