1、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); }
通过ThreadLocal的set方法看出,ThreadLocalMap的
2、ThreadLocal本身不存储值,在使用中,ThreadLocal是作为一个key,从ThreadLocalMap获取值,从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(); }
以上是java ThreadLocal的对象如何存储和获取的详细内容。更多信息请关注PHP中文网其他相关文章!