src/share/classes/java/lang/ThreadLocal.java

Print this page




  30 /**
  31  * This class provides thread-local variables.  These variables differ from
  32  * their normal counterparts in that each thread that accesses one (via its
  33  * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized
  34  * copy of the variable.  <tt>ThreadLocal</tt> instances are typically private
  35  * static fields in classes that wish to associate state with a thread (e.g.,
  36  * a user ID or Transaction ID).
  37  *
  38  * <p>For example, the class below generates unique identifiers local to each
  39  * thread.
  40  * A thread's id is assigned the first time it invokes <tt>ThreadId.get()</tt>
  41  * and remains unchanged on subsequent calls.
  42  * <pre>
  43  * import java.util.concurrent.atomic.AtomicInteger;
  44  *
  45  * public class ThreadId {
  46  *     // Atomic integer containing the next thread ID to be assigned
  47  *     private static final AtomicInteger nextId = new AtomicInteger(0);
  48  *
  49  *     // Thread local variable containing each thread's ID
  50  *     private static final ThreadLocal&lt;Integer> threadId =
  51  *         new ThreadLocal&lt;Integer>() {
  52  *             &#64;Override protected Integer initialValue() {
  53  *                 return nextId.getAndIncrement();
  54  *         }
  55  *     };
  56  *
  57  *     // Returns the current thread's unique ID, assigning it if necessary
  58  *     public static int get() {
  59  *         return threadId.get();
  60  *     }
  61  * }
  62  * </pre>
  63  * <p>Each thread holds an implicit reference to its copy of a thread-local
  64  * variable as long as the thread is alive and the <tt>ThreadLocal</tt>
  65  * instance is accessible; after a thread goes away, all of its copies of
  66  * thread-local instances are subject to garbage collection (unless other
  67  * references to these copies exist).
  68  *
  69  * @author  Josh Bloch and Doug Lea
  70  * @since   1.2
  71  */


 205              m.remove(this);
 206      }
 207 
 208     /**
 209      * Get the map associated with a ThreadLocal. Overridden in
 210      * InheritableThreadLocal.
 211      *
 212      * @param  t the current thread
 213      * @return the map
 214      */
 215     ThreadLocalMap getMap(Thread t) {
 216         return t.threadLocals;
 217     }
 218 
 219     /**
 220      * Create the map associated with a ThreadLocal. Overridden in
 221      * InheritableThreadLocal.
 222      *
 223      * @param t the current thread
 224      * @param firstValue value for the initial entry of the map
 225      * @param map the map to store.
 226      */
 227     void createMap(Thread t, T firstValue) {
 228         t.threadLocals = new ThreadLocalMap(this, firstValue);
 229     }
 230 
 231     /**
 232      * Factory method to create map of inherited thread locals.
 233      * Designed to be called only from Thread constructor.
 234      *
 235      * @param  parentMap the map associated with parent thread
 236      * @return a map containing the parent's inheritable bindings
 237      */
 238     static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
 239         return new ThreadLocalMap(parentMap);
 240     }
 241 
 242     /**
 243      * Method childValue is visibly defined in subclass
 244      * InheritableThreadLocal, but is internally defined here for the
 245      * sake of providing createInheritedMap factory method without




  30 /**
  31  * This class provides thread-local variables.  These variables differ from
  32  * their normal counterparts in that each thread that accesses one (via its
  33  * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized
  34  * copy of the variable.  <tt>ThreadLocal</tt> instances are typically private
  35  * static fields in classes that wish to associate state with a thread (e.g.,
  36  * a user ID or Transaction ID).
  37  *
  38  * <p>For example, the class below generates unique identifiers local to each
  39  * thread.
  40  * A thread's id is assigned the first time it invokes <tt>ThreadId.get()</tt>
  41  * and remains unchanged on subsequent calls.
  42  * <pre>
  43  * import java.util.concurrent.atomic.AtomicInteger;
  44  *
  45  * public class ThreadId {
  46  *     // Atomic integer containing the next thread ID to be assigned
  47  *     private static final AtomicInteger nextId = new AtomicInteger(0);
  48  *
  49  *     // Thread local variable containing each thread's ID
  50  *     private static final ThreadLocal&lt;Integer&gt; threadId =
  51  *         new ThreadLocal&lt;Integer&gt;() {
  52  *             &#64;Override protected Integer initialValue() {
  53  *                 return nextId.getAndIncrement();
  54  *         }
  55  *     };
  56  *
  57  *     // Returns the current thread's unique ID, assigning it if necessary
  58  *     public static int get() {
  59  *         return threadId.get();
  60  *     }
  61  * }
  62  * </pre>
  63  * <p>Each thread holds an implicit reference to its copy of a thread-local
  64  * variable as long as the thread is alive and the <tt>ThreadLocal</tt>
  65  * instance is accessible; after a thread goes away, all of its copies of
  66  * thread-local instances are subject to garbage collection (unless other
  67  * references to these copies exist).
  68  *
  69  * @author  Josh Bloch and Doug Lea
  70  * @since   1.2
  71  */


 205              m.remove(this);
 206      }
 207 
 208     /**
 209      * Get the map associated with a ThreadLocal. Overridden in
 210      * InheritableThreadLocal.
 211      *
 212      * @param  t the current thread
 213      * @return the map
 214      */
 215     ThreadLocalMap getMap(Thread t) {
 216         return t.threadLocals;
 217     }
 218 
 219     /**
 220      * Create the map associated with a ThreadLocal. Overridden in
 221      * InheritableThreadLocal.
 222      *
 223      * @param t the current thread
 224      * @param firstValue value for the initial entry of the map

 225      */
 226     void createMap(Thread t, T firstValue) {
 227         t.threadLocals = new ThreadLocalMap(this, firstValue);
 228     }
 229 
 230     /**
 231      * Factory method to create map of inherited thread locals.
 232      * Designed to be called only from Thread constructor.
 233      *
 234      * @param  parentMap the map associated with parent thread
 235      * @return a map containing the parent's inheritable bindings
 236      */
 237     static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
 238         return new ThreadLocalMap(parentMap);
 239     }
 240 
 241     /**
 242      * Method childValue is visibly defined in subclass
 243      * InheritableThreadLocal, but is internally defined here for the
 244      * sake of providing createInheritedMap factory method without