ThreadLocal: Thread局部變量-------ThreadLocalVariable
java.lang
Class ThreadLocal<T>
java.lang.Object
java.lang.ThreadLocal<T>
Direct Known Subclasses:
InheritableThreadLocal
public class ThreadLocal<T>
extends Object
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread
ThreadLocal為每個線程使用該變量的線程提供一個獨立的副本,這樣每個副本讀可以獨立改變自己的副本,不受其他線程的影響
這個類有四個方法:
Method Summary
Tget()
Returns the value in the current thread's copy of this thread-local variable.
//返回當前線程對應的局部變量的值
protected T initialValue()
Returns the current thread's initial value for this thread-local variable.
//返回該線程變量的初始值,缺省值返回null,protected修飾,便於子類覆蓋,是一個延遲調用,當第一次調用set(),get()時才執行1次
void remove()
Removes the value for this ThreadLocal.
//刪除當前局部變量的值
void set(T value)
Sets the current thread's copy of this thread-local variable to the specified value.
//設置當前線程局部變量的值: