ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledFutureLoops.java
Revision: 1.4
Committed: Tue Nov 3 01:04:02 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +1 -1 lines
Log Message:
coding style

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/licenses/publicdomain
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.*;
22 import java.util.concurrent.locks.*;
23 import java.util.*;
24
25 public final class CancelledFutureLoops {
26 static final ExecutorService pool = Executors.newCachedThreadPool();
27 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
28 static boolean print = false;
29 static final int ITERS = 10000000;
30 static final long TIMEOUT = 100;
31
32 public static void main(String[] args) throws Exception {
33 int maxThreads = 100;
34 if (args.length > 0)
35 maxThreads = Integer.parseInt(args[0]);
36
37 print = true;
38
39 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
40 System.out.print("Threads: " + i);
41 new FutureLoop(i).test();
42 Thread.sleep(TIMEOUT);
43 }
44 pool.shutdown();
45 }
46
47 static final class FutureLoop implements Callable {
48 private int v = rng.next();
49 private final ReentrantLock lock = new ReentrantLock();
50 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
51 private final CyclicBarrier barrier;
52 private final int nthreads;
53 FutureLoop(int nthreads) {
54 this.nthreads = nthreads;
55 barrier = new CyclicBarrier(nthreads+1, timer);
56 }
57
58 final void test() throws Exception {
59 Future[] futures = new Future[nthreads];
60 for (int i = 0; i < nthreads; ++i)
61 futures[i] = pool.submit(this);
62
63 barrier.await();
64 Thread.sleep(TIMEOUT);
65 boolean tooLate = false;
66 for (int i = 1; i < nthreads; ++i) {
67 if (!futures[i].cancel(true))
68 tooLate = true;
69 // Unbunch some of the cancels
70 if ( (i & 3) == 0)
71 Thread.sleep(1 + rng.next() % 10);
72 }
73
74 Object f0 = futures[0].get();
75 if (!tooLate) {
76 for (int i = 1; i < nthreads; ++i) {
77 if (!futures[i].isDone() || !futures[i].isCancelled())
78 throw new Error("Only one thread should complete");
79 }
80 }
81 else
82 System.out.print("(cancelled too late) ");
83
84 long endTime = System.nanoTime();
85 long time = endTime - timer.startTime;
86 if (print) {
87 double secs = (double) time / 1000000000.0;
88 System.out.println("\t " + secs + "s run time");
89 }
90
91 }
92
93 public final Object call() throws Exception {
94 barrier.await();
95 int sum = v;
96 int x = 0;
97 int n = ITERS;
98 while (n-- > 0) {
99 lock.lockInterruptibly();
100 try {
101 v = x = LoopHelpers.compute1(v);
102 }
103 finally {
104 lock.unlock();
105 }
106 sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
107 }
108 return new Integer(sum);
109 }
110 }
111
112 }