ThreadLocal
一、源码
1、属性
private final int threadLocalHashCode = nextHashCode(); 获得hashcode private static AtomicInteger nextHashCode = new AtomicInteger(); 定义原子操作的int private static final int HASH_INCREMENT = 0x61c88647; 为了让哈希码能均匀的分布在2的N次方的数组里,所使用的的魔数 private static int nextHashCode() { return nextHashCode.getAndAdd(HASH_INCREMENT); }
2、构造方法
public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) { return new SuppliedThreadLocal<>(supplier); } static final class SuppliedThreadLocal extends ThreadLocal { private final Supplier<? extends T> supplier; SuppliedThreadLocal(Supplier<? extends T> supplier) { this.supplier = Objects.requireNonNull(supplier); } @Override protected T initialValue() { return supplier.get(); } }
lambda表达式初始化
public ThreadLocal() {} protected T initialValue() { return null; }
用于创建对象时重写初始化方法
3、一般方法
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(); } private T setInitialValue() { T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); return value; }
如果线程的本地map副本不存在就创建,如果存在就返回副本里的内容
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } 设置副本内容 public void remove() { ThreadLocalMap m = getMap(Thread.currentThread()); if (m != null) m.remove(this); } 从副本中移除 ThreadLocalMap getMap(Thread t) { return t.threadLocals; } 获得线程的副本集合 void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); } 创建副本
static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) { return new ThreadLocalMap(parentMap); }
创建一个包含父副本的map
4、ThreadLocalMap
略