Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/CopyOnWriteArraySet/RacingCows.java
+++ new/test/java/util/concurrent/CopyOnWriteArraySet/RacingCows.java
1 1 /*
2 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 26 * @bug 6330307 6355645
27 27 * @summary Check for race conditions in COWArray classes
28 28 * @author Martin Buchholz
29 29 */
30 30
31 31 import java.util.*;
32 32 import java.util.concurrent.*;
33 33
34 34 public class RacingCows {
35 35 private static void realMain(String[] args) throws Throwable {
36 36 final int iterations = 100000;
37 37 final Integer two = Integer.valueOf(2);
38 38 final Integer three = Integer.valueOf(3);
39 39
40 40 //------------ CopyOnWriteArraySet -------------------------------
41 41 final Set<Integer> s1 = new CopyOnWriteArraySet<Integer>();
42 42 final Set<Integer> s2 = new CopyOnWriteArraySet<Integer>();
43 43 s1.add(1);
44 44
45 45 final Thread t1 = new CheckedThread() { public void realRun() {
46 46 for (int i = 0; i < iterations; i++) {
47 47 s2.add(two);
48 48 s2.remove(two);
49 49 }}};
50 50 t1.start();
51 51
52 52 for (int i = 0; i < iterations; i++) {
53 53 check(! s1.equals(s2));
54 54 check(! s2.equals(s1));
55 55 }
56 56 t1.join();
57 57
58 58 //------------ CopyOnWriteArrayList ------------------------------
59 59 final List<Integer> l1 = new CopyOnWriteArrayList<Integer>();
60 60 final List<Integer> l2 = new CopyOnWriteArrayList<Integer>();
61 61 final List<Integer> l3 = new CopyOnWriteArrayList<Integer>();
62 62 l1.add(1);
63 63
64 64 final Thread t2 = new CheckedThread() { public void realRun() {
65 65 for (int i = 0; i < iterations; i++) {
66 66 switch (i%2) {
67 67 case 0: l2.add(two); break;
68 68 case 1: l2.add(0, two); break;
69 69 }
70 70 switch (i%3) {
71 71 case 0: l2.remove(two); break;
72 72 case 1: l2.remove(0); break;
73 73 case 2: l2.clear(); break;
74 74 }}}};
75 75 t2.start();
76 76
77 77 final Thread t3 = new CheckedThread() { public void realRun() {
78 78 l3.add(three);
79 79 for (int i = 0; i < iterations; i++) {
80 80 switch (i%2) {
81 81 case 0: l3.add(two); break;
82 82 case 1: l3.add(0, two); break;
83 83 }
84 84 switch (i%2) {
85 85 case 0: l3.remove(two); break;
86 86 case 1: l3.remove(0); break;
87 87 }}}};
88 88 t3.start();
89 89
90 90 for (int i = 0; i < iterations; i++) {
91 91 check(! l1.equals(l2));
92 92 check(! l2.equals(l1));
93 93
94 94 // CopyOnWriteArrayList(mutatingCollection)
95 95 try { new CopyOnWriteArrayList<Integer>(l2); }
96 96 catch (Throwable t) { unexpected(t); }
97 97
98 98 // addAllAbsent(mutatingCollection)
99 99 try { new CopyOnWriteArrayList<Integer>().addAllAbsent(l3); }
100 100 catch (Throwable t) { unexpected(t); }
101 101
102 102 // addAll(mutatingCollection)
103 103 try { new CopyOnWriteArrayList<Integer>().addAll(l3); }
104 104 catch (Throwable t) { unexpected(t); }
105 105
106 106 // addAll(int, mutatingCollection)
107 107 try { new CopyOnWriteArrayList<Integer>().addAll(0,l3); }
108 108 catch (Throwable t) { unexpected(t); }
109 109 }
110 110 t2.join();
111 111 t3.join();
112 112 }
113 113
114 114 //--------------------- Infrastructure ---------------------------
115 115 static volatile int passed = 0, failed = 0;
116 116 static void pass() {passed++;}
117 117 static void fail() {failed++; Thread.dumpStack();}
↓ open down ↓ |
117 lines elided |
↑ open up ↑ |
118 118 static void fail(String msg) {System.out.println(msg); fail();}
119 119 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
120 120 static void check(boolean cond) {if (cond) pass(); else fail();}
121 121 static void equal(Object x, Object y) {
122 122 if (x == null ? y == null : x.equals(y)) pass();
123 123 else fail(x + " not equal to " + y);}
124 124 public static void main(String[] args) throws Throwable {
125 125 try {realMain(args);} catch (Throwable t) {unexpected(t);}
126 126 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
127 127 if (failed > 0) throw new AssertionError("Some tests failed");}
128 - private static abstract class CheckedThread extends Thread {
128 + private abstract static class CheckedThread extends Thread {
129 129 public abstract void realRun() throws Throwable;
130 130 public void run() {
131 131 try { realRun(); } catch (Throwable t) { unexpected(t); }}}
132 132 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX