Path: blob/master/test/jdk/java/lang/System/finalization/FinThreads.java
41153 views
/*1* Copyright (c) 1998, 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*/2223/* @test24@bug 402689525@summary Ensure that System.runFinalization does not run finalizers in the26thread that invokes it27*/282930public class FinThreads {3132static Thread mainThread;3334static Object lock = new Object(); /* Protects following two fields */35static Thread finalizerThread = null;36static Thread finalizedBy = null;373839static class Foo {4041boolean catchFinalizer = false;4243/* Instances are only created in an auxiliary thread, in order to44guard against stray references from the current thread's stack45*/46public static void create(final boolean catchFinalizer)47throws InterruptedException48{49Thread t = new Thread(new Runnable() {50public void run() {51new Foo(catchFinalizer);52}});53t.start();54t.join();55}5657public Foo(boolean catchFinalizer) {58this.catchFinalizer = catchFinalizer;59}6061public void finalize() throws InterruptedException {62if (catchFinalizer) {63boolean gotFinalizer = false;64synchronized (lock) {65if (finalizerThread == null) {66finalizerThread = Thread.currentThread();67gotFinalizer = true;68}69}70if (gotFinalizer) {71System.err.println("Caught finalizer thread; sleeping...");72Thread.sleep(Long.MAX_VALUE);73}74} else {75synchronized (lock) {76finalizedBy = Thread.currentThread();77}78System.err.println("Test object finalized by " + finalizedBy);79}80}8182}838485static void alarm(final Thread sleeper, final long delay)86throws InterruptedException87{88Thread t = new Thread(new Runnable() {89public void run() {90try {91Thread.sleep(delay);92System.err.println("Waking " + sleeper);93sleeper.interrupt();94} catch (InterruptedException x) { }95}});96t.setDaemon(true);97t.start();98}99100101public static void main(String[] args) throws Exception {102103mainThread = Thread.currentThread();104105/* Find the finalizer thread and put it to sleep */106for (;;) {107Foo.create(true);108System.gc();109synchronized (lock) {110if (finalizerThread != null) break;111}112}113114/* Now create a finalizable object and run finalization */115alarm(finalizerThread, 5000);116Foo.create(false);117for (;;) {118System.gc();119System.runFinalization();120synchronized (lock) {121if (finalizedBy != null) break;122}123}124125if (finalizedBy == mainThread)126throw new Exception("Finalizer run in main thread");127128}129130}131132133