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中文網其他相關文章!