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