< prev index next >

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

Print this page




 154      * current thread, it is first initialized to the value returned
 155      * by an invocation of the {@link #initialValue} method.
 156      *
 157      * @return the current thread's value of this thread-local
 158      */
 159     public T get() {
 160         Thread t = Thread.currentThread();
 161         ThreadLocalMap map = getMap(t);
 162         if (map != null) {
 163             ThreadLocalMap.Entry e = map.getEntry(this);
 164             if (e != null) {
 165                 @SuppressWarnings("unchecked")
 166                 T result = (T)e.value;
 167                 return result;
 168             }
 169         }
 170         return setInitialValue();
 171     }
 172 
 173     /**





















 174      * Variant of set() to establish initialValue. Used instead
 175      * of set() in case user has overridden the set() method.
 176      *
 177      * @return the initial value
 178      */
 179     private T setInitialValue() {
 180         T value = initialValue();
 181         Thread t = Thread.currentThread();
 182         ThreadLocalMap map = getMap(t);
 183         if (map != null)
 184             map.set(this, value);
 185         else
 186             createMap(t, value);
 187         return value;
 188     }
 189 
 190     /**
 191      * Sets the current thread's copy of this thread-local variable
 192      * to the specified value.  Most subclasses will have no need to
 193      * override this method, relying solely on the {@link #initialValue}




 154      * current thread, it is first initialized to the value returned
 155      * by an invocation of the {@link #initialValue} method.
 156      *
 157      * @return the current thread's value of this thread-local
 158      */
 159     public T get() {
 160         Thread t = Thread.currentThread();
 161         ThreadLocalMap map = getMap(t);
 162         if (map != null) {
 163             ThreadLocalMap.Entry e = map.getEntry(this);
 164             if (e != null) {
 165                 @SuppressWarnings("unchecked")
 166                 T result = (T)e.value;
 167                 return result;
 168             }
 169         }
 170         return setInitialValue();
 171     }
 172 
 173     /**
 174      * Returns the value in the current thread's copy of this
 175      * thread-local variable.  If the variable has no value for the
 176      * current thread, null is returned and no value is initialized.
 177      *
 178      * @return the current thread's value of this thread-local or null
 179      */
 180     T getIfPresent() {
 181         Thread t = Thread.currentThread();
 182         ThreadLocalMap map = getMap(t);
 183         if (map != null) {
 184             ThreadLocalMap.Entry e = map.getEntry(this);
 185             if (e != null) {
 186                 @SuppressWarnings("unchecked")
 187                 T result = (T)e.value;
 188                 return result;
 189             }
 190         }
 191         return null;
 192     }
 193 
 194     /**
 195      * Variant of set() to establish initialValue. Used instead
 196      * of set() in case user has overridden the set() method.
 197      *
 198      * @return the initial value
 199      */
 200     private T setInitialValue() {
 201         T value = initialValue();
 202         Thread t = Thread.currentThread();
 203         ThreadLocalMap map = getMap(t);
 204         if (map != null)
 205             map.set(this, value);
 206         else
 207             createMap(t, value);
 208         return value;
 209     }
 210 
 211     /**
 212      * Sets the current thread's copy of this thread-local variable
 213      * to the specified value.  Most subclasses will have no need to
 214      * override this method, relying solely on the {@link #initialValue}


< prev index next >