Methods Summary |
---|
T | childValue(T parentValue)Method childValue is visibly defined in subclass
InheritableThreadLocal, but is internally defined here for the
sake of providing createInheritedMap factory method without
needing to subclass the map class in InheritableThreadLocal.
This technique is preferable to the alternative of embedding
instanceof tests in methods.
throw new UnsupportedOperationException();
|
static java.lang.ThreadLocal$ThreadLocalMap | createInheritedMap(java.lang.ThreadLocal$ThreadLocalMap parentMap)Factory method to create map of inherited thread locals.
Designed to be called only from Thread constructor.
return new ThreadLocalMap(parentMap);
|
void | createMap(java.lang.Thread t, T firstValue)Create the map associated with a ThreadLocal. Overridden in
InheritableThreadLocal.
t.threadLocals = new ThreadLocalMap(this, firstValue);
|
public T | 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.
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();
|
java.lang.ThreadLocal$ThreadLocalMap | getMap(java.lang.Thread t)Get the map associated with a ThreadLocal. Overridden in
InheritableThreadLocal.
return t.threadLocals;
|
protected T | initialValue()Returns the current thread's "initial value" for this
thread-local variable. This method will be invoked the first
time a thread accesses the variable with the {@link #get}
method, unless the thread previously invoked the {@link #set}
method, in which case the initialValue method will not
be invoked for the thread. Normally, this method is invoked at
most once per thread, but it may be invoked again in case of
subsequent invocations of {@link #remove} followed by {@link #get}.
This implementation simply returns null; if the
programmer desires thread-local variables to have an initial
value other than null, ThreadLocal must be
subclassed, and this method overridden. Typically, an
anonymous inner class will be used.
return null;
|
private static int | nextHashCode()Returns the next hash code.
return nextHashCode.getAndAdd(HASH_INCREMENT);
|
public void | remove()Removes the current thread's value for this thread-local
variable. If this thread-local variable is subsequently
{@linkplain #get read} by the current thread, its value will be
reinitialized by invoking its {@link #initialValue} method,
unless its value is {@linkplain #set set} by the current thread
in the interim. This may result in multiple invocations of the
initialValue method in the current thread.
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
|
public void | set(T value)Sets the current thread's copy of this thread-local variable
to the specified value. Most subclasses will have no need to
override this method, relying solely on the {@link #initialValue}
method to set the values of thread-locals.
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
|
private T | setInitialValue()Variant of set() to establish initialValue. Used instead
of set() in case user has overridden the set() method.
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
|