/**
* Sets the current thread's copyof this thread-local variable
* tothe specified value. Most subclasses will have no need to
* override this method, relying solely onthe {@link #initialValue}
* method tosetthe values of thread-locals.
*
* @param value the value to be stored inthe current thread's copyof
* this thread-local.
*/
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
/**
* Returns the value inthe current thread's copyof this
* thread-local variable. If the variable has no value forthe
* current thread, itisfirst initialized tothe value returned
* by an invocation ofthe {@link #initialValue} method.
*
* @returnthe current thread's value of this thread-local
*/
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();
}
/**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
*
* @param t the current thread
* @returnthe map
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}