Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/EnqueuePollRace.java
38813 views
/*1* Copyright (c) 2013, 2014, 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 801489025* @summary Verify that a race between ReferenceQueue.enqueue() and poll() does not occur.26* @author [email protected]27* @run main/othervm -Xmx10M EnqueuePollRace28*/2930import java.lang.ref.*;3132public class EnqueuePollRace {3334public static void main(String args[]) throws Exception {35new WeakRef().run();36System.out.println("Test passed.");37}3839static class WeakRef {40ReferenceQueue<Object> queue = new ReferenceQueue<Object>();41final int numReferences = 100;42final Reference refs[] = new Reference[numReferences];43final int iterations = 1000;4445void run() throws InterruptedException {46for (int i = 0; i < iterations; i++) {47queue = new ReferenceQueue<Object>();4849for (int j = 0; j < refs.length; j++) {50refs[j] = new WeakReference(new Object(), queue);51}5253System.gc(); // enqueues references into the list of discovered references5455// now manually enqueue all of them56for (int j = 0; j < refs.length; j++) {57refs[j].enqueue();58}59// and get them back. There should be exactly numReferences60// entries in the queue now.61int foundReferences = 0;62while (queue.poll() != null) {63foundReferences++;64}6566if (foundReferences != refs.length) {67throw new RuntimeException("Got " + foundReferences + " references in the queue, but expected " + refs.length);68}69}70}71}72}737475