1 /* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4268317 8132306 26 * @summary Test if Reference.enqueue() works properly with GC 27 */ 28 29 import java.lang.ref.*; 30 31 public class ReferenceEnqueue { 32 33 public static void main(String args[]) throws Exception { 34 for (int i=0; i < 5; i++) 35 new WeakRef().run(); 36 System.out.println("Test passed."); 37 } 38 39 static class WeakRef { 40 final ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); 41 final Reference<Object> ref; 42 final int iterations = 1000; 43 44 WeakRef() { 45 this.ref = new WeakReference<Object>(new Object(), queue); 46 } 47 48 void run() throws InterruptedException { 49 System.gc(); 50 for (int i = 0; i < iterations; i++) { 51 System.gc(); 52 if (ref.isEnqueued()) { 53 break; 54 } 55 59 if (ref.isEnqueued() == false) { 60 // GC have not enqueued refWeak for the timeout period 61 System.out.println("Reference not enqueued yet"); 62 return; 63 } 64 65 if (ref.enqueue() == true) { 66 // enqueue() should return false since 67 // ref is already enqueued by the GC 68 throw new RuntimeException("Error: enqueue() returned true;" 69 + " expected false"); 70 } 71 72 if (queue.poll() == null) { 73 // poll() should return ref enqueued by the GC 74 throw new RuntimeException("Error: poll() returned null;" 75 + " expected ref object"); 76 } 77 } 78 } 79 } | 1 /* 2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4268317 8132306 8175797 26 * @summary Test if Reference.enqueue() works properly with GC 27 * @run main ReferenceEnqueue 28 * @run main/othervm -Djdk.ref.disableClearAndEnqueue=true ReferenceEnqueue 29 * @run main/othervm -Djava.security.manager ReferenceEnqueue 30 */ 31 32 import java.lang.ref.*; 33 import java.util.ArrayList; 34 import java.util.List; 35 36 public class ReferenceEnqueue { 37 38 public static void main(String args[]) throws Exception { 39 for (int i=0; i < 5; i++) { 40 new WeakRef().run(); 41 new ExplicitEnqueue().run(); 42 } 43 System.out.println("Test passed."); 44 } 45 46 static class WeakRef { 47 final ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); 48 final Reference<Object> ref; 49 final int iterations = 1000; 50 51 WeakRef() { 52 this.ref = new WeakReference<Object>(new Object(), queue); 53 } 54 55 void run() throws InterruptedException { 56 System.gc(); 57 for (int i = 0; i < iterations; i++) { 58 System.gc(); 59 if (ref.isEnqueued()) { 60 break; 61 } 62 66 if (ref.isEnqueued() == false) { 67 // GC have not enqueued refWeak for the timeout period 68 System.out.println("Reference not enqueued yet"); 69 return; 70 } 71 72 if (ref.enqueue() == true) { 73 // enqueue() should return false since 74 // ref is already enqueued by the GC 75 throw new RuntimeException("Error: enqueue() returned true;" 76 + " expected false"); 77 } 78 79 if (queue.poll() == null) { 80 // poll() should return ref enqueued by the GC 81 throw new RuntimeException("Error: poll() returned null;" 82 + " expected ref object"); 83 } 84 } 85 } 86 87 static class ExplicitEnqueue { 88 final ReferenceQueue<Object> queue = new ReferenceQueue<>(); 89 final List<Reference<Object>> refs = new ArrayList<>(); 90 final int iterations = 1000; 91 final boolean disableClearAndEnqueue = 92 Boolean.parseBoolean("jdk.ref.disableClearAndEnqueue"); 93 94 ExplicitEnqueue() { 95 this.refs.add(new SoftReference<>(new Object(), queue)); 96 this.refs.add(new WeakReference<>(new Object(), queue)); 97 this.refs.add(new PhantomReference<>(new Object(), queue)); 98 } 99 100 void run() throws InterruptedException { 101 for (Reference<Object> ref : refs) { 102 if (ref.enqueue() == false) { 103 throw new RuntimeException("Error: enqueue failed"); 104 } 105 if (disableClearAndEnqueue && ref.get() == null) { 106 throw new RuntimeException("Error: clearing should be disabled"); 107 } 108 if (!disableClearAndEnqueue && ref.get() != null) { 109 throw new RuntimeException("Error: referent must be cleared"); 110 } 111 } 112 113 System.gc(); 114 for (int i = 0; refs.size() > 0 && i < iterations; i++) { 115 Reference<Object> ref = (Reference<Object>)queue.poll(); 116 if (ref == null) { 117 System.gc(); 118 Thread.sleep(100); 119 continue; 120 } 121 122 if (refs.remove(ref) == false) { 123 throw new RuntimeException("Error: unknown reference " + ref); 124 } 125 } 126 127 if (!refs.isEmpty()) { 128 throw new RuntimeException("Error: not all references are removed"); 129 } 130 } 131 } 132 } |