1 |
/* |
2 |
* Copyright (c) 2004, 2013, 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 4486658 8010293 |
27 |
* @summary thread safety of toArray methods of collection views |
28 |
* @author Martin Buchholz |
29 |
*/ |
30 |
|
31 |
import java.util.List; |
32 |
import java.util.concurrent.CompletableFuture; |
33 |
import java.util.concurrent.ConcurrentHashMap; |
34 |
import java.util.concurrent.ThreadLocalRandom; |
35 |
import java.util.stream.Collectors; |
36 |
import java.util.stream.IntStream; |
37 |
|
38 |
public class ToArray { |
39 |
|
40 |
public static void main(String[] args) throws Throwable { |
41 |
final int runsPerTest = Integer.getInteger("jsr166.runsPerTest", 1); |
42 |
final int reps = 10 * runsPerTest; |
43 |
for (int i = reps; i--> 0; ) |
44 |
executeTest(); |
45 |
} |
46 |
|
47 |
static void executeTest() throws Throwable { |
48 |
final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>(); |
49 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
50 |
final int nCPU = Runtime.getRuntime().availableProcessors(); |
51 |
final int minWorkers = 2; |
52 |
final int maxWorkers = Math.max(minWorkers, Math.min(32, nCPU)); |
53 |
final int nWorkers = rnd.nextInt(minWorkers, maxWorkers + 1); |
54 |
final int sizePerWorker = 1024; |
55 |
final int maxSize = nWorkers * sizePerWorker; |
56 |
|
57 |
// The foreman busy-checks that the size of the arrays obtained |
58 |
// from the keys and values views grows monotonically until it |
59 |
// reaches the maximum size. |
60 |
|
61 |
// NOTE: these size constraints are not specific to toArray and are |
62 |
// applicable to any form of traversal of the collection views |
63 |
CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() { |
64 |
private int prevSize = 0; |
65 |
|
66 |
private boolean checkProgress(Object[] a) { |
67 |
int size = a.length; |
68 |
if (size < prevSize || size > maxSize) |
69 |
throw new AssertionError( |
70 |
String.format("prevSize=%d size=%d maxSize=%d", |
71 |
prevSize, size, maxSize)); |
72 |
prevSize = size; |
73 |
return size == maxSize; |
74 |
} |
75 |
|
76 |
public void run() { |
77 |
Integer[] empty = new Integer[0]; |
78 |
for (;;) |
79 |
if (checkProgress(m.values().toArray()) |
80 |
& checkProgress(m.keySet().toArray()) |
81 |
& checkProgress(m.values().toArray(empty)) |
82 |
& checkProgress(m.keySet().toArray(empty))) |
83 |
return; |
84 |
} |
85 |
}); |
86 |
|
87 |
// Each worker puts globally unique keys into the map |
88 |
List<CompletableFuture<?>> workers = |
89 |
IntStream.range(0, nWorkers) |
90 |
.mapToObj(w -> (Runnable) () -> { |
91 |
for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++) |
92 |
m.put(o + i, i); |
93 |
}) |
94 |
.map(CompletableFuture::runAsync) |
95 |
.collect(Collectors.toList()); |
96 |
|
97 |
// Wait for workers and foreman to complete |
98 |
workers.forEach(CompletableFuture<?>::join); |
99 |
foreman.join(); |
100 |
} |
101 |
} |