--- old/src/java.base/share/classes/java/lang/ThreadLocal.java 2018-06-06 20:45:07.137552986 +0200 +++ new/src/java.base/share/classes/java/lang/ThreadLocal.java 2018-06-06 20:45:07.038554668 +0200 @@ -24,6 +24,8 @@ */ package java.lang; +import jdk.internal.misc.TerminatingThreadLocal; + import java.lang.ref.*; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; @@ -171,6 +173,19 @@ } /** + * Returns {@code true} if there is a value in the current thread's copy of + * this thread-local variable, even if that values is {@code null}. + * + * @return {@code true} if current thread has associated value in this + * thread-local variable; {@code false} if not + */ + boolean isPresent() { + Thread t = Thread.currentThread(); + ThreadLocalMap map = getMap(t); + return map != null && map.getEntry(this) != null; + } + + /** * Variant of set() to establish initialValue. Used instead * of set() in case user has overridden the set() method. * @@ -184,6 +199,7 @@ map.set(this, value); else createMap(t, value); + TerminatingThreadLocal.register(this); return value; }