
说明:在我们Thread 的类里面,存在一个属性,这个属性是:ThreadLocal类实现的内部类(ThreadLocalMap),所以对于每一个线程来说,他都具有一个本地的map,保存属于自己的参数
类似于:session 里面 存放一个map, 把用户看做线程,session当做Thread
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 T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
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;
}
此类的变量中存在
ThreadLocal.ThreadLocalMap threadLocals = null;
原文:http://www.cnblogs.com/sg9527/p/7692652.html