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 |
|
|
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 |
jsr166 |
1.3 |
static final long TIMEOUT = 100; |
31 |
dl |
1.1 |
|
32 |
|
|
public static void main(String[] args) throws Exception { |
33 |
|
|
int maxThreads = 100; |
34 |
jsr166 |
1.3 |
if (args.length > 0) |
35 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
for (int i = 0; i < nthreads; ++i) |
61 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
if ( (i & 3) == 0) |
71 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
|
84 |
dl |
1.1 |
long endTime = System.nanoTime(); |
85 |
|
|
long time = endTime - timer.startTime; |
86 |
|
|
if (print) { |
87 |
jsr166 |
1.4 |
double secs = (double) time / 1000000000.0; |
88 |
dl |
1.1 |
System.out.println("\t " + secs + "s run time"); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
public final Object call() throws Exception { |
94 |
jsr166 |
1.3 |
barrier.await(); |
95 |
dl |
1.1 |
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 |
jsr166 |
1.3 |
} |
108 |
dl |
1.1 |
return new Integer(sum); |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
} |