Java里,每一个线程都有本身的ThreadLocalMap,里边存着本身私有的对象。Map的Entry里,key为ThreadLocal对象,value即为私有对象T。在spring MVC中,经常使用ThreadLocal保存当前登录用户信息,这样线程在任意地方均可以取到用户信息了。spring
public class UserContext { private static final ThreadLocal<UserInfo> userInfoLocal = new ThreadLocal<UserInfo>(); public static UserInfo getUserInfo() { return userInfoLocal.get(); } public static void setUserInfo(UserInfo userInfo) { userInfoLocal.set(userInfo); } public static void clear() { userInfoLocal.remove(); } }
这里,跳过ThreadLocal和ThreadLocalMap的工做原理及场景不讲,主要来讲说Entry为何是WeakReference?多线程
/** * 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; } }
先来看看WeakReference的做用,百度一下:函数
WeakReference是Java语言规范中为了区别直接的对象引用(程序中经过构造函数声明出来的对象引用)而定义的另一种引用关系。WeakReference标志性的特色是:reference实例不会影响到被应用对象的GC回收行为(即只要对象被除WeakReference对象以外全部的对象解除引用后,该对象即可以被GC回收),只不过在被对象回收以后,reference实例想得到被应用的对象时程序会返回null。
个人理解就是,WeakReference对应用的对象userInfoLocal是弱引用,不会影响到userInfoLocal的GC行为。若是是强引用的话,在线程运行过程当中,咱们再也不使用userInfoLocal了,将userInfoLocal置为null,但userInfoLocal在线程的ThreadLocalMap里还有引用,致使其没法被GC回收(固然,能够等到线程运行结束后,整个Map都会被回收,但不少线程要运行好久,若是等到线程结束,便会一直占着内存空间)。而Entry声明为WeakReference,userInfoLocal置为null后,线程的threadLocalMap就不算强引用了,userInfoLocal就能够被GC回收了。map的后续操做中,也会逐渐把对应的"stale entry"清理出去,避免内存泄漏。this
因此,咱们在使用完ThreadLocal变量时,尽可能用threadLocal.remove()来清除,避免threadLocal=null的操做。前者remove()会同时清除掉线程threadLocalMap里的entry,算是完全清除;然后者虽然释放掉了threadLocal,但线种threadLocalMap里还有其"stale entry",后续还须要处理。spa