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 4486658 37 * @summary Checks for responsiveness of futures to cancellation. 38 * Runs under the assumption that ITERS computations require more than 39 * TIMEOUT msecs to complete. 40 * @library /lib/testlibrary/ 41 * @run main/timeout=2000 CancelledFutureLoops 42 */ 43 44 import static java.util.concurrent.TimeUnit.MILLISECONDS; 45 46 import java.util.SplittableRandom; 47 import java.util.concurrent.BrokenBarrierException; 48 import java.util.concurrent.Callable; 49 import java.util.concurrent.CyclicBarrier; 50 import java.util.concurrent.ExecutionException; 51 import java.util.concurrent.ExecutorService; 52 import java.util.concurrent.Executors; 53 import java.util.concurrent.Future; 54 import java.util.concurrent.locks.ReentrantLock; 55 import jdk.testlibrary.Utils; 56 57 public final class CancelledFutureLoops { 58 static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); 59 static final ExecutorService pool = Executors.newCachedThreadPool(); 60 static final SplittableRandom rnd = new SplittableRandom(); 61 static boolean print = false; 62 static final int ITERS = 1000000; 63 static final long TIMEOUT = 100; 64 65 public static void main(String[] args) throws Exception { 66 int maxThreads = 5; 67 if (args.length > 0) 68 maxThreads = Integer.parseInt(args[0]); 69 70 print = true; 71 72 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) { 73 System.out.print("Threads: " + i); 74 try { 75 new FutureLoop(i, rnd.split()).test(); 76 } 77 catch (BrokenBarrierException bb) { 78 // OK; ignore 79 } 80 catch (ExecutionException ee) { 81 // OK; ignore 82 } 83 Thread.sleep(TIMEOUT); 84 } 85 pool.shutdown(); 86 if (! pool.awaitTermination(6 * LONG_DELAY_MS, MILLISECONDS)) 87 throw new Error(); 88 } 89 90 static final class FutureLoop implements Callable { 91 private final int nthreads; 92 private final SplittableRandom rnd; 93 private final ReentrantLock lock = new ReentrantLock(); 94 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); 95 private final CyclicBarrier barrier; 96 private int v; 97 FutureLoop(int nthreads, SplittableRandom rnd) { 98 this.nthreads = nthreads; 99 this.rnd = rnd; 100 barrier = new CyclicBarrier(nthreads+1, timer); 101 v = rnd.nextInt(); 102 } 103 104 final void test() throws Exception { 105 Future[] futures = new Future[nthreads]; 106 for (int i = 0; i < nthreads; ++i) 107 futures[i] = pool.submit(this); 108 109 barrier.await(); 110 Thread.sleep(TIMEOUT); 111 boolean tooLate = false; 112 for (int i = 1; i < nthreads; ++i) { 113 if (!futures[i].cancel(true)) 114 tooLate = true; 115 // Unbunch some of the cancels 116 if ( (i & 3) == 0) 117 Thread.sleep(1 + rnd.nextInt(5)); 118 } 119 120 Object f0 = futures[0].get(); 121 if (!tooLate) { 122 for (int i = 1; i < nthreads; ++i) { 123 if (!futures[i].isDone() || !futures[i].isCancelled()) 124 throw new Error("Only one thread should complete"); 125 } 126 } 127 else 128 System.out.print("(cancelled too late) "); 129 130 long endTime = System.nanoTime(); 131 long time = endTime - timer.startTime; 132 if (print) { 133 double secs = (double)(time) / 1000000000.0; 134 System.out.println("\t " + secs + "s run time"); 135 } 136 137 } 138 139 public final Object call() throws Exception { 140 barrier.await(); 141 int sum = v; 142 int x = 0; 143 int n = ITERS; 144 while (n-- > 0) { 145 lock.lockInterruptibly(); 146 try { 147 v = x = LoopHelpers.compute1(v); 148 } 149 finally { 150 lock.unlock(); 151 } 152 sum += LoopHelpers.compute2(LoopHelpers.compute2(x)); 153 } 154 return new Integer(sum); 155 } 156 } 157 158 }