|
1 /* |
|
2 * Copyright 1997-1998 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any 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 { |
|
43 protected void finalize() { |
|
44 System.err.println("Cleared finalizer thread"); |
|
45 } |
|
46 }; |
|
47 |
|
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 |
|
57 static void fork(Runnable proc) throws InterruptedException { |
|
58 Thread t = new Thread(proc); |
|
59 t.start(); |
|
60 t.join(); |
|
61 } |
|
62 |
|
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 |
|
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 |
|
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 |
|
109 showReferences(); |
|
110 while ((r = q2.poll()) != null) { |
|
111 System.err.println("Noise " + r); |
|
112 } |
|
113 |
|
114 /* Cause a dummy object to be finalized, since the finalizer thread |
|
115 might retain a reference to the Basic instance after it's been |
|
116 finalized (this happens with java_g) */ |
|
117 if (Basic.finalized && !prevFinalized) { |
|
118 fork(new Runnable() { |
|
119 public void run() { |
|
120 new ClearFinalizerThread(); |
|
121 }}); |
|
122 prevFinalized = true; |
|
123 } |
|
124 |
|
125 while ((r = q.poll()) != null) { |
|
126 ndq++; |
|
127 if (r != null) { |
|
128 System.err.println("Dequeued " + r); |
|
129 if (ndq == 3) break outer; |
|
130 } |
|
131 } |
|
132 |
|
133 if (i >= 10) break; |
|
134 |
|
135 } |
|
136 |
|
137 if (ndq != 3) { |
|
138 throw new Exception("Expected to dequeue 3 reference objects," |
|
139 + " but only got " + ndq); |
|
140 } |
|
141 |
|
142 if (! Basic.finalized) { |
|
143 throw new Exception("Test object not finalized"); |
|
144 } |
|
145 |
|
146 } |
|
147 |
|
148 } |