OfferDrainToLoops.java revision 12745:f068a4ffddd2
1/* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23/* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 */ 33 34/* 35 * @test 36 * @bug 6805775 6815766 37 * @run main OfferDrainToLoops 300 38 * @summary Test concurrent offer vs. drainTo 39 */ 40 41import java.util.*; 42import java.util.concurrent.*; 43import java.util.concurrent.atomic.*; 44 45@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) 46public class OfferDrainToLoops { 47 final long testDurationMillisDefault = 10L * 1000L; 48 final long testDurationMillis; 49 50 OfferDrainToLoops(String[] args) { 51 testDurationMillis = (args.length > 0) ? 52 Long.valueOf(args[0]) : testDurationMillisDefault; 53 } 54 55 void checkNotContainsNull(Iterable it) { 56 for (Object x : it) 57 check(x != null); 58 } 59 60 void test(String[] args) throws Throwable { 61 test(new LinkedBlockingQueue()); 62 test(new LinkedBlockingQueue(2000)); 63 test(new LinkedBlockingDeque()); 64 test(new LinkedBlockingDeque(2000)); 65 test(new ArrayBlockingQueue(2000)); 66 test(new LinkedTransferQueue()); 67 } 68 69 Random getRandom() { 70 return ThreadLocalRandom.current(); 71 } 72 73 void test(final BlockingQueue q) throws Throwable { 74 System.out.println(q.getClass().getSimpleName()); 75 final long testDurationNanos = testDurationMillis * 1000L * 1000L; 76 final long quittingTimeNanos = System.nanoTime() + testDurationNanos; 77 final long timeoutMillis = 10L * 1000L; 78 79 /** Poor man's bounded buffer. */ 80 final AtomicLong approximateCount = new AtomicLong(0L); 81 82 abstract class CheckedThread extends Thread { 83 CheckedThread(String name) { 84 super(name); 85 setDaemon(true); 86 start(); 87 } 88 /** Polls for quitting time. */ 89 protected boolean quittingTime() { 90 return System.nanoTime() - quittingTimeNanos > 0; 91 } 92 /** Polls occasionally for quitting time. */ 93 protected boolean quittingTime(long i) { 94 return (i % 1024) == 0 && quittingTime(); 95 } 96 protected abstract void realRun(); 97 public void run() { 98 try { realRun(); } catch (Throwable t) { unexpected(t); } 99 } 100 } 101 102 Thread offerer = new CheckedThread("offerer") { 103 protected void realRun() { 104 long c = 0; 105 for (long i = 0; ! quittingTime(i); i++) { 106 if (q.offer(c)) { 107 if ((++c % 1024) == 0) { 108 approximateCount.getAndAdd(1024); 109 while (approximateCount.get() > 10000) 110 Thread.yield(); 111 } 112 } else { 113 Thread.yield(); 114 }}}}; 115 116 Thread drainer = new CheckedThread("drainer") { 117 protected void realRun() { 118 final Random rnd = getRandom(); 119 while (! quittingTime()) { 120 List list = new ArrayList(); 121 int n = rnd.nextBoolean() ? 122 q.drainTo(list) : 123 q.drainTo(list, 100); 124 approximateCount.getAndAdd(-n); 125 equal(list.size(), n); 126 for (int j = 0; j < n - 1; j++) 127 equal((Long) list.get(j) + 1L, list.get(j + 1)); 128 Thread.yield(); 129 } 130 q.clear(); 131 approximateCount.set(0); // Releases waiting offerer thread 132 }}; 133 134 Thread scanner = new CheckedThread("scanner") { 135 protected void realRun() { 136 final Random rnd = getRandom(); 137 while (! quittingTime()) { 138 switch (rnd.nextInt(3)) { 139 case 0: checkNotContainsNull(q); break; 140 case 1: q.size(); break; 141 case 2: 142 Long[] a = (Long[]) q.toArray(new Long[0]); 143 int n = a.length; 144 for (int j = 0; j < n - 1; j++) { 145 check(a[j] < a[j+1]); 146 check(a[j] != null); 147 } 148 break; 149 } 150 Thread.yield(); 151 }}}; 152 153 for (Thread thread : new Thread[] { offerer, drainer, scanner }) { 154 thread.join(timeoutMillis + testDurationMillis); 155 if (thread.isAlive()) { 156 System.err.printf("Hung thread: %s%n", thread.getName()); 157 failed++; 158 for (StackTraceElement e : thread.getStackTrace()) 159 System.err.println(e); 160 // Kludge alert 161 thread.stop(); 162 thread.join(timeoutMillis); 163 } 164 } 165 } 166 167 //--------------------- Infrastructure --------------------------- 168 volatile int passed = 0, failed = 0; 169 void pass() {passed++;} 170 void fail() {failed++; Thread.dumpStack();} 171 void fail(String msg) {System.err.println(msg); fail();} 172 void unexpected(Throwable t) {failed++; t.printStackTrace();} 173 void check(boolean cond) {if (cond) pass(); else fail();} 174 void equal(Object x, Object y) { 175 if (x == null ? y == null : x.equals(y)) pass(); 176 else fail(x + " not equal to " + y);} 177 public static void main(String[] args) throws Throwable { 178 new OfferDrainToLoops(args).instanceMain(args);} 179 public void instanceMain(String[] args) throws Throwable { 180 try {test(args);} catch (Throwable t) {unexpected(t);} 181 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 182 if (failed > 0) throw new AssertionError("Some tests failed");} 183} 184