Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/NullQueue.java
38812 views
/*1* Copyright (c) 1999, 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 417869325* @summary Ensure that null queue arguments don't crash the Reference handler26* @author Mark Reinhold27*/2829import java.lang.ref.Reference;30import java.lang.ref.WeakReference;313233public class NullQueue {3435private static Reference r = null;3637private static Thread findThread(String name) {38/* Find reference-handler thread */39ThreadGroup tg = Thread.currentThread().getThreadGroup();40for (ThreadGroup tgn = tg;41tgn != null;42tg = tgn, tgn = tg.getParent());43int nt = tg.activeCount();44Thread[] ts = new Thread[nt];45tg.enumerate(ts);46Thread refHandler = null;47for (int i = 0; i < ts.length; i++) {48if (ts[i].getName().equals(name)) return ts[i];49}50return null;51}5253private static void fork(Runnable proc) throws InterruptedException {54Thread t = new Thread(proc);55t.start();56t.join();57}5859public static void main(String[] args) throws Exception {6061Thread refHandler = findThread("Reference Handler");62if (refHandler == null)63throw new Exception("Couldn't find Reference-handler thread");64if (!refHandler.isAlive())65throw new Exception("Reference-handler thread is not alive");6667/* Invoke a Reference constructor, passing null for the queue */68fork(new Runnable() {69public void run() {70r = new WeakReference(new Object(), null);71}});7273/* Force the reference to be cleared and enqueued by the GC */74for (int i = 0;; i++) {75Thread.sleep(10);76System.gc();77if (r.get() == null) break;78if (i >= 10)79throw new Exception("Couldn't cause weak ref to be cleared");80}8182/* Check that the handler is still alive */83if (!refHandler.isAlive())84throw new Exception("Reference-handler thread died");8586}8788}899091