Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/ReferenceEnqueue.java
38812 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 426831725* @summary Test if Reference.enqueue() works properly with GC26*/2728import java.lang.ref.*;2930public class ReferenceEnqueue {3132public static void main(String args[]) throws Exception {33for (int i=0; i < 5; i++)34new WeakRef().run();35System.out.println("Test passed.");36}3738static class WeakRef {39final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();40final Reference<Object> ref;41final int iterations = 1000;4243WeakRef() {44this.ref = new WeakReference<Object>(new Object(), queue);45}4647void run() throws InterruptedException {48System.gc();49for (int i = 0; i < iterations; i++) {50System.gc();51if (ref.isEnqueued()) {52break;53}5455Thread.sleep(100);56}5758if (ref.isEnqueued() == false) {59// GC have not enqueued refWeak for the timeout period60System.out.println("Reference not enqueued yet");61return;62}6364if (ref.enqueue() == true) {65// enqueue() should return false since66// ref is already enqueued by the GC67throw new RuntimeException("Error: enqueue() returned true;"68+ " expected false");69}7071if (queue.poll() == null) {72// poll() should return ref enqueued by the GC73throw new RuntimeException("Error: poll() returned null;"74+ " expected ref object");75}76}77}78}798081