jdk/test/java/lang/ref/ReferenceEnqueue.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 10895 7434064aba55
child 32219 e613f4863973
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     1
/*
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     4
 *
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     8
 *
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    13
 * accompanied this code).
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    14
 *
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    18
 *
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    21
 * questions.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    22
 */
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    23
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    24
/* @test
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    25
 * @bug 4268317
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    26
 * @summary Test if Reference.enqueue() works properly with GC
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    27
 */
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    28
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    29
import java.lang.ref.*;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    30
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    31
public class ReferenceEnqueue {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    32
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    33
    public static void main(String args[]) throws Exception {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    34
        for (int i=0; i < 5; i++)
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    35
            new WeakRef().run();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    36
        System.out.println("Test passed.");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    37
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    38
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    39
    static class WeakRef {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    40
        final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    41
        final Reference<Object> ref;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    42
        final int iterations = 1000;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    43
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    44
        WeakRef() {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    45
            this.ref = new WeakReference<Object>(new Object(), queue);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    46
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    47
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    48
        void run() throws InterruptedException {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    49
            System.gc();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    50
            for (int i = 0; i < iterations; i++) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    51
                System.gc();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    52
                if (ref.isEnqueued()) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    53
                    break;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    54
                }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    55
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    56
                Thread.sleep(100);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    57
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    58
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    59
            if (ref.isEnqueued() == false) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    60
                // GC have not enqueued refWeak for the timeout period
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    61
                System.out.println("Reference not enqueued yet");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    62
                return;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    63
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    64
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    65
            if (ref.enqueue() == true) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    66
                // enqueue() should return false since
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    67
                // ref is already enqueued by the GC
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    68
                throw new RuntimeException("Error: enqueue() returned true;"
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    69
                        + " expected false");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    70
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    71
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    72
            if (queue.poll() == null) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    73
                // poll() should return ref enqueued by the GC
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    74
                throw new RuntimeException("Error: poll() returned null;"
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    75
                        + " expected ref object");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    76
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    77
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    78
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    79
}