0N/A/*
2362N/A * Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Basic functional test of reference objects
0N/A * @author Mark Reinhold
0N/A */
0N/A
0N/A
0N/Aimport java.lang.ref.*;
0N/Aimport java.util.Vector;
0N/A
0N/A
0N/Apublic class Basic {
0N/A
0N/A static ReferenceQueue q = new ReferenceQueue();
0N/A static ReferenceQueue q2 = new ReferenceQueue();
0N/A static Reference rw, rw2, rp, rp2;
0N/A static Vector keep = new Vector();
0N/A static boolean finalized = false;
0N/A
0N/A public static class ClearFinalizerThread {
0N/A protected void finalize() {
0N/A System.err.println("Cleared finalizer thread");
0N/A }
0N/A };
0N/A
0N/A protected void finalize() {
0N/A Basic.finalized = true;
0N/A System.err.println("Finalized " + this);
0N/A }
0N/A
0N/A public static class Sub { };
0N/A
0N/A Object sub = new Sub();
0N/A
0N/A static void fork(Runnable proc) throws InterruptedException {
0N/A Thread t = new Thread(proc);
0N/A t.start();
0N/A t.join();
0N/A }
0N/A
0N/A static void showReferences() throws InterruptedException {
0N/A fork(new Runnable() {
0N/A public void run() {
0N/A System.err.println("References: W " + rw.get()
0N/A + ", W2 " + rw2.get()
0N/A + ", P " + rp.get()
0N/A + ", P2 " + rp2.get());
0N/A }
0N/A });
0N/A }
0N/A
0N/A static void createNoise() throws InterruptedException {
0N/A fork(new Runnable() {
0N/A public void run() {
0N/A keep.addElement(new PhantomReference(new Object(), q2));
0N/A }
0N/A });
0N/A }
0N/A
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A fork(new Runnable() {
0N/A public void run() {
0N/A Basic s = new Basic();
0N/A rw = new WeakReference(s, q);
0N/A rw2 = new WeakReference(s);
0N/A rp = new PhantomReference(s, q);
0N/A rp2 = new PhantomReference(s.sub, q);
0N/A s = null;
0N/A }
0N/A });
0N/A
0N/A showReferences();
0N/A
0N/A int ndq = 0;
0N/A boolean prevFinalized = false;
0N/A outer:
0N/A for (int i = 1;; i++) {
0N/A Reference r;
0N/A
0N/A createNoise();
0N/A System.err.println("GC " + i);
0N/A Thread.sleep(10);
0N/A System.gc();
0N/A
0N/A showReferences();
0N/A while ((r = q2.poll()) != null) {
0N/A System.err.println("Noise " + r);
0N/A }
0N/A
0N/A /* Cause a dummy object to be finalized, since the finalizer thread
0N/A might retain a reference to the Basic instance after it's been
0N/A finalized (this happens with java_g) */
0N/A if (Basic.finalized && !prevFinalized) {
0N/A fork(new Runnable() {
0N/A public void run() {
0N/A new ClearFinalizerThread();
0N/A }});
0N/A prevFinalized = true;
0N/A }
0N/A
0N/A while ((r = q.poll()) != null) {
0N/A ndq++;
0N/A if (r != null) {
0N/A System.err.println("Dequeued " + r);
0N/A if (ndq == 3) break outer;
0N/A }
0N/A }
0N/A
0N/A if (i >= 10) break;
0N/A
0N/A }
0N/A
0N/A if (ndq != 3) {
0N/A throw new Exception("Expected to dequeue 3 reference objects,"
0N/A + " but only got " + ndq);
0N/A }
0N/A
0N/A if (! Basic.finalized) {
0N/A throw new Exception("Test object not finalized");
0N/A }
0N/A
0N/A }
0N/A
0N/A}