ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/concurrent/CopyOnWriteArrayList/EqualsRace.java
Revision: 1.6
Committed: Wed Jan 4 04:46:19 2017 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +1 -1 lines
Log Message:
convert to Diamond

File Contents

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