< prev index next >
src/java.base/share/classes/java/lang/ThreadLocal.java
Print this page
@@ -169,10 +169,31 @@
}
return setInitialValue();
}
/**
+ * Returns the value in the current thread's copy of this
+ * thread-local variable. If the variable has no value for the
+ * current thread, null is returned and no value is initialized.
+ *
+ * @return the current thread's value of this thread-local or null
+ */
+ T getIfPresent() {
+ Thread t = Thread.currentThread();
+ ThreadLocalMap map = getMap(t);
+ if (map != null) {
+ ThreadLocalMap.Entry e = map.getEntry(this);
+ if (e != null) {
+ @SuppressWarnings("unchecked")
+ T result = (T)e.value;
+ return result;
+ }
+ }
+ return null;
+ }
+
+ /**
* Variant of set() to establish initialValue. Used instead
* of set() in case user has overridden the set() method.
*
* @return the initial value
*/
< prev index next >