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