summaryrefslogtreecommitdiff
path: root/src/java.base/share/classes/java/lang/ThreadLocal.java
diff options
context:
space:
mode:
authorPeter Levart <plevart@openjdk.org>2018-06-22 17:56:55 +0200
committerPeter Levart <plevart@openjdk.org>2018-06-22 17:56:55 +0200
commit6ec2cfcc49d8def0d035860f17fd4faeb5eb431c (patch)
treee69d2751bad25eb52c1e180e1a59f05979a8ed60 /src/java.base/share/classes/java/lang/ThreadLocal.java
parent578576f523a176e149b837d124f977013659d1cc (diff)
downloadopenjdk-6ec2cfcc49d8def0d035860f17fd4faeb5eb431c.tar.gz
openjdk-6ec2cfcc49d8def0d035860f17fd4faeb5eb431c.zip
8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
Add internal TerminatingThreadLocal and use it to free cached thread-local direct buffers and nio-fs native buffers Reviewed-by: tonyp, alanb
Diffstat (limited to 'src/java.base/share/classes/java/lang/ThreadLocal.java')
-rw-r--r--src/java.base/share/classes/java/lang/ThreadLocal.java31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/java.base/share/classes/java/lang/ThreadLocal.java b/src/java.base/share/classes/java/lang/ThreadLocal.java
index 3cb657470cc..35529d4a142 100644
--- a/src/java.base/share/classes/java/lang/ThreadLocal.java
+++ b/src/java.base/share/classes/java/lang/ThreadLocal.java
@@ -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 @@ public class ThreadLocal<T> {
}
/**
+ * 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.
*
@@ -180,10 +195,14 @@ public class ThreadLocal<T> {
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
- if (map != null)
+ if (map != null) {
map.set(this, value);
- else
+ } else {
createMap(t, value);
+ }
+ if (this instanceof TerminatingThreadLocal) {
+ TerminatingThreadLocal.register((TerminatingThreadLocal<?>) this);
+ }
return value;
}
@@ -199,10 +218,11 @@ public class ThreadLocal<T> {
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
- if (map != null)
+ if (map != null) {
map.set(this, value);
- else
+ } else {
createMap(t, value);
+ }
}
/**
@@ -218,8 +238,9 @@ public class ThreadLocal<T> {
*/
public void remove() {
ThreadLocalMap m = getMap(Thread.currentThread());
- if (m != null)
+ if (m != null) {
m.remove(this);
+ }
}
/**