3965N/A/*
4176N/A * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3965N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3965N/A *
3965N/A * This code is free software; you can redistribute it and/or modify it
3965N/A * under the terms of the GNU General Public License version 2 only, as
3965N/A * published by the Free Software Foundation.
3965N/A *
3965N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3965N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3965N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3965N/A * version 2 for more details (a copy is included in the LICENSE file that
3965N/A * accompanied this code).
3965N/A *
3965N/A * You should have received a copy of the GNU General Public License version
3965N/A * 2 along with this work; if not, write to the Free Software Foundation,
3965N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3965N/A *
3965N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3965N/A * or visit www.oracle.com if you need additional information or have any
3965N/A * questions.
3965N/A *
3965N/A */
3965N/A
3965N/A/*
4176N/A * @test
4176N/A * @bug 7190310
4176N/A * @summary Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
4176N/A * @run main/othervm/timeout=600 -Xbatch Test7190310
4176N/A */
4176N/A
4176N/A/*
4176N/A * Note bug exhibits as infinite loop, timeout is helpful.
4176N/A * It should normally finish pretty quickly, but on some especially slow machines
4176N/A * it may not. The companion _unsafe test lacks a timeout, but that is okay.
3965N/A */
3965N/A
3965N/Aimport java.lang.ref.*;
3965N/A
3965N/Apublic class Test7190310 {
3965N/A private static Object str = new Object() {
3965N/A public String toString() {
3965N/A return "The Object";
3965N/A }
3965N/A
3965N/A protected void finalize() throws Throwable {
3965N/A System.out.println("The Object is being finalized");
3965N/A super.finalize();
3965N/A }
3965N/A };
3965N/A private final static ReferenceQueue<Object> rq =
3965N/A new ReferenceQueue<Object>();
3965N/A private final static WeakReference<Object> wr =
3965N/A new WeakReference<Object>(str, rq);
3965N/A
3965N/A public static void main(String[] args)
3965N/A throws InterruptedException {
3965N/A Thread reader = new Thread() {
3965N/A public void run() {
3965N/A while (wr.get() != null) {
3965N/A }
3965N/A System.out.println("wr.get() returned null");
3965N/A }
3965N/A };
3965N/A
3965N/A Thread queueReader = new Thread() {
3965N/A public void run() {
3965N/A try {
3965N/A Reference<? extends Object> ref = rq.remove();
3965N/A System.out.println(ref);
3965N/A System.out.println("queueReader returned, ref==wr is "
3965N/A + (ref == wr));
3965N/A } catch (InterruptedException e) {
3965N/A System.err.println("Sleep interrupted - exiting");
3965N/A }
3965N/A }
3965N/A };
3965N/A
3965N/A reader.start();
3965N/A queueReader.start();
3965N/A
3965N/A Thread.sleep(1000);
3965N/A str = null;
3965N/A System.gc();
3965N/A }
3965N/A}
3965N/A