/[classpath]/classpath/java/lang/ThreadLocal.java
ViewVC logotype

Diff of /classpath/java/lang/ThreadLocal.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4.2.2 by gnu_andrew, Sat Jan 15 17:01:53 2005 UTC revision 1.4.2.3 by gnu_andrew, Sun Mar 13 14:38:40 2005 UTC
# Line 37  exception statement from your version. * Line 37  exception statement from your version. *
37    
38  package java.lang;  package java.lang;
39    
 import java.util.Collections;  
40  import java.util.Map;  import java.util.Map;
41  import java.util.WeakHashMap;  import java.util.WeakHashMap;
42    
# Line 98  public class ThreadLocal<T> Line 97  public class ThreadLocal<T>
97    static final Object NULL = new Object();    static final Object NULL = new Object();
98    
99    /**    /**
100     * The stored value. Package visible for use by InheritableThreadLocal. */     * Serves as a key for the Thread.locals WeakHashMap.
101    T value;     * We can't use "this", because a subclass may override equals/hashCode
102       * and we need to use object identity for the map.
   /**  
    * Maps Threads to values. Uses a WeakHashMap so if a Thread is garbage  
    * collected the reference to the Value will disappear. A null value means  
    * uninitialized, while NULL means a user-specified null. Only the  
    * <code>set(Thread, Object)</code> and <code>get(Thread)</code> methods  
    * access it. Package visible for use by InheritableThreadLocal.  
103     */     */
104    final Map<Thread, T> valueMap    final Key key = new Key();
105      = Collections.synchronizedMap(new WeakHashMap<Thread, T>());  
106      class Key
107      {
108        ThreadLocal get()
109        {
110          return ThreadLocal.this;
111        }
112      }
113    
114    
115    /**    /**
116     * Creates a ThreadLocal object without associating any value to it yet.     * Creates a ThreadLocal object without associating any value to it yet.
# Line 141  public class ThreadLocal<T> Line 142  public class ThreadLocal<T>
142     */     */
143    public T get()    public T get()
144    {    {
145      Thread currentThread = Thread.currentThread();      Map<Key,T> map = (Map<Key,T>) Thread.getThreadLocals();
146      // Note that we don't have to synchronize, as only this thread will      // Note that we don't have to synchronize, as only this thread will
147      // ever modify the returned value and valueMap is a synchronizedMap.      // ever modify the map.
148      T value = valueMap.get(currentThread);      T value = map.get(key);
149      if (value == null)      if (value == null)
150        {        {
151          value = initialValue();          value = initialValue();
152          valueMap.put(currentThread, value == null ? (T) NULL : value);          map.put(key, value == null ? (T) NULL : value);
153        }        }
154      return value == (T) NULL ? null : value;      return value == (T) NULL ? null : value;
155    }    }
# Line 163  public class ThreadLocal<T> Line 164  public class ThreadLocal<T>
164     */     */
165    public void set(T value)    public void set(T value)
166    {    {
167        Map map = Thread.getThreadLocals();
168      // Note that we don't have to synchronize, as only this thread will      // Note that we don't have to synchronize, as only this thread will
169      // ever modify the returned value and valueMap is a synchronizedMap.      // ever modify the map.
170      valueMap.put(Thread.currentThread(), value == null ? (T) NULL : value);      map.put(key, value == null ? NULL : value);
171    }    }
172  }  }

Legend:
Removed from v.1.4.2.2  
changed lines
  Added in v.1.4.2.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26