ThreadLocal:Thread局部变量-------ThreadLocalVariable
java.lang
ClassThreadLocal<T>
java.lang.Object
java.lang.ThreadLocal<T>
DirectKnownSubclasses:
InheritableThreadLocal
publicclassThreadLocal<T>
extendsObject
Thisclassprovidesthread-localvariables.Thesevariablesdifferfromtheirnormalcounterpartsinthateachthreadthataccessesone(viaitsgetorsetmethod)hasitsown,independentlyinitializedcopyofthevariable.ThreadLocalinstancesaretypicallyprivatestaticfieldsinclassesthatwishtoassociatestatewithathread
ThreadLocal为每个线程使用该变量的线程提供一个独立的副本,这样每个副本读可以独立改变自己的副本,不受其他线程的影响
这个类有四个方法:
MethodSummary
Tget()
Returnsthevalueinthecurrentthread'scopyofthisthread-localvariable.
//返回当前线程对应的局部变量的值
protectedTinitialValue()
Returnsthecurrentthread'sinitialvalueforthisthread-localvariable.
//返回该线程变量的初始值,缺省值返回null,protected修饰,便于子类覆盖,是一个延迟调用,当第一次调用set(),get()时才执行1次
voidremove()
RemovesthevalueforthisThreadLocal.
//删除当前局部变量的值
voidset(Tvalue)
Setsthecurrentthread'scopyofthisthread-localvariabletothespecifiedvalue.
//设置当前线程局部变量的值:www.cdtarena.com