java.lang
Class ThreadLocal.ThreadLocalMap

java.lang.Object
  extended byjava.lang.ThreadLocal.ThreadLocalMap
Enclosing class:
ThreadLocal

static class ThreadLocal.ThreadLocalMap
extends Object

ThreadLocalMap is a customized hash map suitable only for maintaining thread local values. No operations are exported outside of the ThreadLocal class. The class is package private to allow declaration of fields in class Thread. To help deal with very large and long-lived usages, the hash table entries use WeakReferences for keys. However, since reference queues are not used, stale entries are guaranteed to be removed only when the table starts running out of space.


Nested Class Summary
private static class ThreadLocal.ThreadLocalMap.Entry
          The entries in this hash map extend WeakReference, using its main ref field as the key (which is always a ThreadLocal object).
 
Field Summary
private static int INITIAL_CAPACITY
          The initial capacity -- MUST be a power of two.
private  int size
          The number of entries in the table.
private  ThreadLocal.ThreadLocalMap.Entry[] table
          The table, resized as necessary.
private  int threshold
          The next size value at which to resize.
 
Constructor Summary
private ThreadLocal.ThreadLocalMap(ThreadLocal.ThreadLocalMap parentMap)
          Construct a new map including all Inheritable ThreadLocals from given parent map.
(package private) ThreadLocal.ThreadLocalMap(ThreadLocal firstKey, Object firstValue)
          Construct a new map initially containing (firstKey, firstValue).
 
Method Summary
private  void expungeStaleEntries()
          Expunge all stale entries in the table.
private  void expungeStaleEntry(int staleSlot)
          Expunge a stale entry by rehashing any possibly colliding entries lying between staleSlot and the next null slot.
private  Object get(ThreadLocal key)
          Get the value associated with key with code h.
private  Object getAfterMiss(ThreadLocal key, int i, ThreadLocal.ThreadLocalMap.Entry e)
          Version of get method for use when key is not found in its direct hash slot.
private static int nextIndex(int i, int len)
          Increment i modulo len.
private static int prevIndex(int i, int len)
          Decrement i modulo len.
private  void rehash()
          Re-pack and/or re-size the table.
private  Object replaceStaleEntry(ThreadLocal key, Object value, int staleSlot, boolean actAsGet)
          Replace a stale entry encountered during a get or set operation with an entry for the specified key.
private  void resize()
          Double the capacity of the table.
private  void set(ThreadLocal key, Object value)
          Set the value associated with key.
private  void setThreshold(int len)
          Set the resize threshold to maintain at worst a 2/3 load factor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INITIAL_CAPACITY

private static final int INITIAL_CAPACITY
The initial capacity -- MUST be a power of two.

See Also:
Constant Field Values

table

private ThreadLocal.ThreadLocalMap.Entry[] table
The table, resized as necessary. table.length MUST always be a power of two.


size

private int size
The number of entries in the table.


threshold

private int threshold
The next size value at which to resize.

Constructor Detail

ThreadLocal.ThreadLocalMap

ThreadLocal.ThreadLocalMap(ThreadLocal firstKey,
                           Object firstValue)
Construct a new map initially containing (firstKey, firstValue). ThreadLocalMaps are constructed lazily, so we only create one when we have at least one entry to put in it.


ThreadLocal.ThreadLocalMap

private ThreadLocal.ThreadLocalMap(ThreadLocal.ThreadLocalMap parentMap)
Construct a new map including all Inheritable ThreadLocals from given parent map. Called only by createInheritedMap.

Parameters:
parentMap - the map associated with parent thread.
Method Detail

setThreshold

private void setThreshold(int len)
Set the resize threshold to maintain at worst a 2/3 load factor.


nextIndex

private static int nextIndex(int i,
                             int len)
Increment i modulo len.


prevIndex

private static int prevIndex(int i,
                             int len)
Decrement i modulo len.


get

private Object get(ThreadLocal key)
Get the value associated with key with code h. This method itself handles only the fast path: a direct hit of existing key. It otherwise relays to getAfterMiss. This is designed to maximize performance for direct hits, in part by making this method readily inlinable.

Parameters:
key - the thread local object
Returns:
the value associated with key

getAfterMiss

private Object getAfterMiss(ThreadLocal key,
                            int i,
                            ThreadLocal.ThreadLocalMap.Entry e)
Version of get method for use when key is not found in its direct hash slot.

Parameters:
key - the thread local object
i - the table index for key's hash code
e - the entry at table[i];
Returns:
the value associated with key

set

private void set(ThreadLocal key,
                 Object value)
Set the value associated with key.

Parameters:
key - the thread local object
value - the value to be set

replaceStaleEntry

private Object replaceStaleEntry(ThreadLocal key,
                                 Object value,
                                 int staleSlot,
                                 boolean actAsGet)
Replace a stale entry encountered during a get or set operation with an entry for the specified key. If actAsGet is true and an entry for the key already exists, the value in the entry is unchanged; if no entry exists for the key, the value in the new entry is obtained by calling key.initialValue. If actAsGet is false, the value passed in the value parameter is stored in the entry, whether or not an entry already exists for the specified key. As a side effect, this method expunges all stale entries in the "run" containing the stale entry. (A run is a sequence of entries between two null slots.)

Parameters:
key - the key
value - the value to be associated with key; meaningful only if actAsGet is false.
staleSlot - index of the first stale entry encountered while searching for key.
actAsGet - true if this method should act as a get; false it should act as a set.
Returns:
the value associated with key after the operation completes.

expungeStaleEntry

private void expungeStaleEntry(int staleSlot)
Expunge a stale entry by rehashing any possibly colliding entries lying between staleSlot and the next null slot. This also expunges any other stale entries encountered before the trailing null. See Knuth, Section 6.4

Parameters:
staleSlot - index of slot known to have null key

rehash

private void rehash()
Re-pack and/or re-size the table. First scan the entire table removing stale entries. If this doesn't sufficiently shrink the size of the table, double the table size.


resize

private void resize()
Double the capacity of the table.


expungeStaleEntries

private void expungeStaleEntries()
Expunge all stale entries in the table.