2N/A/*
2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2N/A *
2N/A * This code is free software; you can redistribute it and/or modify it
2N/A * under the terms of the GNU General Public License version 2 only, as
2N/A * published by the Free Software Foundation.
2N/A *
2N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2N/A * version 2 for more details (a copy is included in the LICENSE file that
2N/A * accompanied this code).
2N/A *
2N/A * You should have received a copy of the GNU General Public License version
2N/A * 2 along with this work; if not, write to the Free Software Foundation,
2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2N/A *
2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2N/A * or visit www.oracle.com if you need additional information or have any
2N/A * questions.
2N/A */
2N/A
2N/A/*
2N/A * This file is available under and governed by the GNU General Public
2N/A * License version 2 only, as published by the Free Software Foundation.
2N/A * However, the following notice accompanied the original version of this
2N/A * file:
2N/A *
2N/A * Written by Doug Lea with assistance from members of JCP JSR-166
2N/A * Expert Group and released to the public domain, as explained at
2N/A * http://creativecommons.org/publicdomain/zero/1.0/
2N/A */
2N/A
2N/A/*
2N/A * @test
2N/A * @bug 4486658
2N/A * @compile -source 1.5 CancelledFutureLoops.java
2N/A * @run main/timeout=2000 CancelledFutureLoops
2N/A * @summary Checks for responsiveness of futures to cancellation.
2N/A * Runs under the assumption that ITERS computations require more than
2N/A * TIMEOUT msecs to complete.
2N/A */
2N/A
2N/Aimport java.util.concurrent.*;
2N/Aimport java.util.concurrent.locks.*;
2N/Aimport java.util.*;
2N/A
2N/Apublic final class CancelledFutureLoops {
2N/A static final ExecutorService pool = Executors.newCachedThreadPool();
2N/A static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
2N/A static boolean print = false;
2N/A static final int ITERS = 1000000;
2N/A static final long TIMEOUT = 100;
2N/A
2N/A public static void main(String[] args) throws Exception {
2N/A int maxThreads = 5;
2N/A if (args.length > 0)
2N/A maxThreads = Integer.parseInt(args[0]);
2N/A
2N/A print = true;
2N/A
2N/A for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
2N/A System.out.print("Threads: " + i);
2N/A try {
2N/A new FutureLoop(i).test();
2N/A }
2N/A catch (BrokenBarrierException bb) {
2N/A // OK; ignore
2N/A }
2N/A catch (ExecutionException ee) {
2N/A // OK; ignore
2N/A }
2N/A Thread.sleep(TIMEOUT);
2N/A }
2N/A pool.shutdown();
2N/A if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
2N/A throw new Error();
2N/A }
2N/A
2N/A static final class FutureLoop implements Callable {
private int v = rng.next();
private final ReentrantLock lock = new ReentrantLock();
private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
private final CyclicBarrier barrier;
private final int nthreads;
FutureLoop(int nthreads) {
this.nthreads = nthreads;
barrier = new CyclicBarrier(nthreads+1, timer);
}
final void test() throws Exception {
Future[] futures = new Future[nthreads];
for (int i = 0; i < nthreads; ++i)
futures[i] = pool.submit(this);
barrier.await();
Thread.sleep(TIMEOUT);
boolean tooLate = false;
for (int i = 1; i < nthreads; ++i) {
if (!futures[i].cancel(true))
tooLate = true;
// Unbunch some of the cancels
if ( (i & 3) == 0)
Thread.sleep(1 + rng.next() % 10);
}
Object f0 = futures[0].get();
if (!tooLate) {
for (int i = 1; i < nthreads; ++i) {
if (!futures[i].isDone() || !futures[i].isCancelled())
throw new Error("Only one thread should complete");
}
}
else
System.out.print("(cancelled too late) ");
long endTime = System.nanoTime();
long time = endTime - timer.startTime;
if (print) {
double secs = (double)(time) / 1000000000.0;
System.out.println("\t " + secs + "s run time");
}
}
public final Object call() throws Exception {
barrier.await();
int sum = v;
int x = 0;
int n = ITERS;
while (n-- > 0) {
lock.lockInterruptibly();
try {
v = x = LoopHelpers.compute1(v);
}
finally {
lock.unlock();
}
sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
}
return new Integer(sum);
}
}
}