rt/emul/compact/src/main/java/java/lang/ThreadLocal.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 12 Sep 2013 12:26:23 +0200
changeset 1281 8ed05565a481
parent 1280 emul/compact/src/main/java/java/lang/ThreadLocal.java@c1e76ee31360
permissions -rw-r--r--
Merging in Simplified implementation of thread local that assumes we run in a single thread
     1 /*
     2  * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 /**
    29  * This class provides thread-local variables.  These variables differ from
    30  * their normal counterparts in that each thread that accesses one (via its
    31  * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized
    32  * copy of the variable.  <tt>ThreadLocal</tt> instances are typically private
    33  * static fields in classes that wish to associate state with a thread (e.g.,
    34  * a user ID or Transaction ID).
    35  *
    36  * <p>For example, the class below generates unique identifiers local to each
    37  * thread.
    38  * A thread's id is assigned the first time it invokes <tt>ThreadId.get()</tt>
    39  * and remains unchanged on subsequent calls.
    40  * <pre>
    41  * import java.util.concurrent.atomic.AtomicInteger;
    42  *
    43  * public class ThreadId {
    44  *     // Atomic integer containing the next thread ID to be assigned
    45  *     private static final AtomicInteger nextId = new AtomicInteger(0);
    46  *
    47  *     // Thread local variable containing each thread's ID
    48  *     private static final ThreadLocal&lt;Integer> threadId =
    49  *         new ThreadLocal&lt;Integer>() {
    50  *             &#64;Override protected Integer initialValue() {
    51  *                 return nextId.getAndIncrement();
    52  *         }
    53  *     };
    54  *
    55  *     // Returns the current thread's unique ID, assigning it if necessary
    56  *     public static int get() {
    57  *         return threadId.get();
    58  *     }
    59  * }
    60  * </pre>
    61  * <p>Each thread holds an implicit reference to its copy of a thread-local
    62  * variable as long as the thread is alive and the <tt>ThreadLocal</tt>
    63  * instance is accessible; after a thread goes away, all of its copies of
    64  * thread-local instances are subject to garbage collection (unless other
    65  * references to these copies exist).
    66  *
    67  * @author  Josh Bloch and Doug Lea
    68  * @since   1.2
    69  */
    70 public class ThreadLocal<T> {
    71     private static final Object NONE = new Object();
    72     private Object value = NONE;
    73     
    74     /**
    75      * Returns the current thread's "initial value" for this
    76      * thread-local variable.  This method will be invoked the first
    77      * time a thread accesses the variable with the {@link #get}
    78      * method, unless the thread previously invoked the {@link #set}
    79      * method, in which case the <tt>initialValue</tt> method will not
    80      * be invoked for the thread.  Normally, this method is invoked at
    81      * most once per thread, but it may be invoked again in case of
    82      * subsequent invocations of {@link #remove} followed by {@link #get}.
    83      *
    84      * <p>This implementation simply returns <tt>null</tt>; if the
    85      * programmer desires thread-local variables to have an initial
    86      * value other than <tt>null</tt>, <tt>ThreadLocal</tt> must be
    87      * subclassed, and this method overridden.  Typically, an
    88      * anonymous inner class will be used.
    89      *
    90      * @return the initial value for this thread-local
    91      */
    92     protected T initialValue() {
    93         return null;
    94     }
    95 
    96     /**
    97      * Creates a thread local variable.
    98      */
    99     public ThreadLocal() {
   100     }
   101 
   102     /**
   103      * Returns the value in the current thread's copy of this
   104      * thread-local variable.  If the variable has no value for the
   105      * current thread, it is first initialized to the value returned
   106      * by an invocation of the {@link #initialValue} method.
   107      *
   108      * @return the current thread's value of this thread-local
   109      */
   110     public T get() {
   111         if (value == NONE) {
   112             return setInitialValue();
   113         } else {
   114             return (T)value;
   115         }
   116     }
   117 
   118     /**
   119      * Variant of set() to establish initialValue. Used instead
   120      * of set() in case user has overridden the set() method.
   121      *
   122      * @return the initial value
   123      */
   124     private T setInitialValue() {
   125         T v = initialValue();
   126         this.value = v;
   127         return v;
   128     }
   129 
   130     /**
   131      * Sets the current thread's copy of this thread-local variable
   132      * to the specified value.  Most subclasses will have no need to
   133      * override this method, relying solely on the {@link #initialValue}
   134      * method to set the values of thread-locals.
   135      *
   136      * @param value the value to be stored in the current thread's copy of
   137      *        this thread-local.
   138      */
   139     public void set(T value) {
   140         this.value = value;
   141     }
   142 
   143     /**
   144      * Removes the current thread's value for this thread-local
   145      * variable.  If this thread-local variable is subsequently
   146      * {@linkplain #get read} by the current thread, its value will be
   147      * reinitialized by invoking its {@link #initialValue} method,
   148      * unless its value is {@linkplain #set set} by the current thread
   149      * in the interim.  This may result in multiple invocations of the
   150      * <tt>initialValue</tt> method in the current thread.
   151      *
   152      * @since 1.5
   153      */
   154      public void remove() {
   155          this.value = NONE;
   156      }
   157 }