Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/CopyOnWriteArrayList/EqualsRace.java
+++ new/test/java/util/concurrent/CopyOnWriteArrayList/EqualsRace.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 6318638 6325166 6330307
27 27 * @summary CopyOnWriteArrayList.equals should be thread-safe
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 EqualsRace {
35 35 private static void realMain(String[] args) throws Throwable {
36 36 final int iterations = 100000;
37 37 final List<Integer> list = new CopyOnWriteArrayList<Integer>();
38 38 final Integer one = Integer.valueOf(1);
39 39 final List<Integer> oneElementList = Arrays.asList(one);
40 40 final Thread t = new CheckedThread() { public void realRun() {
41 41 for (int i = 0; i < iterations; i++) {
42 42 list.add(one);
43 43 list.remove(one);
44 44 }}};
45 45 t.start();
46 46
47 47 for (int i = 0; i < iterations; i++) {
48 48 list.equals(oneElementList);
49 49 list.equals(Collections.EMPTY_LIST);
50 50 }
51 51 t.join();
52 52 check(list.size() == 0);
53 53 }
54 54
55 55 //--------------------- Infrastructure ---------------------------
56 56 static volatile int passed = 0, failed = 0;
57 57 static void pass() {passed++;}
58 58 static void fail() {failed++; Thread.dumpStack();}
↓ open down ↓ |
58 lines elided |
↑ open up ↑ |
59 59 static void fail(String msg) {System.out.println(msg); fail();}
60 60 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
61 61 static void check(boolean cond) {if (cond) pass(); else fail();}
62 62 static void equal(Object x, Object y) {
63 63 if (x == null ? y == null : x.equals(y)) pass();
64 64 else fail(x + " not equal to " + y);}
65 65 public static void main(String[] args) throws Throwable {
66 66 try {realMain(args);} catch (Throwable t) {unexpected(t);}
67 67 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
68 68 if (failed > 0) throw new AssertionError("Some tests failed");}
69 - private static abstract class CheckedThread extends Thread {
69 + private abstract static class CheckedThread extends Thread {
70 70 public abstract void realRun() throws Throwable;
71 71 public void run() {
72 72 try { realRun(); } catch (Throwable t) { unexpected(t); }}}
73 73 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX