1 /*
2  * Copyright (c) 2012, 2013, 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 /*
25  * @test
26  * @bug 7190310
27  * @summary Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
28  *
29  * @run main/othervm/timeout=600 -Xbatch compiler.c2.Test7190310
30  */
31 
32 /*
33  * Note bug exhibits as infinite loop, timeout is helpful.
34  * It should normally finish pretty quickly, but on some especially slow machines
35  * it may not.  The companion _unsafe test lacks a timeout, but that is okay.
36  */
37 package compiler.c2;
38 
39 import java.lang.ref.Reference;
40 import java.lang.ref.ReferenceQueue;
41 import java.lang.ref.WeakReference;
42 
43 public class Test7190310 {
44     private static Object str = new Object() {
45         public String toString() {
46             return "The Object";
47         }
48 
49         protected void finalize() throws Throwable {
50             System.out.println("The Object is being finalized");
51             super.finalize();
52         }
53     };
54     private final static ReferenceQueue<Object> rq =
55             new ReferenceQueue<Object>();
56     private final static WeakReference<Object> wr =
57             new WeakReference<Object>(str, rq);
58 
main(String[] args)59     public static void main(String[] args)
60             throws InterruptedException {
61         Thread reader = new Thread() {
62             public void run() {
63                 while (wr.get() != null) {
64                 }
65                 System.out.println("wr.get() returned null");
66             }
67         };
68 
69         Thread queueReader = new Thread() {
70             public void run() {
71                 try {
72                     Reference<? extends Object> ref = rq.remove();
73                     System.out.println(ref);
74                     System.out.println("queueReader returned, ref==wr is "
75                             + (ref == wr));
76                 } catch (InterruptedException e) {
77                     System.err.println("Sleep interrupted - exiting");
78                 }
79             }
80         };
81 
82         reader.start();
83         queueReader.start();
84 
85         Thread.sleep(1000);
86         str = null;
87         System.gc();
88     }
89 }
90 
91