2
|
1 |
/*
|
|
2 |
* Copyright 1999 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 |
* @bug 4178693
|
|
26 |
* @summary Ensure that null queue arguments don't crash the Reference handler
|
|
27 |
* @author Mark Reinhold
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.lang.ref.Reference;
|
|
31 |
import java.lang.ref.WeakReference;
|
|
32 |
|
|
33 |
|
|
34 |
public class NullQueue {
|
|
35 |
|
|
36 |
private static Reference r = null;
|
|
37 |
|
|
38 |
private static Thread findThread(String name) {
|
|
39 |
/* Find reference-handler thread */
|
|
40 |
ThreadGroup tg = Thread.currentThread().getThreadGroup();
|
|
41 |
for (ThreadGroup tgn = tg;
|
|
42 |
tgn != null;
|
|
43 |
tg = tgn, tgn = tg.getParent());
|
|
44 |
int nt = tg.activeCount();
|
|
45 |
Thread[] ts = new Thread[nt];
|
|
46 |
tg.enumerate(ts);
|
|
47 |
Thread refHandler = null;
|
|
48 |
for (int i = 0; i < ts.length; i++) {
|
|
49 |
if (ts[i].getName().equals(name)) return ts[i];
|
|
50 |
}
|
|
51 |
return null;
|
|
52 |
}
|
|
53 |
|
|
54 |
private static void fork(Runnable proc) throws InterruptedException {
|
|
55 |
Thread t = new Thread(proc);
|
|
56 |
t.start();
|
|
57 |
t.join();
|
|
58 |
}
|
|
59 |
|
|
60 |
public static void main(String[] args) throws Exception {
|
|
61 |
|
|
62 |
Thread refHandler = findThread("Reference Handler");
|
|
63 |
if (refHandler == null)
|
|
64 |
throw new Exception("Couldn't find Reference-handler thread");
|
|
65 |
if (!refHandler.isAlive())
|
|
66 |
throw new Exception("Reference-handler thread is not alive");
|
|
67 |
|
|
68 |
/* Invoke a Reference constructor, passing null for the queue */
|
|
69 |
fork(new Runnable() {
|
|
70 |
public void run() {
|
|
71 |
r = new WeakReference(new Object(), null);
|
|
72 |
}});
|
|
73 |
|
|
74 |
/* Force the reference to be cleared and enqueued by the GC */
|
|
75 |
for (int i = 0;; i++) {
|
|
76 |
Thread.sleep(10);
|
|
77 |
System.gc();
|
|
78 |
if (r.get() == null) break;
|
|
79 |
if (i >= 10)
|
|
80 |
throw new Exception("Couldn't cause weak ref to be cleared");
|
|
81 |
}
|
|
82 |
|
|
83 |
/* Check that the handler is still alive */
|
|
84 |
if (!refHandler.isAlive())
|
|
85 |
throw new Exception("Reference-handler thread died");
|
|
86 |
|
|
87 |
}
|
|
88 |
|
|
89 |
}
|