< prev index next >
src/java.base/share/classes/java/lang/ThreadLocal.java
Print this page
rev 50167 : 8202788: Explicitly reclaim cached thread-local direct buffers at thread exit
Reviewed-by:
*** 26,35 ****
--- 26,36 ----
package java.lang;
import java.lang.ref.*;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
+ import jdk.internal.misc.JdkThreadLocal;
/**
* This class provides thread-local variables. These variables differ from
* their normal counterparts in that each thread that accesses one (via its
* {@code get} or {@code set} method) has its own, independently initialized
*** 82,91 ****
--- 83,109 ----
* are used by the same threads, while remaining well-behaved in
* less common cases.
*/
private final int threadLocalHashCode = nextHashCode();
+ static void callThreadTerminated(ThreadLocalMap map) {
+ if (map == null) {
+ return;
+ }
+
+ // We expect to have a very small number of hooks and
+ // potentially a lot of entries in the map. So, it should be
+ // faster to iterate over the hooks and do look-ups on the map
+ // instead of iterating over the map.
+ JdkThreadLocal.forEach(tl -> {
+ final ThreadLocalMap.Entry entry = map.getEntry(tl);
+ if (entry != null) {
+ tl.callThreadTerminated(entry.value);
+ }
+ });
+ }
+
/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
*/
private static AtomicInteger nextHashCode =
< prev index next >