1 |
dl |
1.1 |
/* |
2 |
dl |
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 |
jsr166 |
1.5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
|
|
/* |
8 |
dl |
1.1 |
* @test |
9 |
jsr166 |
1.3 |
* @summary Checks for responsiveness of futures to cancellation. |
10 |
dl |
1.1 |
* 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 |
jsr166 |
1.9 |
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 |
dl |
1.1 |
|
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 |
jsr166 |
1.3 |
static final long TIMEOUT = 100; |
34 |
dl |
1.1 |
|
35 |
|
|
public static void main(String[] args) throws Exception { |
36 |
|
|
int maxThreads = 100; |
37 |
jsr166 |
1.3 |
if (args.length > 0) |
38 |
dl |
1.1 |
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 |
jsr166 |
1.8 |
static final class FutureLoop implements Callable<Object> { |
51 |
dl |
1.1 |
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 |
jsr166 |
1.6 |
Future<?>[] futures = new Future<?>[nthreads]; |
63 |
jsr166 |
1.3 |
for (int i = 0; i < nthreads; ++i) |
64 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
if ( (i & 3) == 0) |
74 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
|
87 |
dl |
1.1 |
long endTime = System.nanoTime(); |
88 |
|
|
long time = endTime - timer.startTime; |
89 |
|
|
if (print) { |
90 |
jsr166 |
1.4 |
double secs = (double) time / 1000000000.0; |
91 |
dl |
1.1 |
System.out.println("\t " + secs + "s run time"); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
public final Object call() throws Exception { |
97 |
jsr166 |
1.3 |
barrier.await(); |
98 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
} |
111 |
dl |
1.1 |
return new Integer(sum); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
} |