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 100 38 * @summary Test concurrent offer vs. drainTo 39 */ 40 41import java.util.ArrayList; 42import java.util.List; 43import java.util.SplittableRandom; 44import java.util.concurrent.ArrayBlockingQueue; 45import java.util.concurrent.BlockingQueue; 46import java.util.concurrent.LinkedBlockingDeque; 47import java.util.concurrent.LinkedBlockingQueue; 48import java.util.concurrent.LinkedTransferQueue; 49import java.util.concurrent.atomic.AtomicLong; 50 51@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) 52public class OfferDrainToLoops { 53 final long testDurationMillisDefault = 10L * 1000L; 54 final long testDurationMillis; 55 56 OfferDrainToLoops(String[] args) { 57 testDurationMillis = (args.length > 0) ? 58 Long.valueOf(args[0]) : testDurationMillisDefault; 59 } 60 61 void checkNotContainsNull(Iterable it) { 62 for (Object x : it) 63 check(x != null); 64 } 65 66 void test(String[] args) throws Throwable { 67 test(new LinkedBlockingQueue()); 68 test(new LinkedBlockingQueue(2000)); 69 test(new LinkedBlockingDeque()); 70 test(new LinkedBlockingDeque(2000)); 71 test(new ArrayBlockingQueue(2000)); 72 test(new LinkedTransferQueue()); 73 } 74 75 void test(final BlockingQueue q) throws Throwable { 76 System.out.println(q.getClass().getSimpleName()); 77 final long testDurationNanos = testDurationMillis * 1000L * 1000L; 78 final long quittingTimeNanos = System.nanoTime() + testDurationNanos; 79 final long timeoutMillis = 10L * 1000L; 80 final SplittableRandom rnd = new SplittableRandom(); 81 82 /** Poor man's bounded buffer. */ 83 final AtomicLong approximateCount = new AtomicLong(0L); 84 85 abstract class CheckedThread extends Thread { 86 final SplittableRandom rnd; 87 88 CheckedThread(String name, SplittableRandom rnd) { 89 super(name); 90 this.rnd = rnd; 91 setDaemon(true); 92 start(); 93 } 94 /** Polls for quitting time. */ 95 protected boolean quittingTime() { 96 return System.nanoTime() - quittingTimeNanos > 0; 97 } 98 /** Polls occasionally for quitting time. */ 99 protected boolean quittingTime(long i) { 100 return (i % 1024) == 0 && quittingTime(); 101 } 102 protected abstract void realRun(); 103 public void run() { 104 try { realRun(); } catch (Throwable t) { unexpected(t); } 105 } 106 } 107 108 Thread offerer = new CheckedThread("offerer", rnd.split()) { 109 protected void realRun() { 110 long c = 0; 111 for (long i = 0; ! quittingTime(i); i++) { 112 if (q.offer(c)) { 113 if ((++c % 1024) == 0) { 114 approximateCount.getAndAdd(1024); 115 while (approximateCount.get() > 10000) 116 Thread.yield(); 117 } 118 } else { 119 Thread.yield(); 120 }}}}; 121 122 Thread drainer = new CheckedThread("drainer", rnd.split()) { 123 protected void realRun() { 124 while (! quittingTime()) { 125 List list = new ArrayList(); 126 int n = rnd.nextBoolean() ? 127 q.drainTo(list) : 128 q.drainTo(list, 100); 129 approximateCount.getAndAdd(-n); 130 equal(list.size(), n); 131 for (int j = 0; j < n - 1; j++) 132 equal((Long) list.get(j) + 1L, list.get(j + 1)); 133 Thread.yield(); 134 } 135 q.clear(); 136 approximateCount.set(0); // Releases waiting offerer thread 137 }}; 138 139 Thread scanner = new CheckedThread("scanner", rnd.split()) { 140 protected void realRun() { 141 while (! quittingTime()) { 142 switch (rnd.nextInt(3)) { 143 case 0: checkNotContainsNull(q); break; 144 case 1: q.size(); break; 145 case 2: 146 Long[] a = (Long[]) q.toArray(new Long[0]); 147 int n = a.length; 148 for (int j = 0; j < n - 1; j++) { 149 check(a[j] < a[j+1]); 150 check(a[j] != null); 151 } 152 break; 153 } 154 Thread.yield(); 155 }}}; 156 157 for (Thread thread : new Thread[] { offerer, drainer, scanner }) { 158 thread.join(timeoutMillis + testDurationMillis); 159 if (thread.isAlive()) { 160 System.err.printf("Hung thread: %s%n", thread.getName()); 161 failed++; 162 for (StackTraceElement e : thread.getStackTrace()) 163 System.err.println(e); 164 thread.join(timeoutMillis); 165 } 166 } 167 } 168 169 //--------------------- Infrastructure --------------------------- 170 volatile int passed = 0, failed = 0; 171 void pass() {passed++;} 172 void fail() {failed++; Thread.dumpStack();} 173 void fail(String msg) {System.err.println(msg); fail();} 174 void unexpected(Throwable t) {failed++; t.printStackTrace();} 175 void check(boolean cond) {if (cond) pass(); else fail();} 176 void equal(Object x, Object y) { 177 if (x == null ? y == null : x.equals(y)) pass(); 178 else fail(x + " not equal to " + y);} 179 public static void main(String[] args) throws Throwable { 180 new OfferDrainToLoops(args).instanceMain(args);} 181 public void instanceMain(String[] args) throws Throwable { 182 try {test(args);} catch (Throwable t) {unexpected(t);} 183 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 184 if (failed > 0) throw new AssertionError("Some tests failed");} 185} 186