jdk/test/java/lang/ref/ReferenceEnqueuePending.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 23010 6dadb192ad81
child 35971 6ff2a27164f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 10895
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21596
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
10895
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 4243978
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    26
 * @summary Test if Reference.enqueue() works properly with pending references
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
import java.lang.ref.*;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    29
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    30
public class ReferenceEnqueuePending {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    31
    static class NumberedWeakReference extends WeakReference<Integer> {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    32
        //  Add an integer to identify the weak reference object.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    33
        int number;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    34
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    35
        NumberedWeakReference(Integer referent, ReferenceQueue<Integer> q, int i) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    36
            super(referent, q);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    37
            number = i;
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
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    40
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    41
    static final boolean debug = System.getProperty("test.debug") != null;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    42
    static final int iterations = 1000;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 23010
diff changeset
    43
    static final int gc_trigger = 99;
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    44
    static int[] a = new int[2 * iterations];
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    45
    // Keep all weak references alive with the following array.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    46
    static NumberedWeakReference[] b = new NumberedWeakReference[iterations];
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
    public static void main(String[] argv) throws Exception {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    49
        if (debug) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    50
            System.out.println("Starting the test.");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    51
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    52
        // Raise thread priority to match the referenceHandler
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    53
        // priority, so that they can race also on a uniprocessor.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    54
        raisePriority();
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
        ReferenceQueue<Integer> refQueue = new ReferenceQueue<>();
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
        // Our objective is to let the mutator enqueue
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    59
        // a Reference object that may already be in the
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    60
        // pending state because of having been identified
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    61
        // as weakly reachable at a previous garbage collection.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    62
        // To this end, we create many Reference objects, each with a
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    63
        // a unique integer object as its referant.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    64
        // We let the referents become eligible for collection,
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    65
        // while racing with the garbage collector which may
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    66
        // have pended some of these Reference objects.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    67
        // Finally we check that all of the Reference objects
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    68
        // end up on the their queue. The test was originally
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    69
        // submitted to show that such races could break the
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    70
        // pending list and/or the reference queue, because of sharing
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    71
        // the same link ("next") for maintaining both lists, thus
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    72
        // losing some of the Reference objects on either queue.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    73
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    74
        Integer obj = new Integer(0);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    75
        NumberedWeakReference weaky = new NumberedWeakReference(obj, refQueue, 0);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    76
        for (int i = 1; i < iterations; i++) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    77
            // Create a new object, dropping the onlY strong reference to
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    78
            // the previous Integer object.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    79
            obj = new Integer(i);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    80
            // Trigger gc each gc_trigger iterations.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    81
            if ((i % gc_trigger) == 0) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    82
                forceGc(0);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    83
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    84
            // Enqueue every other weaky.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    85
            if ((i % 2) == 0) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    86
                weaky.enqueue();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    87
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    88
            // Remember the Reference objects, for testing later.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    89
            b[i - 1] = weaky;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    90
            // Get a new weaky for the Integer object just
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    91
            // created, which may be explicitly enqueued in
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    92
            // our next trip around the loop.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    93
            weaky = new NumberedWeakReference(obj, refQueue, i);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    94
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    95
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    96
        // Do a final collection to discover and process all
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    97
        // Reference objects created above, allowing enough time
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    98
        // for the ReferenceHandler thread to queue the References.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
    99
        forceGc(100);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   100
        forceGc(100);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   101
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   102
        // Verify that all WeakReference objects ended up queued.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   103
        checkResult(refQueue, obj, iterations-1);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   104
        System.out.println("Test passed.");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   105
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   106
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   107
    private static void checkResult(ReferenceQueue<Integer> queue,
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   108
                                    Integer obj,
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   109
                                    int expected) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   110
        if (debug) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   111
            System.out.println("Reading the queue");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   112
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   113
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   114
        // Empty the queue and record numbers into a[];
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   115
        NumberedWeakReference weakRead = (NumberedWeakReference) queue.poll();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   116
        int length = 0;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   117
        while (weakRead != null) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   118
            a[length++] = weakRead.number;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   119
            weakRead = (NumberedWeakReference) queue.poll();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   120
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   121
        if (debug) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   122
            System.out.println("Reference Queue had " + length + " elements");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   123
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   124
        // Use the last Reference object of those created above, so as to keep it "alive".
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   125
        System.out.println("I must write " + obj + " to prevent compiler optimizations.");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   126
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   127
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   128
        // verify the queued references: all but the last Reference object
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   129
        // should have been in the queue.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   130
        if (debug) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   131
            System.out.println("Start of final check");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   132
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   133
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   134
        // Sort the first "length" elements in array "a[]".
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   135
        sort(length);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   136
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   137
        boolean fail = (length != expected);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   138
        for (int i = 0; i < length; i++) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   139
            if (a[i] != i) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   140
                if (debug) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   141
                    System.out.println("a[" + i + "] is not " + i + " but " + a[i]);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   142
                }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   143
                fail = true;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   144
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   145
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   146
        if (fail) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   147
             printMissingElements(length, expected);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   148
             throw new RuntimeException("TEST FAILED: only " + length
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   149
                    + " reference objects have been queued out of "
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   150
                    + expected);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   151
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   152
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   153
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   154
    private static void printMissingElements(int length, int expected) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   155
        System.out.println("The following numbers were not found in the reference queue: ");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   156
        int missing = 0;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   157
        int element = 0;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   158
        for (int i = 0; i < length; i++) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   159
            while ((a[i] != element) & (element < expected)) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   160
                System.out.print(element + " ");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   161
                if (missing % 20 == 19) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   162
                    System.out.println(" ");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   163
                }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   164
                missing++;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   165
                element++;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   166
            }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   167
            element++;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   168
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   169
        System.out.print("\n");
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   170
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   171
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   172
    private static void forceGc(long millis) throws InterruptedException {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   173
        Runtime.getRuntime().gc();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   174
        Thread.sleep(millis);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   175
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   176
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   177
    // Bubble sort the first "length" elements in array "a".
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   178
    private static void sort(int length) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   179
        int hold;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   180
        if (debug) {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   181
            System.out.println("Sorting. Length=" + length);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   182
        }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   183
        for (int pass = 1; pass < length; pass++) {    // passes over the array
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   184
            for (int i = 0; i < length - pass; i++) {  //  a single pass
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   185
                if (a[i] > a[i + 1]) {  // then swap
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   186
                    hold = a[i];
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   187
                    a[i] = a[i + 1];
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   188
                    a[i + 1] = hold;
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   189
                }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   190
            }  // End of i loop
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   191
        } // End of pass loop
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   192
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   193
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   194
    // Raise thread priority so as to increase the
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   195
    // probability of the mutator succeeding in enqueueing
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   196
    // an object that is still in the pending state.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   197
    // This is (probably) only required for a uniprocessor.
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   198
    static void raisePriority() {
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   199
        Thread tr = Thread.currentThread();
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   200
        tr.setPriority(Thread.MAX_PRIORITY);
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   201
    }
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents:
diff changeset
   202
}   // End of class ReferenceEnqueuePending