ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledFutureLoops.java
Revision: 1.9
Committed: Sat Dec 31 19:34:08 2016 UTC (7 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +6 -3 lines
Log Message:
organize imports

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 /*
8 * @test
9 * @summary Checks for responsiveness of futures to cancellation.
10 * Runs under
11 * the assumption that ITERS computations require more than TIMEOUT
12 * msecs to complete.
13 */
14
15 /*
16 * Written by Doug Lea with assistance from members of JCP JSR-166
17 * Expert Group and released to the public domain. Use, modify, and
18 * redistribute this code in any way without acknowledgement.
19 */
20
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.CyclicBarrier;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.Future;
26 import java.util.concurrent.locks.ReentrantLock;
27
28 public final class CancelledFutureLoops {
29 static final ExecutorService pool = Executors.newCachedThreadPool();
30 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
31 static boolean print = false;
32 static final int ITERS = 10000000;
33 static final long TIMEOUT = 100;
34
35 public static void main(String[] args) throws Exception {
36 int maxThreads = 100;
37 if (args.length > 0)
38 maxThreads = Integer.parseInt(args[0]);
39
40 print = true;
41
42 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
43 System.out.print("Threads: " + i);
44 new FutureLoop(i).test();
45 Thread.sleep(TIMEOUT);
46 }
47 pool.shutdown();
48 }
49
50 static final class FutureLoop implements Callable<Object> {
51 private int v = rng.next();
52 private final ReentrantLock lock = new ReentrantLock();
53 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
54 private final CyclicBarrier barrier;
55 private final int nthreads;
56 FutureLoop(int nthreads) {
57 this.nthreads = nthreads;
58 barrier = new CyclicBarrier(nthreads+1, timer);
59 }
60
61 final void test() throws Exception {
62 Future<?>[] futures = new Future<?>[nthreads];
63 for (int i = 0; i < nthreads; ++i)
64 futures[i] = pool.submit(this);
65
66 barrier.await();
67 Thread.sleep(TIMEOUT);
68 boolean tooLate = false;
69 for (int i = 1; i < nthreads; ++i) {
70 if (!futures[i].cancel(true))
71 tooLate = true;
72 // Unbunch some of the cancels
73 if ( (i & 3) == 0)
74 Thread.sleep(1 + rng.next() % 10);
75 }
76
77 Object f0 = futures[0].get();
78 if (!tooLate) {
79 for (int i = 1; i < nthreads; ++i) {
80 if (!futures[i].isDone() || !futures[i].isCancelled())
81 throw new Error("Only one thread should complete");
82 }
83 }
84 else
85 System.out.print("(cancelled too late) ");
86
87 long endTime = System.nanoTime();
88 long time = endTime - timer.startTime;
89 if (print) {
90 double secs = (double) time / 1000000000.0;
91 System.out.println("\t " + secs + "s run time");
92 }
93
94 }
95
96 public final Object call() throws Exception {
97 barrier.await();
98 int sum = v;
99 int x = 0;
100 int n = ITERS;
101 while (n-- > 0) {
102 lock.lockInterruptibly();
103 try {
104 v = x = LoopHelpers.compute1(v);
105 }
106 finally {
107 lock.unlock();
108 }
109 sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
110 }
111 return new Integer(sum);
112 }
113 }
114
115 }