staticclassEntryextendsWeakReference<ThreadLocal<?>>{/** The value associated with this ThreadLocal. */Object value;Entry(ThreadLocal<?> k,Object v){super(k);// 创建一个虚引用指向k,当k没有其他引用指向时,下一次垃圾回收将被回收
value = v;}}
get方法
publicTget(){Thread t =Thread.currentThread();ThreadLocalMap map =getMap(t);if(map !=null){ThreadLocalMap.Entry e = map.getEntry(this);// 根据threadlocal对象获取entryif(e !=null){@SuppressWarnings("unchecked")T result =(T)e.value; 返回entry的value
return result;}}returnsetInitialValue();}