Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/Basic.java
38812 views
/*1* Copyright (c) 1997, 2012, 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* @summary Basic functional test of reference objects25* @author Mark Reinhold26*/272829import java.lang.ref.*;30import java.util.Vector;313233public class Basic {3435static ReferenceQueue q = new ReferenceQueue();36static ReferenceQueue q2 = new ReferenceQueue();37static Reference rw, rw2, rp, rp2;38static Vector keep = new Vector();39static boolean finalized = false;4041public static class ClearFinalizerThread {42protected void finalize() {43System.err.println("Cleared finalizer thread");44}45};4647protected void finalize() {48Basic.finalized = true;49System.err.println("Finalized " + this);50}5152public static class Sub { };5354Object sub = new Sub();5556static void fork(Runnable proc) throws InterruptedException {57Thread t = new Thread(proc);58t.start();59t.join();60}6162static void showReferences() throws InterruptedException {63fork(new Runnable() {64public void run() {65System.err.println("References: W " + rw.get()66+ ", W2 " + rw2.get()67+ ", P " + rp.get()68+ ", P2 " + rp2.get());69}70});71}7273static void createNoise() throws InterruptedException {74fork(new Runnable() {75public void run() {76keep.addElement(new PhantomReference(new Object(), q2));77}78});79}808182public static void main(String[] args) throws Exception {8384fork(new Runnable() {85public void run() {86Basic s = new Basic();87rw = new WeakReference(s, q);88rw2 = new WeakReference(s);89rp = new PhantomReference(s, q);90rp2 = new PhantomReference(s.sub, q);91s = null;92}93});9495showReferences();9697int ndq = 0;98boolean prevFinalized = false;99outer:100for (int i = 1;; i++) {101Reference r;102103createNoise();104System.err.println("GC " + i);105Thread.sleep(10);106System.gc();107System.runFinalization();108109showReferences();110while ((r = q2.poll()) != null) {111System.err.println("Noise " + r);112}113114/* Cause a dummy object to be finalized, since the finalizer thread115might retain a reference to the Basic instance after it's been116finalized (this happens with java_g) */117if (Basic.finalized && !prevFinalized) {118fork(new Runnable() {119public void run() {120new ClearFinalizerThread();121}});122prevFinalized = true;123}124125while ((r = q.poll()) != null) {126ndq++;127if (r != null) {128System.err.println("Dequeued " + r);129if (ndq == 3) break outer;130}131}132133if (i >= 10) break;134135}136137if (ndq != 3) {138throw new Exception("Expected to dequeue 3 reference objects,"139+ " but only got " + ndq);140}141142if (! Basic.finalized) {143throw new Exception("Test object not finalized");144}145146}147148}149150151