1 /*
2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25  * @summary Basic functional test of reference objects
26  * @author Mark Reinhold
27  */
28 
29 
30 import java.lang.ref.*;
31 import java.util.Vector;
32 
33 
34 public class Basic {
35 
36     static ReferenceQueue q = new ReferenceQueue();
37     static ReferenceQueue q2 = new ReferenceQueue();
38     static Reference rw, rw2, rp, rp2;
39     static Vector keep = new Vector();
40     static boolean finalized = false;
41 
42     public static class ClearFinalizerThread {
finalize()43         protected void finalize() {
44             System.err.println("Cleared finalizer thread");
45         }
46     };
47 
finalize()48     protected void finalize() {
49         Basic.finalized = true;
50         System.err.println("Finalized " + this);
51     }
52 
53     public static class Sub { };
54 
55     Object sub = new Sub();
56 
fork(Runnable proc)57     static void fork(Runnable proc) throws InterruptedException {
58         Thread t = new Thread(proc);
59         t.start();
60         t.join();
61     }
62 
showReferences()63     static void showReferences() throws InterruptedException {
64         fork(new Runnable() {
65             public void run() {
66                 System.err.println("References: W " + rw.get()
67                                    + ", W2 " + rw2.get()
68                                    + ", P " + rp.get()
69                                    + ", P2 " + rp2.get());
70             }
71         });
72     }
73 
createNoise()74     static void createNoise() throws InterruptedException {
75         fork(new Runnable() {
76             public void run() {
77                 keep.addElement(new PhantomReference(new Object(), q2));
78             }
79         });
80     }
81 
82 
main(String[] args)83     public static void main(String[] args) throws Exception {
84 
85         fork(new Runnable() {
86             public void run() {
87                 Basic s = new Basic();
88                 rw = new WeakReference(s, q);
89                 rw2 = new WeakReference(s);
90                 rp = new PhantomReference(s, q);
91                 rp2 = new PhantomReference(s.sub, q);
92                 s = null;
93             }
94         });
95 
96         showReferences();
97 
98         int ndq = 0;
99         boolean prevFinalized = false;
100     outer:
101         for (int i = 1;; i++) {
102             Reference r;
103 
104             createNoise();
105             System.err.println("GC " + i);
106             Thread.sleep(10);
107             System.gc();
108             System.runFinalization();
109 
110             showReferences();
111             while ((r = q2.poll()) != null) {
112                 System.err.println("Noise " + r);
113             }
114 
115             /* Cause a dummy object to be finalized, since the finalizer thread
116                might retain a reference to the Basic instance after it's been
117                finalized (this happens with java_g) */
118             if (Basic.finalized && !prevFinalized) {
119                 fork(new Runnable() {
120                     public void run() {
121                         new ClearFinalizerThread();
122                     }});
123                 prevFinalized = true;
124             }
125 
126             while ((r = q.poll()) != null) {
127                 ndq++;
128                 if (r != null) {
129                     System.err.println("Dequeued " + r);
130                     if (ndq == 3) break outer;
131                 }
132             }
133 
134             if (i >= 10) break;
135 
136         }
137 
138         if (ndq != 3) {
139             throw new Exception("Expected to dequeue 3 reference objects,"
140                                 + " but only got " + ndq);
141         }
142 
143         if (! Basic.finalized) {
144             throw new Exception("Test object not finalized");
145         }
146 
147     }
148 
149 }
150