ThreadLocal 原理(JDK6)

ThreadLocal.set("test")

=>

    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

当前Thread中有一个threadLocals的ThreadLocalMap属性,该Map中key为ThreadLocal对象,值为set的value。

你可能感兴趣的:(threadLocal)