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. Creates and initializes the copy if this is the first time
the thread has called this method.
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
return (T)map.get(this);
// Maps are constructed lazily. if the map for this thread
// doesn't exist, create it, with this ThreadLocal and its
// initial value as its only entry.
T value = initialValue();
createMap(t, value);
return value;
|
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 at most once per accessing
thread for each thread-local, the first time the thread accesses the
variable with the {@link #get} method. The initialValue
method will not be invoked in a thread if the thread invokes the {@link
#set} method prior to the get method.
This implementation simply returns null; if the programmer
desires thread-local variables to be initialized to some value other
than null, ThreadLocal must be subclassed, and this
method overridden. Typically, an anonymous inner class will be used.
Typical implementations of initialValue will invoke an
appropriate constructor and return the newly constructed object.
return null;
|
private static synchronized int | nextHashCode()Compute the next hash code. The static synchronization used here
should not be a performance bottleneck. When ThreadLocals are
generated in different threads at a fast enough rate to regularly
contend on this lock, memory contention is by far a more serious
problem than lock contention.
int h = nextHashCode;
nextHashCode = h + HASH_INCREMENT;
return h;
|
public void | remove()Removes the value for this ThreadLocal. This may help reduce
the storage requirements of ThreadLocals. If this ThreadLocal
is accessed again, it will by default have its
initialValue.
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. Many applications will have no need for
this functionality, 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);
|