blob: 5a69608c565a0b583050f5e38f529e50a2edd468 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4486658
27 * @summary thread safety of toArray methods of subCollections
28 * @author Martin Buchholz
29 */
30
31import java.util.*;
32import java.util.concurrent.*;
33
34public class toArray {
35
36 public static void main(String[] args) throws Throwable {
37 final Throwable throwable[] = new Throwable[1];
38 final int maxSize = 1000;
39 final ConcurrentHashMap<Integer, Integer> m
40 = new ConcurrentHashMap<Integer, Integer>();
41
42 final Thread t1 = new Thread() { public void run() {
43 for (int i = 0; i < maxSize; i++)
44 m.put(i,i);}};
45
46 final Thread t2 = new Thread() {
47 public Throwable exception = null;
48 private int prevSize = 0;
49
50 private boolean checkProgress(Object[] a) {
51 int size = a.length;
52 if (size < prevSize) throw new RuntimeException("WRONG WAY");
53 if (size > maxSize) throw new RuntimeException("OVERSHOOT");
54 if (size == maxSize) return true;
55 prevSize = size;
56 return false;
57 }
58
59 public void run() {
60 try {
61 Integer[] empty = new Integer[0];
62 while (true) {
63 if (checkProgress(m.values().toArray())) return;
64 if (checkProgress(m.keySet().toArray())) return;
65 if (checkProgress(m.values().toArray(empty))) return;
66 if (checkProgress(m.keySet().toArray(empty))) return;
67 }
68 } catch (Throwable t) {
69 throwable[0] = t;
70 }}};
71
72 t2.start();
73 t1.start();
74
75 t1.join();
76 t2.join();
77
78 if (throwable[0] != null)
79 throw throwable[0];
80 }
81}