Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Finalizer.java
40948 views
/*1* Copyright (c) 2001, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share;2425import java.util.Stack;2627/**28* Finalizer performs object finalization when virtual mashine shuts down.29* Finalizer is a thread that acts as a VM shutdown hook.30* This thread will be activated as VM shuts down because of31* invocation of <code>exit()</code> or termination. After activation32* Finalizer just calls <code>finalizeAtExit()</code> method of the specified object.33* The finalizable object should implement interface <code>Finalizable</code>.34*35* @see Finalizable36*/37public class Finalizer {3839/** Finalizer thread to register as a VM shutdown hook. */40private static FinalizerThread finalizerThread = null;4142/** An object to finalize. */43private Finalizable object;4445/**46* Create finalizer for the specified object.47*/48public Finalizer(Finalizable object) {49this.object = object;50}5152/**53* Register finalizer for finalization at VM shutdown.54*/55public void activate() {56if (finalizerThread == null) {57finalizerThread = new FinalizerThread("FinalizerThread for Finalizable objects");58finalizerThread.activate();59}60finalizerThread.add(object);61}6263/**64* Unregister finalizer for finalization at VM shutdown.65*/66public void deactivate() {67if (finalizerThread == null)68return;69finalizerThread.remove(object);70}7172/**73* Static inner thread that is registered as a VM shutdown hook74* and performs finalization of all registered finalizable objects.75*/76private static class FinalizerThread extends Thread {7778/** Stack of objects registered for finalization. */79private Stack<Object> objects = new Stack<Object>();8081/** Make new instance of FinalizerThread with given thread name. */82public FinalizerThread(String threadName) {83super(threadName);84}8586/**87* Push an object to the stack of registered objects.88*/89public void add(Finalizable object) {90objects.push(object);91}9293/**94* Remove an object from the stack of registered objects.95*/96public void remove(Finalizable object) {97objects.remove(object);98}99100/**101* Register finalizer thread as a VM shutdown hook.102*/103public void activate() {104Runtime.getRuntime().addShutdownHook( this );105}106107/**108* Unregister finalizer thread as a VM shutdown hook.109*/110public void deactivate() {111Runtime.getRuntime().removeShutdownHook( this );112}113114/**115* Pop all registered objects from the stack and finalize them.116*/117public void run() {118while (!objects.empty()) {119Finalizable object = (Finalizable)objects.pop();120try {121object.finalizeAtExit();122} catch (ThreadDeath e) {123throw e;124} catch (Throwable ex) {125ex.printStackTrace();126}127}128}129130} // end of FinalizerThread131132} // end of Finalizer133134135