< prev index next >

test/java/lang/ref/ReferenceEnqueue.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 20,40 **** * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! * @bug 4268317 8132306 * @summary Test if Reference.enqueue() works properly with GC */ import java.lang.ref.*; public class ReferenceEnqueue { public static void main(String args[]) throws Exception { ! for (int i=0; i < 5; i++) new WeakRef().run(); System.out.println("Test passed."); } static class WeakRef { final ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); --- 20,47 ---- * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! * @bug 4268317 8132306 8175797 * @summary Test if Reference.enqueue() works properly with GC + * @run main ReferenceEnqueue + * @run main/othervm -Djdk.ref.disableClearAndEnqueue=true ReferenceEnqueue + * @run main/othervm -Djava.security.manager ReferenceEnqueue */ import java.lang.ref.*; + import java.util.ArrayList; + import java.util.List; public class ReferenceEnqueue { public static void main(String args[]) throws Exception { ! for (int i=0; i < 5; i++) { new WeakRef().run(); + new ExplicitEnqueue().run(); + } System.out.println("Test passed."); } static class WeakRef { final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
*** 74,79 **** --- 81,132 ---- throw new RuntimeException("Error: poll() returned null;" + " expected ref object"); } } } + + static class ExplicitEnqueue { + final ReferenceQueue<Object> queue = new ReferenceQueue<>(); + final List<Reference<Object>> refs = new ArrayList<>(); + final int iterations = 1000; + final boolean disableClearAndEnqueue = + Boolean.parseBoolean("jdk.ref.disableClearAndEnqueue"); + + ExplicitEnqueue() { + this.refs.add(new SoftReference<>(new Object(), queue)); + this.refs.add(new WeakReference<>(new Object(), queue)); + this.refs.add(new PhantomReference<>(new Object(), queue)); + } + + void run() throws InterruptedException { + for (Reference<Object> ref : refs) { + if (ref.enqueue() == false) { + throw new RuntimeException("Error: enqueue failed"); + } + if (disableClearAndEnqueue && ref.get() == null) { + throw new RuntimeException("Error: clearing should be disabled"); + } + if (!disableClearAndEnqueue && ref.get() != null) { + throw new RuntimeException("Error: referent must be cleared"); + } + } + + System.gc(); + for (int i = 0; refs.size() > 0 && i < iterations; i++) { + Reference<Object> ref = (Reference<Object>)queue.poll(); + if (ref == null) { + System.gc(); + Thread.sleep(100); + continue; + } + + if (refs.remove(ref) == false) { + throw new RuntimeException("Error: unknown reference " + ref); + } + } + + if (!refs.isEmpty()) { + throw new RuntimeException("Error: not all references are removed"); + } + } + } }
< prev index next >