< prev index next >

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

Print this page




 142     public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) {
 143         return new SuppliedThreadLocal<>(supplier);
 144     }
 145 
 146     /**
 147      * Creates a thread local variable.
 148      * @see #withInitial(java.util.function.Supplier)
 149      */
 150     public ThreadLocal() {
 151     }
 152 
 153     /**
 154      * Returns the value in the current thread's copy of this
 155      * thread-local variable.  If the variable has no value for the
 156      * current thread, it is first initialized to the value returned
 157      * by an invocation of the {@link #initialValue} method.
 158      *
 159      * @return the current thread's value of this thread-local
 160      */
 161     public T get() {
 162         Thread t = Thread.currentThread();











 163         ThreadLocalMap map = getMap(t);
 164         if (map != null) {
 165             ThreadLocalMap.Entry e = map.getEntry(this);
 166             if (e != null) {
 167                 @SuppressWarnings("unchecked")
 168                 T result = (T)e.value;
 169                 return result;
 170             }
 171         }
 172         return setInitialValue();
 173     }
 174 
 175     /**
 176      * Returns {@code true} if there is a value in the current thread's copy of
 177      * this thread-local variable, even if that values is {@code null}.
 178      *
 179      * @return {@code true} if current thread has associated value in this
 180      *         thread-local variable; {@code false} if not
 181      */
 182     boolean isPresent() {
 183         Thread t = Thread.currentThread();
 184         ThreadLocalMap map = getMap(t);
 185         return map != null && map.getEntry(this) != null;
 186     }
 187 
 188     /**
 189      * Variant of set() to establish initialValue. Used instead
 190      * of set() in case user has overridden the set() method.
 191      *
 192      * @return the initial value
 193      */
 194     private T setInitialValue() {
 195         T value = initialValue();
 196         Thread t = Thread.currentThread();
 197         ThreadLocalMap map = getMap(t);
 198         if (map != null) {
 199             map.set(this, value);
 200         } else {
 201             createMap(t, value);
 202         }
 203         if (this instanceof TerminatingThreadLocal) {
 204             TerminatingThreadLocal.register((TerminatingThreadLocal<?>) this);
 205         }
 206         return value;
 207     }
 208 
 209     /**
 210      * Sets the current thread's copy of this thread-local variable
 211      * to the specified value.  Most subclasses will have no need to
 212      * override this method, relying solely on the {@link #initialValue}
 213      * method to set the values of thread-locals.
 214      *
 215      * @param value the value to be stored in the current thread's copy of
 216      *        this thread-local.




 142     public static <S> ThreadLocal<S> withInitial(Supplier<? extends S> supplier) {
 143         return new SuppliedThreadLocal<>(supplier);
 144     }
 145 
 146     /**
 147      * Creates a thread local variable.
 148      * @see #withInitial(java.util.function.Supplier)
 149      */
 150     public ThreadLocal() {
 151     }
 152 
 153     /**
 154      * Returns the value in the current thread's copy of this
 155      * thread-local variable.  If the variable has no value for the
 156      * current thread, it is first initialized to the value returned
 157      * by an invocation of the {@link #initialValue} method.
 158      *
 159      * @return the current thread's value of this thread-local
 160      */
 161     public T get() {
 162         return get(Thread.currentThread());
 163     }
 164 
 165     /**
 166      * Returns the value in the current kernel thread's copy of this
 167      * thread-local variable.
 168      */
 169     T getCarrierThreadLocal() {
 170         return get(Thread.currentCarrierThread());
 171     }
 172 
 173     private T get(Thread t) {
 174         ThreadLocalMap map = getMap(t);
 175         if (map != null) {
 176             ThreadLocalMap.Entry e = map.getEntry(this);
 177             if (e != null) {
 178                 @SuppressWarnings("unchecked")
 179                 T result = (T)e.value;
 180                 return result;
 181             }
 182         }
 183         return setInitialValue(t);
 184     }
 185 
 186     /**
 187      * Returns {@code true} if there is a value in the current thread's copy of
 188      * this thread-local variable, even if that values is {@code null}.
 189      *
 190      * @return {@code true} if current thread has associated value in this
 191      *         thread-local variable; {@code false} if not
 192      */
 193     boolean isPresent() {
 194         Thread t = Thread.currentThread();
 195         ThreadLocalMap map = getMap(t);
 196         return map != null && map.getEntry(this) != null;
 197     }
 198 
 199     /**
 200      * Variant of set() to establish initialValue. Used instead
 201      * of set() in case user has overridden the set() method.
 202      *
 203      * @return the initial value
 204      */
 205     private T setInitialValue(Thread t) {
 206         T value = initialValue();

 207         ThreadLocalMap map = getMap(t);
 208         if (map != null) {
 209             map.set(this, value);
 210         } else {
 211             createMap(t, value);
 212         }
 213         if (this instanceof TerminatingThreadLocal) {
 214             TerminatingThreadLocal.register((TerminatingThreadLocal<?>) this);
 215         }
 216         return value;
 217     }
 218 
 219     /**
 220      * Sets the current thread's copy of this thread-local variable
 221      * to the specified value.  Most subclasses will have no need to
 222      * override this method, relying solely on the {@link #initialValue}
 223      * method to set the values of thread-locals.
 224      *
 225      * @param value the value to be stored in the current thread's copy of
 226      *        this thread-local.


< prev index next >