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 /* 26 * @test 27 * @bug 7190310 28 * @summary Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops 29 * @run main/othervm/timeout=600 -Xbatch 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 38 import java.lang.ref.*; 39 40 public class Test7190310 { 41 private static Object str = new Object() { 42 public String toString() { 43 return "The Object"; 44 } 45 46 protected void finalize() throws Throwable { 47 System.out.println("The Object is being finalized"); 48 super.finalize(); 49 } 50 }; 51 private final static ReferenceQueue<Object> rq = 52 new ReferenceQueue<Object>(); 53 private final static WeakReference<Object> wr = 54 new WeakReference<Object>(str, rq); 55 56 public static void main(String[] args) 57 throws InterruptedException { 58 Thread reader = new Thread() { 59 public void run() { 60 while (wr.get() != null) { 61 } 62 System.out.println("wr.get() returned null"); 63 } 64 }; 65 66 Thread queueReader = new Thread() { 67 public void run() { 68 try { 69 Reference<? extends Object> ref = rq.remove(); 70 System.out.println(ref); 71 System.out.println("queueReader returned, ref==wr is " 72 + (ref == wr)); 73 } catch (InterruptedException e) { 74 System.err.println("Sleep interrupted - exiting"); 75 } 76 } 77 }; 78 79 reader.start(); 80 queueReader.start(); 81 82 Thread.sleep(1000); 83 str = null; 84 System.gc(); 85 } 86 } 87