Path: blob/master/src/java.base/share/classes/jdk/internal/misc/TerminatingThreadLocal.java
41159 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package jdk.internal.misc;2526import java.util.Collection;27import java.util.Collections;28import java.util.IdentityHashMap;2930/**31* A thread-local variable that is notified when a thread terminates and32* it has been initialized in the terminating thread (even if it was33* initialized with a null value).34*/35public class TerminatingThreadLocal<T> extends ThreadLocal<T> {3637@Override38public void set(T value) {39super.set(value);40register(this);41}4243@Override44public void remove() {45super.remove();46unregister(this);47}4849/**50* Invoked by a thread when terminating and this thread-local has an associated51* value for the terminating thread (even if that value is null), so that any52* native resources maintained by the value can be released.53*54* @param value current thread's value of this thread-local variable55* (may be null but only if null value was explicitly initialized)56*/57protected void threadTerminated(T value) {58}5960// following methods and field are implementation details and should only be61// called from the corresponding code int Thread/ThreadLocal class.6263/**64* Invokes the TerminatingThreadLocal's {@link #threadTerminated()} method65* on all instances registered in current thread.66*/67public static void threadTerminated() {68for (TerminatingThreadLocal<?> ttl : REGISTRY.get()) {69ttl._threadTerminated();70}71}7273private void _threadTerminated() { threadTerminated(get()); }7475/**76* Register given TerminatingThreadLocal77*78* @param tl the ThreadLocal to register79*/80public static void register(TerminatingThreadLocal<?> tl) {81REGISTRY.get().add(tl);82}8384/**85* Unregister given TerminatingThreadLocal86*87* @param tl the ThreadLocal to unregister88*/89private static void unregister(TerminatingThreadLocal<?> tl) {90REGISTRY.get().remove(tl);91}9293/**94* a per-thread registry of TerminatingThreadLocal(s) that have been registered95* but later not unregistered in a particular thread.96*/97public static final ThreadLocal<Collection<TerminatingThreadLocal<?>>> REGISTRY =98new ThreadLocal<>() {99@Override100protected Collection<TerminatingThreadLocal<?>> initialValue() {101return Collections.newSetFromMap(new IdentityHashMap<>(4));102}103};104}105106107