|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 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 | +/** |
| 25 | + * @test |
| 26 | + * @bug 8071507 |
| 27 | + * @summary Test that PhantomReferences are cleared when notified. |
| 28 | + * @run main/othervm PhantomReferentClearing |
| 29 | + */ |
| 30 | + |
| 31 | +import java.lang.ref.PhantomReference; |
| 32 | +import java.lang.ref.ReferenceQueue; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +public class PhantomReferentClearing { |
| 37 | + |
| 38 | + private static final long ENQUEUE_TIMEOUT = 1000; // 1 sec, in millis |
| 39 | + |
| 40 | + // P1 & P2 are PhantomReference objects |
| 41 | + // O1 & O2 are objects |
| 42 | + // |
| 43 | + // -> is a strong reference |
| 44 | + // => is a referent reference |
| 45 | + // |
| 46 | + // root -> P1 |
| 47 | + // root -> P2 |
| 48 | + // root -> O1 |
| 49 | + // root -> O2 |
| 50 | + // O1 -> O2 |
| 51 | + // P1 => O1 |
| 52 | + // P2 => O2 |
| 53 | + // |
| 54 | + // (1) Remove root -> O1 and collect. P1 notified, P2 !notified. |
| 55 | + // (2) Remove root -> O2 and collect. |
| 56 | + // |
| 57 | + // If phantom references are cleared when notified, as proposed by |
| 58 | + // 8071507, then P2 should be notified, and the test passes. |
| 59 | + // |
| 60 | + // Otherwise, P2 does not get notified because it remains reachable |
| 61 | + // from O1, which is being retained by P1. This fails the test. |
| 62 | + |
| 63 | + private static final ReferenceQueue<Object> Q1 = new ReferenceQueue<>(); |
| 64 | + private static final ReferenceQueue<Object> Q2 = new ReferenceQueue<>(); |
| 65 | + |
| 66 | + private static volatile Object O2 = new Object(); |
| 67 | + private static volatile List<Object> O1 = new ArrayList<>(); |
| 68 | + static { |
| 69 | + O1.add(O2); |
| 70 | + } |
| 71 | + |
| 72 | + private static final PhantomReference<Object> P1 = new PhantomReference<>(O1, Q1); |
| 73 | + private static final PhantomReference<Object> P2 = new PhantomReference<>(O2, Q2); |
| 74 | + |
| 75 | + public static void main(String[] args) throws InterruptedException { |
| 76 | + |
| 77 | + // Collect, and verify neither P1 or P2 notified. |
| 78 | + System.gc(); |
| 79 | + if (Q1.remove(ENQUEUE_TIMEOUT) != null) { |
| 80 | + throw new RuntimeException("P1 already notified"); |
| 81 | + } else if (Q2.poll() != null) { |
| 82 | + throw new RuntimeException("P2 already notified"); |
| 83 | + } |
| 84 | + |
| 85 | + // Delete root -> O1, collect, verify P1 notified, P2 not notified. |
| 86 | + O1 = null; |
| 87 | + System.gc(); |
| 88 | + if (Q1.remove(ENQUEUE_TIMEOUT) == null) { |
| 89 | + throw new RuntimeException("P1 not notified by O1 deletion"); |
| 90 | + } else if (Q2.remove(ENQUEUE_TIMEOUT) != null) { |
| 91 | + throw new RuntimeException("P2 notified by O1 deletion."); |
| 92 | + } |
| 93 | + |
| 94 | + // Delete root -> O2, collect. P2 should be notified. |
| 95 | + O2 = null; |
| 96 | + System.gc(); |
| 97 | + if (Q2.remove(ENQUEUE_TIMEOUT) == null) { |
| 98 | + throw new RuntimeException("P2 not notified by O2 deletion"); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments