Merging in Simplified implementation of thread local that assumes we run in a single thread
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.
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.
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).
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.
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
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).
36 * <p>For example, the class below generates unique identifiers local to each
38 * A thread's id is assigned the first time it invokes <tt>ThreadId.get()</tt>
39 * and remains unchanged on subsequent calls.
41 * import java.util.concurrent.atomic.AtomicInteger;
43 * public class ThreadId {
44 * // Atomic integer containing the next thread ID to be assigned
45 * private static final AtomicInteger nextId = new AtomicInteger(0);
47 * // Thread local variable containing each thread's ID
48 * private static final ThreadLocal<Integer> threadId =
49 * new ThreadLocal<Integer>() {
50 * @Override protected Integer initialValue() {
51 * return nextId.getAndIncrement();
55 * // Returns the current thread's unique ID, assigning it if necessary
56 * public static int get() {
57 * return threadId.get();
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).
67 * @author Josh Bloch and Doug Lea
70 public class ThreadLocal<T> {
71 private static final Object NONE = new Object();
72 private Object value = NONE;
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}.
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.
90 * @return the initial value for this thread-local
92 protected T initialValue() {
97 * Creates a thread local variable.
99 public ThreadLocal() {
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.
108 * @return the current thread's value of this thread-local
112 return setInitialValue();
119 * Variant of set() to establish initialValue. Used instead
120 * of set() in case user has overridden the set() method.
122 * @return the initial value
124 private T setInitialValue() {
125 T v = initialValue();
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.
136 * @param value the value to be stored in the current thread's copy of
139 public void set(T value) {
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.
154 public void remove() {