ThreadLocal学习

Thread 类中存在变量  :

```

ThreadLocal.ThreadLocalMapthreadLocals =null;

```

关于源码:get()方法:

/**

* Returns the value in the current thread's copy of this

* thread-local variable.  If the variable has no value for the

* current thread, it is first initialized to the value returned

* by an invocation of the {@link#initialValue} method.

*

*@returnthe current thread's value of this thread-local

*/

publicTget() {

Thread t = Thread.currentThread();

ThreadLocalMap map = getMap(t);

if(map !=null) {

ThreadLocalMap.Entry e = map.getEntry(this);

if(e !=null)

return(T)e.value;

}

returnsetInitialValue();

}

这里getMap(t)实现:

ThreadLocalMap getMap(Thread t) {

returnt.threadLocals;

}


参考地址:http://blog.csdn.net/lufeng20/article/details/24314381

你可能感兴趣的:(ThreadLocal学习)