/[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.5 by mkoch, Fri Oct 15 07:44:17 2004 UTC revision 1.6 by jfrijters, Thu Mar 3 09:21:57 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 Line 97  public class ThreadLocal
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    Object 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 valueMap = Collections.synchronizedMap(new WeakHashMap());    final Key key = new Key();
105            
106      class Key
107      {
108        ThreadLocal get()
109        {
110          return ThreadLocal.this;
111        }
112      }
113    
114    /**    /**
115     * Creates a ThreadLocal object without associating any value to it yet.     * Creates a ThreadLocal object without associating any value to it yet.
116     */     */
# Line 140  public class ThreadLocal Line 141  public class ThreadLocal
141     */     */
142    public Object get()    public Object get()
143    {    {
144      Thread currentThread = Thread.currentThread();      Map map = Thread.getThreadLocals();
145      // 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
146      // ever modify the returned value and valueMap is a synchronizedMap.      // ever modify the map.
147      Object value = valueMap.get(currentThread);      Object value = map.get(key);
148      if (value == null)      if (value == null)
149        {        {
150          value = initialValue();          value = initialValue();
151          valueMap.put(currentThread, value == null ? NULL : value);          map.put(key, value == null ? NULL : value);
152        }        }
153      return value == NULL ? null : value;      return value == NULL ? null : value;
154    }    }
# Line 162  public class ThreadLocal Line 163  public class ThreadLocal
163     */     */
164    public void set(Object value)    public void set(Object value)
165    {    {
166        Map map = Thread.getThreadLocals();
167      // 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
168      // ever modify the returned value and valueMap is a synchronizedMap.      // ever modify the map.
169      valueMap.put(Thread.currentThread(), value == null ? NULL : value);      map.put(key, value == null ? NULL : value);
170    }    }
171  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

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