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 6330307 6355645 27 * @summary Check for race conditions in COWArray classes 28 * @author Martin Buchholz 29 */ 30 31 import java.util.List; 32 import java.util.Set; 33 import java.util.concurrent.CopyOnWriteArrayList; 34 import java.util.concurrent.CopyOnWriteArraySet; 35 36 public class RacingCows { realMain(String[] args)37 private static void realMain(String[] args) throws Throwable { 38 final int iterations = 100000; 39 final Integer two = Integer.valueOf(2); 40 final Integer three = Integer.valueOf(3); 41 42 //------------ CopyOnWriteArraySet ------------------------------- 43 final Set<Integer> s1 = new CopyOnWriteArraySet<>(); 44 final Set<Integer> s2 = new CopyOnWriteArraySet<>(); 45 s1.add(1); 46 47 final Thread t1 = new CheckedThread() { public void realRun() { 48 for (int i = 0; i < iterations; i++) { 49 s2.add(two); 50 s2.remove(two); 51 }}}; 52 t1.start(); 53 54 for (int i = 0; i < iterations; i++) { 55 check(! s1.equals(s2)); 56 check(! s2.equals(s1)); 57 } 58 t1.join(); 59 60 //------------ CopyOnWriteArrayList ------------------------------ 61 final List<Integer> l1 = new CopyOnWriteArrayList<>(); 62 final List<Integer> l2 = new CopyOnWriteArrayList<>(); 63 final List<Integer> l3 = new CopyOnWriteArrayList<>(); 64 l1.add(1); 65 66 final Thread t2 = new CheckedThread() { public void realRun() { 67 for (int i = 0; i < iterations; i++) { 68 switch (i%2) { 69 case 0: l2.add(two); break; 70 case 1: l2.add(0, two); break; 71 } 72 switch (i%3) { 73 case 0: l2.remove(two); break; 74 case 1: l2.remove(0); break; 75 case 2: l2.clear(); break; 76 }}}}; 77 t2.start(); 78 79 final Thread t3 = new CheckedThread() { public void realRun() { 80 l3.add(three); 81 for (int i = 0; i < iterations; i++) { 82 switch (i%2) { 83 case 0: l3.add(two); break; 84 case 1: l3.add(0, two); break; 85 } 86 switch (i%2) { 87 case 0: l3.remove(two); break; 88 case 1: l3.remove(0); break; 89 }}}}; 90 t3.start(); 91 92 for (int i = 0; i < iterations; i++) { 93 check(! l1.equals(l2)); 94 check(! l2.equals(l1)); 95 96 // CopyOnWriteArrayList(mutatingCollection) 97 try { new CopyOnWriteArrayList<Integer>(l2); } 98 catch (Throwable t) { unexpected(t); } 99 100 // addAllAbsent(mutatingCollection) 101 try { new CopyOnWriteArrayList<Integer>().addAllAbsent(l3); } 102 catch (Throwable t) { unexpected(t); } 103 104 // addAll(mutatingCollection) 105 try { new CopyOnWriteArrayList<Integer>().addAll(l3); } 106 catch (Throwable t) { unexpected(t); } 107 108 // addAll(int, mutatingCollection) 109 try { new CopyOnWriteArrayList<Integer>().addAll(0,l3); } 110 catch (Throwable t) { unexpected(t); } 111 } 112 t2.join(); 113 t3.join(); 114 } 115 116 //--------------------- Infrastructure --------------------------- 117 static volatile int passed = 0, failed = 0; pass()118 static void pass() {passed++;} fail()119 static void fail() {failed++; Thread.dumpStack();} fail(String msg)120 static void fail(String msg) {System.out.println(msg); fail();} unexpected(Throwable t)121 static void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)122 static void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)123 static void equal(Object x, Object y) { 124 if (x == null ? y == null : x.equals(y)) pass(); 125 else fail(x + " not equal to " + y);} main(String[] args)126 public static void main(String[] args) throws Throwable { 127 try {realMain(args);} catch (Throwable t) {unexpected(t);} 128 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 129 if (failed > 0) throw new AssertionError("Some tests failed");} 130 private abstract static class CheckedThread extends Thread { realRun()131 public abstract void realRun() throws Throwable; run()132 public void run() { 133 try { realRun(); } catch (Throwable t) { unexpected(t); }}} 134 } 135