1 |
|
/* |
2 |
< |
* Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved. |
2 |
> |
* Copyright (c) 2006, 2010, 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 |
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. |
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 |
|
/* |
28 |
|
* @author Martin Buchholz |
29 |
|
*/ |
30 |
|
|
31 |
< |
import static java.util.Collections.*; |
32 |
< |
import java.util.*; |
33 |
< |
import java.util.concurrent.*; |
31 |
> |
import java.util.ArrayDeque; |
32 |
> |
import java.util.ArrayList; |
33 |
> |
import java.util.Collection; |
34 |
> |
import java.util.Collections; |
35 |
> |
import java.util.Comparator; |
36 |
> |
import java.util.Deque; |
37 |
> |
import java.util.HashMap; |
38 |
> |
import java.util.HashSet; |
39 |
> |
import java.util.Hashtable; |
40 |
> |
import java.util.LinkedList; |
41 |
> |
import java.util.List; |
42 |
> |
import java.util.Map; |
43 |
> |
import java.util.Queue; |
44 |
> |
import java.util.Set; |
45 |
> |
import java.util.TreeMap; |
46 |
> |
import java.util.TreeSet; |
47 |
> |
import java.util.Vector; |
48 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
49 |
> |
import java.util.concurrent.ConcurrentHashMap; |
50 |
> |
import java.util.concurrent.ConcurrentLinkedDeque; |
51 |
> |
import java.util.concurrent.ConcurrentLinkedQueue; |
52 |
> |
import java.util.concurrent.ConcurrentSkipListMap; |
53 |
> |
import java.util.concurrent.ConcurrentSkipListSet; |
54 |
> |
import java.util.concurrent.CopyOnWriteArrayList; |
55 |
> |
import java.util.concurrent.CopyOnWriteArraySet; |
56 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
57 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
58 |
> |
import java.util.concurrent.LinkedTransferQueue; |
59 |
> |
|
60 |
> |
import static java.util.Collections.asLifoQueue; |
61 |
> |
import static java.util.Collections.checkedList; |
62 |
> |
import static java.util.Collections.checkedMap; |
63 |
> |
import static java.util.Collections.checkedSet; |
64 |
> |
import static java.util.Collections.newSetFromMap; |
65 |
> |
import static java.util.Collections.synchronizedList; |
66 |
> |
import static java.util.Collections.synchronizedMap; |
67 |
> |
import static java.util.Collections.synchronizedSet; |
68 |
> |
import static java.util.Collections.unmodifiableList; |
69 |
> |
import static java.util.Collections.unmodifiableMap; |
70 |
> |
import static java.util.Collections.unmodifiableSet; |
71 |
|
|
72 |
|
public class RacingCollections { |
73 |
|
/** |
75 |
|
* Turn this up to some higher value like 1000 for stress testing: |
76 |
|
* java -Dmillis=1000 RacingCollections |
77 |
|
*/ |
78 |
< |
final static long defaultWorkTimeMillis = Long.getLong("millis", 10L); |
78 |
> |
static final long defaultWorkTimeMillis = Long.getLong("millis", 10L); |
79 |
|
|
80 |
|
/** |
81 |
|
* Whether to print debug information. |
82 |
|
*/ |
83 |
< |
final static boolean debug = Boolean.getBoolean("debug"); |
83 |
> |
static final boolean debug = Boolean.getBoolean("debug"); |
84 |
|
|
85 |
< |
final static Integer one = 1; |
86 |
< |
final static Integer two = 2; |
85 |
> |
static final Integer one = 1; |
86 |
> |
static final Integer two = 2; |
87 |
|
|
88 |
|
/** |
89 |
|
* A thread that mutates an object forever, alternating between |
99 |
|
this.start(); |
100 |
|
} |
101 |
|
|
102 |
< |
@SuppressWarnings("unchecked") void clear(Object o) { |
102 |
> |
@SuppressWarnings("unchecked") |
103 |
> |
void clear(Object o) { |
104 |
|
if (o instanceof Collection) |
105 |
|
((Collection<?>)o).clear(); |
106 |
|
else |
107 |
|
((Map<?,?>)o).clear(); |
108 |
|
} |
109 |
|
|
110 |
< |
@SuppressWarnings("unchecked") void realRun() { |
110 |
> |
@SuppressWarnings("unchecked") |
111 |
> |
void realRun() { |
112 |
|
// Mutate elLoco wildly forever, checking occasionally for "done" |
113 |
|
clear(elLoco); |
114 |
|
if (elLoco instanceof List) { |
195 |
|
quittingTime = System.nanoTime() + workTimeMillis * 1024 * 1024; |
196 |
|
} |
197 |
|
boolean keepGoing() { |
198 |
< |
return (i++ % 128 != 0) || (System.nanoTime() < quittingTime); |
198 |
> |
return (i++ % 128 != 0) || (System.nanoTime() - quittingTime < 0); |
199 |
|
} |
200 |
|
} |
201 |
|
|
221 |
|
} |
222 |
|
|
223 |
|
private static List<Map<Integer, Boolean>> newConcurrentMaps() { |
224 |
< |
List<Map<Integer, Boolean>> list = |
186 |
< |
new ArrayList<Map<Integer, Boolean>>(); |
224 |
> |
List<Map<Integer, Boolean>> list = new ArrayList<>(); |
225 |
|
list.add(new ConcurrentHashMap<Integer, Boolean>()); |
226 |
|
list.add(new ConcurrentSkipListMap<Integer, Boolean>()); |
227 |
|
return list; |
232 |
|
list.add(new Hashtable<Integer, Boolean>()); |
233 |
|
list.add(new HashMap<Integer, Boolean>()); |
234 |
|
list.add(new TreeMap<Integer, Boolean>()); |
235 |
< |
Comparator<Integer> cmp = new Comparator<Integer>() { |
235 |
> |
Comparator<Integer> cmp = new Comparator<>() { |
236 |
|
public int compare(Integer x, Integer y) { |
237 |
|
return x - y; |
238 |
|
}}; |
241 |
|
} |
242 |
|
|
243 |
|
private static List<Set<Integer>> newConcurrentSets() { |
244 |
< |
List<Set<Integer>> list = new ArrayList<Set<Integer>>(); |
244 |
> |
List<Set<Integer>> list = new ArrayList<>(); |
245 |
|
list.add(new ConcurrentSkipListSet<Integer>()); |
246 |
|
list.add(new CopyOnWriteArraySet<Integer>()); |
247 |
|
return list; |
256 |
|
} |
257 |
|
|
258 |
|
private static List<List<Integer>> newConcurrentLists() { |
259 |
< |
List<List<Integer>> list = new ArrayList<List<Integer>>(); |
259 |
> |
List<List<Integer>> list = new ArrayList<>(); |
260 |
|
list.add(new CopyOnWriteArrayList<Integer>()); |
261 |
|
return list; |
262 |
|
} |
269 |
|
} |
270 |
|
|
271 |
|
private static List<Queue<Integer>> newConcurrentQueues() { |
272 |
< |
List<Queue<Integer>> list = |
273 |
< |
new ArrayList<Queue<Integer>>(newConcurrentDeques()); |
272 |
> |
List<Queue<Integer>> list = new ArrayList<>(newConcurrentDeques()); |
273 |
> |
list.add(new ArrayBlockingQueue<Integer>(10)); |
274 |
|
list.add(new LinkedBlockingQueue<Integer>(10)); |
275 |
|
list.add(new LinkedTransferQueue<Integer>()); |
276 |
+ |
list.add(new ConcurrentLinkedQueue<Integer>()); |
277 |
|
return list; |
278 |
|
} |
279 |
|
|
280 |
|
private static List<Queue<Integer>> newQueues() { |
281 |
< |
List<Queue<Integer>> list = |
243 |
< |
new ArrayList<Queue<Integer>>(newDeques()); |
281 |
> |
List<Queue<Integer>> list = new ArrayList<>(newDeques()); |
282 |
|
list.add(new LinkedBlockingQueue<Integer>(10)); |
283 |
|
return list; |
284 |
|
} |
285 |
|
|
286 |
|
private static List<Deque<Integer>> newConcurrentDeques() { |
287 |
< |
List<Deque<Integer>> list = new ArrayList<Deque<Integer>>(); |
287 |
> |
List<Deque<Integer>> list = new ArrayList<>(); |
288 |
|
list.add(new LinkedBlockingDeque<Integer>(10)); |
289 |
+ |
list.add(new ConcurrentLinkedDeque<Integer>()); |
290 |
|
return list; |
291 |
|
} |
292 |
|
|
377 |
|
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
378 |
|
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
379 |
|
if (failed > 0) throw new AssertionError("Some tests failed");} |
380 |
< |
private static abstract class CheckedThread extends Thread { |
380 |
> |
private abstract static class CheckedThread extends Thread { |
381 |
|
abstract void realRun() throws Throwable; |
382 |
|
public void run() { |
383 |
|
try { realRun(); } catch (Throwable t) { unexpected(t); }}} |