Path: blob/master/jcl/src/java.base/share/classes/jdk/internal/ref/CleanerShutdown.java
12575 views
/*[INCLUDE-IF Sidecar19-SE]*/1/*******************************************************************************2* Copyright (c) 2016, 2020 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception21*******************************************************************************/22package jdk.internal.ref;2324import jdk.internal.ref.CleanerFactory;2526import java.lang.ref.Cleaner;27import java.lang.ref.Cleaner.Cleanable;28import java.lang.reflect.InvocationTargetException;29import java.lang.reflect.Method;30import java.security.AccessController;31import java.security.PrivilegedAction;3233@SuppressWarnings("javadoc")34public class CleanerShutdown {3536public static void shutdownCleaner() {37Cleaner commonCleaner = CleanerFactory.cleaner();38CleanerImpl commonCleanerImpl = CleanerImpl.getCleanerImpl(commonCleaner);39Cleanable ref = null;4041while ((ref = (Cleanable) commonCleanerImpl.queue.poll()) != null) {42try {43ref.clean();44} catch (Throwable t) {45/* do nothing */46}47}4849try {50Method phantomRemove = PhantomCleanable.class.getDeclaredMethod("remove", (Class<?>[]) null); //$NON-NLS-1$51AccessController.doPrivileged((PrivilegedAction<Void>) () -> {52phantomRemove.setAccessible(true);53return null;54});55while(!commonCleanerImpl.phantomCleanableList.isListEmpty()) {56phantomRemove.invoke(commonCleanerImpl.phantomCleanableList, (Object[]) null);57}58} catch (NoSuchMethodException59| SecurityException60| IllegalAccessException61| IllegalArgumentException62| InvocationTargetException e)63{64/* should not fail */65e.printStackTrace();66}67}68}697071