Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/gc/FinalizationRunner.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import java.util.concurrent.CountDownLatch;2425import jdk.test.lib.dcmd.CommandExecutor;26import jdk.test.lib.dcmd.JMXExecutor;2728public class FinalizationRunner {29public static final String FAILED = "Failed";30public static final String PASSED = "Passed";3132static volatile boolean wasFinalized = false;33private static final CountDownLatch finRunLatch = new CountDownLatch(1);34private static final CountDownLatch finBlockLatch = new CountDownLatch(1);3536static class MyObject {37@Override38protected void finalize() {39if (Thread.currentThread().getName().equals("Finalizer")) {40try {41System.out.println("inside the regular finalizer thread; blocking");42// 'regular' finalizer thread is ready to be effectively blocked -43// we can continue with the GC.run_finalization test44finRunLatch.countDown();45// prevent the 'regular' finalizer from finalizing this instance46// until the GC.run_finalization has had its chance to do so47finBlockLatch.await();48} catch (InterruptedException e) {49}50} else {51if (Thread.currentThread().getName().equals("Secondary finalizer")) {52System.out.println("finalizing the test instance");53// finalizing on behalf of GC.run_finalization -54// unblock the 'regular' finalizer and the test main method55wasFinalized = true;56} else {57fail("Unexpected finalizer thread name: " +58Thread.currentThread().getName());59}60finBlockLatch.countDown();61}62}63}6465// this instance will be used to provoke the regular finalization66// so the finalizer thread can be blocked for the duration of67// GC.run_finalization test68public static MyObject o1;6970// this instance will be used to perform the GC.run_finalization test71public static MyObject o2;7273private static void run(CommandExecutor executor) {74o2 = new MyObject();75o2 = null;76System.out.println("running GC.run_finalization");77System.gc();78executor.execute("GC.run_finalization");7980System.out.println("Waiting for finalization");8182try {83finBlockLatch.await();84if (wasFinalized) {85System.out.println(PASSED + ": Object was finalized");86} else {87fail("Object was not finalized");88}89} catch (InterruptedException e) {90fail("Interrupted while waiting for finalization", e);91}92}9394public static void main(String ... args) {95System.out.println("\n=== FinalizationRunner");96try {97blockFinalizerThread();9899Runtime.getRuntime().addShutdownHook(new Thread(()->{100run(new JMXExecutor());101}));102} catch (InterruptedException e) {103fail("Interrupted while trying to block the finalizer thread", e);104}105}106107private static void blockFinalizerThread() throws InterruptedException {108System.out.println("trying to block the finalizer thread");109o1 = new MyObject();110o1 = null;111System.gc();112finRunLatch.await();113}114115private static void fail(String msg, Exception e) {116fail(msg);117e.printStackTrace(System.err);118}119120private static void fail(String msg) {121System.err.println(FAILED + ": " + msg);122}123}124125126