test/jdk/java/util/concurrent/ConcurrentHashMap/ToArray.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 19863 jdk/test/java/util/concurrent/ConcurrentHashMap/ToArray.java@0cf14d31a41f
child 47730 c7b5b1ce8145
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19863
0cf14d31a41f 8024837: Rename java/util/concurrent/ConcurrentHashMap/toArray.java to ToArray.java
psandoz
parents: 19862
diff changeset
     2
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    26
 * @bug 4486658 8010293
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary thread safety of toArray methods of subCollections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    31
import java.util.concurrent.CompletableFuture;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    33
import java.util.stream.IntStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    35
public class ToArray {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String[] args) throws Throwable {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    38
        // Execute a number of times to increase the probability of
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    39
        // failure if there is an issue
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    40
        for (int i = 0; i < 16; i++) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    41
            executeTest();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    42
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    43
    }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    44
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    45
    static void executeTest() throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        final Throwable throwable[] = new Throwable[1];
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    47
        final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    49
        // Number of workers equal to the number of processors
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    50
        // Each worker will put globally unique keys into the map
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    51
        final int nWorkers = Runtime.getRuntime().availableProcessors();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    52
        final int sizePerWorker = 1024;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    53
        final int maxSize = nWorkers * sizePerWorker;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    55
        // The foreman keeps checking that the size of the arrays
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    56
        // obtained from the key and value sets is never less than the
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    57
        // previously observed size and is never greater than the maximum size
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    58
        // NOTE: these size constraints are not specific to toArray and are
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    59
        // applicable to any form of traversal of the collection views
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    60
        CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            private int prevSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            private boolean checkProgress(Object[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                int size = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                if (size < prevSize) throw new RuntimeException("WRONG WAY");
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    66
                if (size > maxSize) throw new RuntimeException("OVERSHOOT");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                if (size == maxSize) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                prevSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    72
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    Integer[] empty = new Integer[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        if (checkProgress(m.values().toArray())) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        if (checkProgress(m.keySet().toArray())) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        if (checkProgress(m.values().toArray(empty))) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        if (checkProgress(m.keySet().toArray(empty))) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    82
                }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    83
                catch (Throwable t) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    84
                    throwable[0] = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    85
                }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    86
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    87
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    89
        // Create workers
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    90
        // Each worker will put globally unique keys into the map
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    91
        CompletableFuture<?>[] workers = IntStream.range(0, nWorkers).
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    92
                mapToObj(w -> CompletableFuture.runAsync(() -> {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    93
                    for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    94
                        m.put(o + i, i);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    95
                })).
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    96
                toArray(CompletableFuture<?>[]::new);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    98
        // Wait for workers and then foreman to complete
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
    99
        CompletableFuture.allOf(workers).join();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 5506
diff changeset
   100
        foreman.join();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (throwable[0] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw throwable[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}