/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.internal.misc; import java.util.Collection; import java.util.HashSet; /** * A thread-local variable that is notified when a thread terminates and * it has been initialized in the terminating thread (even if it was * initialized with a null value). */ public class JdkThreadLocal extends ThreadLocal { @Override protected final T initialValue() { T value = computeInitialValue(); REGISTRY.get().add(this); return value; } @Override public void set(T value) { super.set(value); REGISTRY.get().add(this); } @Override public void remove() { super.remove(); REGISTRY.get().remove(this); } /** * Just like {@link ThreadLocal#initialValue()} but subclasses of {@link JdkThreadLocal} * must override this method instead as {@link #initialValue()} is final. * * @return the initial value for this thread-local */ protected T computeInitialValue() { return null; } /** * Invoked by a thread when terminating and this thread-local has an associated * value for the terminating thread (even if that value is null), so that any * native resources maintained by the value can be released. */ protected void threadTerminated() { } // following method and field are implementation details and should only be // called from the corresponding code int Thread class. /** * Invokes the JdkThreadLocal's threadTerminated method on all instances of * given collection. */ public static void threadTerminated(Collection> jdkTls) { if (jdkTls != null) { for (JdkThreadLocal jdkTl : jdkTls) { jdkTl.threadTerminated(); } } } /** * a per-thread registry of JdkThreadLocal(s) that have been initialized * in a particular thread. */ public static final ThreadLocal>> REGISTRY = new ThreadLocal<>() { @Override protected Collection> initialValue() { return new HashSet<>(4); } }; }