rt/emul/compact/src/main/java/java/lang/ThreadLocal.java
changeset 1281 8ed05565a481
parent 1280 c1e76ee31360
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/ThreadLocal.java	Thu Sep 12 12:26:23 2013 +0200
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +/**
    1.32 + * This class provides thread-local variables.  These variables differ from
    1.33 + * their normal counterparts in that each thread that accesses one (via its
    1.34 + * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized
    1.35 + * copy of the variable.  <tt>ThreadLocal</tt> instances are typically private
    1.36 + * static fields in classes that wish to associate state with a thread (e.g.,
    1.37 + * a user ID or Transaction ID).
    1.38 + *
    1.39 + * <p>For example, the class below generates unique identifiers local to each
    1.40 + * thread.
    1.41 + * A thread's id is assigned the first time it invokes <tt>ThreadId.get()</tt>
    1.42 + * and remains unchanged on subsequent calls.
    1.43 + * <pre>
    1.44 + * import java.util.concurrent.atomic.AtomicInteger;
    1.45 + *
    1.46 + * public class ThreadId {
    1.47 + *     // Atomic integer containing the next thread ID to be assigned
    1.48 + *     private static final AtomicInteger nextId = new AtomicInteger(0);
    1.49 + *
    1.50 + *     // Thread local variable containing each thread's ID
    1.51 + *     private static final ThreadLocal&lt;Integer> threadId =
    1.52 + *         new ThreadLocal&lt;Integer>() {
    1.53 + *             &#64;Override protected Integer initialValue() {
    1.54 + *                 return nextId.getAndIncrement();
    1.55 + *         }
    1.56 + *     };
    1.57 + *
    1.58 + *     // Returns the current thread's unique ID, assigning it if necessary
    1.59 + *     public static int get() {
    1.60 + *         return threadId.get();
    1.61 + *     }
    1.62 + * }
    1.63 + * </pre>
    1.64 + * <p>Each thread holds an implicit reference to its copy of a thread-local
    1.65 + * variable as long as the thread is alive and the <tt>ThreadLocal</tt>
    1.66 + * instance is accessible; after a thread goes away, all of its copies of
    1.67 + * thread-local instances are subject to garbage collection (unless other
    1.68 + * references to these copies exist).
    1.69 + *
    1.70 + * @author  Josh Bloch and Doug Lea
    1.71 + * @since   1.2
    1.72 + */
    1.73 +public class ThreadLocal<T> {
    1.74 +    private static final Object NONE = new Object();
    1.75 +    private Object value = NONE;
    1.76 +    
    1.77 +    /**
    1.78 +     * Returns the current thread's "initial value" for this
    1.79 +     * thread-local variable.  This method will be invoked the first
    1.80 +     * time a thread accesses the variable with the {@link #get}
    1.81 +     * method, unless the thread previously invoked the {@link #set}
    1.82 +     * method, in which case the <tt>initialValue</tt> method will not
    1.83 +     * be invoked for the thread.  Normally, this method is invoked at
    1.84 +     * most once per thread, but it may be invoked again in case of
    1.85 +     * subsequent invocations of {@link #remove} followed by {@link #get}.
    1.86 +     *
    1.87 +     * <p>This implementation simply returns <tt>null</tt>; if the
    1.88 +     * programmer desires thread-local variables to have an initial
    1.89 +     * value other than <tt>null</tt>, <tt>ThreadLocal</tt> must be
    1.90 +     * subclassed, and this method overridden.  Typically, an
    1.91 +     * anonymous inner class will be used.
    1.92 +     *
    1.93 +     * @return the initial value for this thread-local
    1.94 +     */
    1.95 +    protected T initialValue() {
    1.96 +        return null;
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Creates a thread local variable.
   1.101 +     */
   1.102 +    public ThreadLocal() {
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Returns the value in the current thread's copy of this
   1.107 +     * thread-local variable.  If the variable has no value for the
   1.108 +     * current thread, it is first initialized to the value returned
   1.109 +     * by an invocation of the {@link #initialValue} method.
   1.110 +     *
   1.111 +     * @return the current thread's value of this thread-local
   1.112 +     */
   1.113 +    public T get() {
   1.114 +        if (value == NONE) {
   1.115 +            return setInitialValue();
   1.116 +        } else {
   1.117 +            return (T)value;
   1.118 +        }
   1.119 +    }
   1.120 +
   1.121 +    /**
   1.122 +     * Variant of set() to establish initialValue. Used instead
   1.123 +     * of set() in case user has overridden the set() method.
   1.124 +     *
   1.125 +     * @return the initial value
   1.126 +     */
   1.127 +    private T setInitialValue() {
   1.128 +        T v = initialValue();
   1.129 +        this.value = v;
   1.130 +        return v;
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Sets the current thread's copy of this thread-local variable
   1.135 +     * to the specified value.  Most subclasses will have no need to
   1.136 +     * override this method, relying solely on the {@link #initialValue}
   1.137 +     * method to set the values of thread-locals.
   1.138 +     *
   1.139 +     * @param value the value to be stored in the current thread's copy of
   1.140 +     *        this thread-local.
   1.141 +     */
   1.142 +    public void set(T value) {
   1.143 +        this.value = value;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Removes the current thread's value for this thread-local
   1.148 +     * variable.  If this thread-local variable is subsequently
   1.149 +     * {@linkplain #get read} by the current thread, its value will be
   1.150 +     * reinitialized by invoking its {@link #initialValue} method,
   1.151 +     * unless its value is {@linkplain #set set} by the current thread
   1.152 +     * in the interim.  This may result in multiple invocations of the
   1.153 +     * <tt>initialValue</tt> method in the current thread.
   1.154 +     *
   1.155 +     * @since 1.5
   1.156 +     */
   1.157 +     public void remove() {
   1.158 +         this.value = NONE;
   1.159 +     }
   1.160 +}