3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
– |
import junit.framework.*; |
7 |
– |
import java.util.concurrent.*; |
8 |
– |
import java.util.*; |
6 |
|
|
7 |
+ |
import junit.framework.*; |
8 |
+ |
import java.util.concurrent.CancellationException; |
9 |
+ |
import java.util.concurrent.ExecutionException; |
10 |
+ |
import java.util.concurrent.ForkJoinPool; |
11 |
+ |
import java.util.concurrent.ForkJoinTask; |
12 |
+ |
import java.util.concurrent.ForkJoinWorkerThread; |
13 |
+ |
import java.util.concurrent.RecursiveTask; |
14 |
+ |
import java.util.concurrent.TimeUnit; |
15 |
+ |
import java.util.concurrent.TimeoutException; |
16 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
17 |
+ |
import java.util.HashSet; |
18 |
|
|
19 |
|
public class RecursiveTaskTest extends JSR166TestCase { |
20 |
|
|
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run (suite()); |
22 |
> |
junit.textui.TestRunner.run(suite()); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(RecursiveTaskTest.class); |
25 |
> |
return new TestSuite(RecursiveTaskTest.class); |
26 |
> |
} |
27 |
> |
|
28 |
> |
private static ForkJoinPool mainPool() { |
29 |
> |
return new ForkJoinPool(); |
30 |
> |
} |
31 |
> |
|
32 |
> |
private static ForkJoinPool singletonPool() { |
33 |
> |
return new ForkJoinPool(1); |
34 |
> |
} |
35 |
> |
|
36 |
> |
private static ForkJoinPool asyncSingletonPool() { |
37 |
> |
return new ForkJoinPool(1, |
38 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
39 |
> |
null, true); |
40 |
> |
} |
41 |
> |
|
42 |
> |
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) { |
43 |
> |
try { |
44 |
> |
checkNotDone(a); |
45 |
> |
|
46 |
> |
T result = pool.invoke(a); |
47 |
> |
|
48 |
> |
checkCompletedNormally(a, result); |
49 |
> |
return result; |
50 |
> |
} finally { |
51 |
> |
joinPool(pool); |
52 |
> |
} |
53 |
> |
} |
54 |
> |
|
55 |
> |
void checkNotDone(RecursiveTask a) { |
56 |
> |
assertFalse(a.isDone()); |
57 |
> |
assertFalse(a.isCompletedNormally()); |
58 |
> |
assertFalse(a.isCompletedAbnormally()); |
59 |
> |
assertFalse(a.isCancelled()); |
60 |
> |
assertNull(a.getException()); |
61 |
> |
assertNull(a.getRawResult()); |
62 |
> |
|
63 |
> |
if (! ForkJoinTask.inForkJoinPool()) { |
64 |
> |
Thread.currentThread().interrupt(); |
65 |
> |
try { |
66 |
> |
a.get(); |
67 |
> |
shouldThrow(); |
68 |
> |
} catch (InterruptedException success) { |
69 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
70 |
> |
|
71 |
> |
Thread.currentThread().interrupt(); |
72 |
> |
try { |
73 |
> |
a.get(5L, SECONDS); |
74 |
> |
shouldThrow(); |
75 |
> |
} catch (InterruptedException success) { |
76 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
77 |
> |
} |
78 |
> |
|
79 |
> |
try { |
80 |
> |
a.get(0L, SECONDS); |
81 |
> |
shouldThrow(); |
82 |
> |
} catch (TimeoutException success) { |
83 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
> |
} |
85 |
> |
|
86 |
> |
<T> void checkCompletedNormally(RecursiveTask<T> a, T expected) { |
87 |
> |
assertTrue(a.isDone()); |
88 |
> |
assertFalse(a.isCancelled()); |
89 |
> |
assertTrue(a.isCompletedNormally()); |
90 |
> |
assertFalse(a.isCompletedAbnormally()); |
91 |
> |
assertNull(a.getException()); |
92 |
> |
assertSame(expected, a.getRawResult()); |
93 |
> |
assertSame(expected, a.join()); |
94 |
> |
assertFalse(a.cancel(false)); |
95 |
> |
assertFalse(a.cancel(true)); |
96 |
> |
try { |
97 |
> |
assertSame(expected, a.get()); |
98 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
> |
try { |
100 |
> |
assertSame(expected, a.get(5L, SECONDS)); |
101 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
102 |
|
} |
103 |
|
|
104 |
< |
static final ForkJoinPool mainPool = new ForkJoinPool(); |
105 |
< |
static final ForkJoinPool singletonPool = new ForkJoinPool(1); |
106 |
< |
static final ForkJoinPool asyncSingletonPool = new ForkJoinPool(1); |
107 |
< |
static { |
108 |
< |
asyncSingletonPool.setAsyncMode(true); |
104 |
> |
/** |
105 |
> |
* Waits for the task to complete, and checks that when it does, |
106 |
> |
* it will have an Integer result equals to the given int. |
107 |
> |
*/ |
108 |
> |
void checkCompletesNormally(RecursiveTask<Integer> a, int expected) { |
109 |
> |
Integer r = a.join(); |
110 |
> |
assertEquals(expected, (int) r); |
111 |
> |
checkCompletedNormally(a, r); |
112 |
> |
} |
113 |
> |
|
114 |
> |
/** |
115 |
> |
* Like checkCompletesNormally, but verifies that the task has |
116 |
> |
* already completed. |
117 |
> |
*/ |
118 |
> |
void checkCompletedNormally(RecursiveTask<Integer> a, int expected) { |
119 |
> |
Integer r = a.getRawResult(); |
120 |
> |
assertEquals(expected, (int) r); |
121 |
> |
checkCompletedNormally(a, r); |
122 |
> |
} |
123 |
> |
|
124 |
> |
void checkCancelled(RecursiveTask a) { |
125 |
> |
assertTrue(a.isDone()); |
126 |
> |
assertTrue(a.isCancelled()); |
127 |
> |
assertFalse(a.isCompletedNormally()); |
128 |
> |
assertTrue(a.isCompletedAbnormally()); |
129 |
> |
assertTrue(a.getException() instanceof CancellationException); |
130 |
> |
assertNull(a.getRawResult()); |
131 |
> |
|
132 |
> |
try { |
133 |
> |
a.join(); |
134 |
> |
shouldThrow(); |
135 |
> |
} catch (CancellationException success) { |
136 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
> |
|
138 |
> |
try { |
139 |
> |
a.get(); |
140 |
> |
shouldThrow(); |
141 |
> |
} catch (CancellationException success) { |
142 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
143 |
> |
|
144 |
> |
try { |
145 |
> |
a.get(5L, SECONDS); |
146 |
> |
shouldThrow(); |
147 |
> |
} catch (CancellationException success) { |
148 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
> |
} |
150 |
> |
|
151 |
> |
void checkCompletedAbnormally(RecursiveTask a, Throwable t) { |
152 |
> |
assertTrue(a.isDone()); |
153 |
> |
assertFalse(a.isCancelled()); |
154 |
> |
assertFalse(a.isCompletedNormally()); |
155 |
> |
assertTrue(a.isCompletedAbnormally()); |
156 |
> |
assertSame(t, a.getException()); |
157 |
> |
assertNull(a.getRawResult()); |
158 |
> |
assertFalse(a.cancel(false)); |
159 |
> |
assertFalse(a.cancel(true)); |
160 |
> |
|
161 |
> |
try { |
162 |
> |
a.join(); |
163 |
> |
shouldThrow(); |
164 |
> |
} catch (Throwable expected) { |
165 |
> |
assertSame(t, expected); |
166 |
> |
} |
167 |
> |
|
168 |
> |
try { |
169 |
> |
a.get(); |
170 |
> |
shouldThrow(); |
171 |
> |
} catch (ExecutionException success) { |
172 |
> |
assertSame(t, success.getCause()); |
173 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
174 |
> |
|
175 |
> |
try { |
176 |
> |
a.get(5L, SECONDS); |
177 |
> |
shouldThrow(); |
178 |
> |
} catch (ExecutionException success) { |
179 |
> |
assertSame(t, success.getCause()); |
180 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
181 |
|
} |
182 |
|
|
183 |
|
static final class FJException extends RuntimeException { |
188 |
|
static final Integer NoResult = Integer.valueOf(-17); |
189 |
|
|
190 |
|
// A simple recursive task for testing |
191 |
< |
static final class FibTask extends RecursiveTask<Integer> { |
191 |
> |
final class FibTask extends CheckedRecursiveTask<Integer> { |
192 |
|
final int number; |
193 |
|
FibTask(int n) { number = n; } |
194 |
< |
public Integer compute() { |
194 |
> |
public Integer realCompute() { |
195 |
|
int n = number; |
196 |
|
if (n <= 1) |
197 |
|
return n; |
199 |
|
f1.fork(); |
200 |
|
return (new FibTask(n - 2)).compute() + f1.join(); |
201 |
|
} |
202 |
+ |
|
203 |
+ |
public void publicSetRawResult(Integer result) { |
204 |
+ |
setRawResult(result); |
205 |
+ |
} |
206 |
|
} |
207 |
|
|
208 |
|
// A recursive action failing in base case |
209 |
< |
static final class FailingFibTask extends RecursiveTask<Integer> { |
209 |
> |
final class FailingFibTask extends RecursiveTask<Integer> { |
210 |
|
final int number; |
211 |
|
int result; |
212 |
|
FailingFibTask(int n) { number = n; } |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
< |
/** |
223 |
> |
/** |
224 |
|
* invoke returns value when task completes normally. |
225 |
|
* isCompletedAbnormally and isCancelled return false for normally |
226 |
|
* completed tasks. getRawResult of a completed non-null task |
227 |
|
* returns value; |
68 |
– |
* |
228 |
|
*/ |
229 |
|
public void testInvoke() { |
230 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
231 |
< |
public Integer compute() { |
232 |
< |
FibTask f = new FibTask(8); |
233 |
< |
Integer r = f.invoke(); |
234 |
< |
threadAssertTrue(r == 21); |
235 |
< |
threadAssertTrue(f.isDone()); |
236 |
< |
threadAssertFalse(f.isCancelled()); |
237 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
238 |
< |
threadAssertTrue(f.getRawResult() == 21); |
80 |
< |
return r; |
81 |
< |
} |
82 |
< |
}; |
83 |
< |
assertTrue(mainPool.invoke(a) == 21); |
230 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
231 |
> |
public Integer realCompute() { |
232 |
> |
FibTask f = new FibTask(8); |
233 |
> |
Integer r = f.invoke(); |
234 |
> |
assertEquals(21, (int) r); |
235 |
> |
checkCompletedNormally(f, r); |
236 |
> |
return r; |
237 |
> |
}}; |
238 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
239 |
|
} |
240 |
|
|
241 |
< |
/** |
241 |
> |
/** |
242 |
|
* quietlyInvoke task returns when task completes normally. |
243 |
|
* isCompletedAbnormally and isCancelled return false for normally |
244 |
|
* completed tasks |
245 |
|
*/ |
246 |
|
public void testQuietlyInvoke() { |
247 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
248 |
< |
public Integer compute() { |
249 |
< |
FibTask f = new FibTask(8); |
250 |
< |
f.quietlyInvoke(); |
251 |
< |
Integer r = f.getRawResult(); |
252 |
< |
threadAssertTrue(r == 21); |
253 |
< |
threadAssertTrue(f.isDone()); |
254 |
< |
threadAssertFalse(f.isCancelled()); |
100 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
101 |
< |
return r; |
102 |
< |
} |
103 |
< |
}; |
104 |
< |
assertTrue(mainPool.invoke(a) == 21); |
247 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
248 |
> |
public Integer realCompute() { |
249 |
> |
FibTask f = new FibTask(8); |
250 |
> |
f.quietlyInvoke(); |
251 |
> |
checkCompletedNormally(f, 21); |
252 |
> |
return NoResult; |
253 |
> |
}}; |
254 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
255 |
|
} |
256 |
|
|
257 |
< |
/** |
257 |
> |
/** |
258 |
|
* join of a forked task returns when task completes |
259 |
|
*/ |
260 |
|
public void testForkJoin() { |
261 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
262 |
< |
public Integer compute() { |
263 |
< |
FibTask f = new FibTask(8); |
264 |
< |
f.fork(); |
265 |
< |
Integer r = f.join(); |
266 |
< |
threadAssertTrue(r == 21); |
267 |
< |
threadAssertTrue(f.isDone()); |
268 |
< |
return r; |
269 |
< |
} |
270 |
< |
}; |
121 |
< |
assertTrue(mainPool.invoke(a) == 21); |
261 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
262 |
> |
public Integer realCompute() { |
263 |
> |
FibTask f = new FibTask(8); |
264 |
> |
assertSame(f, f.fork()); |
265 |
> |
Integer r = f.join(); |
266 |
> |
assertEquals(21, (int) r); |
267 |
> |
checkCompletedNormally(f, r); |
268 |
> |
return r; |
269 |
> |
}}; |
270 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
271 |
|
} |
272 |
|
|
273 |
< |
/** |
273 |
> |
/** |
274 |
|
* get of a forked task returns when task completes |
275 |
|
*/ |
276 |
|
public void testForkGet() { |
277 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
278 |
< |
public Integer compute() { |
279 |
< |
try { |
280 |
< |
FibTask f = new FibTask(8); |
281 |
< |
f.fork(); |
282 |
< |
Integer r = f.get(); |
283 |
< |
threadAssertTrue(r == 21); |
284 |
< |
threadAssertTrue(f.isDone()); |
285 |
< |
return r; |
286 |
< |
} catch(Exception ex) { |
138 |
< |
unexpectedException(); |
139 |
< |
} |
140 |
< |
return NoResult; |
141 |
< |
} |
142 |
< |
}; |
143 |
< |
assertTrue(mainPool.invoke(a) == 21); |
277 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
278 |
> |
public Integer realCompute() throws Exception { |
279 |
> |
FibTask f = new FibTask(8); |
280 |
> |
assertSame(f, f.fork()); |
281 |
> |
Integer r = f.get(); |
282 |
> |
assertEquals(21, (int) r); |
283 |
> |
checkCompletedNormally(f, r); |
284 |
> |
return r; |
285 |
> |
}}; |
286 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
287 |
|
} |
288 |
|
|
289 |
< |
/** |
289 |
> |
/** |
290 |
|
* timed get of a forked task returns when task completes |
291 |
|
*/ |
292 |
|
public void testForkTimedGet() { |
293 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
294 |
< |
public Integer compute() { |
295 |
< |
try { |
296 |
< |
FibTask f = new FibTask(8); |
297 |
< |
f.fork(); |
298 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
299 |
< |
threadAssertTrue(r == 21); |
300 |
< |
threadAssertTrue(f.isDone()); |
301 |
< |
return r; |
302 |
< |
} catch(Exception ex) { |
160 |
< |
unexpectedException(); |
161 |
< |
} |
162 |
< |
return NoResult; |
163 |
< |
} |
164 |
< |
}; |
165 |
< |
assertTrue(mainPool.invoke(a) == 21); |
293 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
294 |
> |
public Integer realCompute() throws Exception { |
295 |
> |
FibTask f = new FibTask(8); |
296 |
> |
assertSame(f, f.fork()); |
297 |
> |
Integer r = f.get(5L, SECONDS); |
298 |
> |
assertEquals(21, (int) r); |
299 |
> |
checkCompletedNormally(f, r); |
300 |
> |
return r; |
301 |
> |
}}; |
302 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
303 |
|
} |
304 |
|
|
305 |
< |
/** |
169 |
< |
* helpJoin of a forked task returns when task completes |
170 |
< |
*/ |
171 |
< |
public void testForkHelpJoin() { |
172 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
173 |
< |
public Integer compute() { |
174 |
< |
FibTask f = new FibTask(8); |
175 |
< |
f.fork(); |
176 |
< |
Integer r = f.helpJoin(); |
177 |
< |
threadAssertTrue(r == 21); |
178 |
< |
threadAssertTrue(f.isDone()); |
179 |
< |
return r; |
180 |
< |
} |
181 |
< |
}; |
182 |
< |
assertTrue(mainPool.invoke(a) == 21); |
183 |
< |
} |
184 |
< |
|
185 |
< |
/** |
305 |
> |
/** |
306 |
|
* quietlyJoin of a forked task returns when task completes |
307 |
|
*/ |
308 |
|
public void testForkQuietlyJoin() { |
309 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
310 |
< |
public Integer compute() { |
311 |
< |
FibTask f = new FibTask(8); |
312 |
< |
f.fork(); |
313 |
< |
f.quietlyJoin(); |
314 |
< |
Integer r = f.getRawResult(); |
315 |
< |
threadAssertTrue(r == 21); |
316 |
< |
threadAssertTrue(f.isDone()); |
317 |
< |
return r; |
318 |
< |
} |
319 |
< |
}; |
200 |
< |
assertTrue(mainPool.invoke(a) == 21); |
309 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
310 |
> |
public Integer realCompute() { |
311 |
> |
FibTask f = new FibTask(8); |
312 |
> |
assertSame(f, f.fork()); |
313 |
> |
f.quietlyJoin(); |
314 |
> |
Integer r = f.getRawResult(); |
315 |
> |
assertEquals(21, (int) r); |
316 |
> |
checkCompletedNormally(f, r); |
317 |
> |
return r; |
318 |
> |
}}; |
319 |
> |
assertEquals(21, (int) testInvokeOnPool(mainPool(), a)); |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
< |
/** |
205 |
< |
* quietlyHelpJoin of a forked task returns when task completes |
206 |
< |
*/ |
207 |
< |
public void testForkQuietlyHelpJoin() { |
208 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
209 |
< |
public Integer compute() { |
210 |
< |
FibTask f = new FibTask(8); |
211 |
< |
f.fork(); |
212 |
< |
f.quietlyHelpJoin(); |
213 |
< |
Integer r = f.getRawResult(); |
214 |
< |
threadAssertTrue(r == 21); |
215 |
< |
threadAssertTrue(f.isDone()); |
216 |
< |
return r; |
217 |
< |
} |
218 |
< |
}; |
219 |
< |
assertTrue(mainPool.invoke(a) == 21); |
220 |
< |
} |
221 |
< |
|
222 |
< |
|
223 |
< |
/** |
323 |
> |
/** |
324 |
|
* helpQuiesce returns when tasks are complete. |
325 |
|
* getQueuedTaskCount returns 0 when quiescent |
326 |
|
*/ |
327 |
|
public void testForkHelpQuiesce() { |
328 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
329 |
< |
public Integer compute() { |
330 |
< |
FibTask f = new FibTask(8); |
331 |
< |
f.fork(); |
332 |
< |
f.helpQuiesce(); |
333 |
< |
Integer r = f.getRawResult(); |
334 |
< |
threadAssertTrue(r == 21); |
335 |
< |
threadAssertTrue(f.isDone()); |
336 |
< |
threadAssertTrue(getQueuedTaskCount() == 0); |
337 |
< |
return r; |
238 |
< |
} |
239 |
< |
}; |
240 |
< |
assertTrue(mainPool.invoke(a) == 21); |
328 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
329 |
> |
public Integer realCompute() { |
330 |
> |
FibTask f = new FibTask(8); |
331 |
> |
assertSame(f, f.fork()); |
332 |
> |
f.helpQuiesce(); |
333 |
> |
assertEquals(0, getQueuedTaskCount()); |
334 |
> |
checkCompletedNormally(f, 21); |
335 |
> |
return NoResult; |
336 |
> |
}}; |
337 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
< |
/** |
341 |
> |
/** |
342 |
|
* invoke task throws exception when task completes abnormally |
343 |
|
*/ |
344 |
|
public void testAbnormalInvoke() { |
345 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
346 |
< |
public Integer compute() { |
347 |
< |
try { |
348 |
< |
FailingFibTask f = new FailingFibTask(8); |
349 |
< |
f.invoke(); |
350 |
< |
shouldThrow(); |
351 |
< |
return NoResult; |
352 |
< |
} catch(FJException success) { |
256 |
< |
} |
257 |
< |
return NoResult; |
345 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
346 |
> |
public Integer realCompute() { |
347 |
> |
FailingFibTask f = new FailingFibTask(8); |
348 |
> |
try { |
349 |
> |
f.invoke(); |
350 |
> |
shouldThrow(); |
351 |
> |
} catch (FJException success) { |
352 |
> |
checkCompletedAbnormally(f, success); |
353 |
|
} |
354 |
< |
}; |
355 |
< |
mainPool.invoke(a); |
354 |
> |
return NoResult; |
355 |
> |
}}; |
356 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
357 |
|
} |
358 |
|
|
359 |
< |
/** |
360 |
< |
* quietelyInvoke task returns when task completes abnormally |
359 |
> |
/** |
360 |
> |
* quietlyInvoke task returns when task completes abnormally |
361 |
|
*/ |
362 |
|
public void testAbnormalQuietlyInvoke() { |
363 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
364 |
< |
public Integer compute() { |
365 |
< |
FailingFibTask f = new FailingFibTask(8); |
366 |
< |
f.quietlyInvoke(); |
367 |
< |
threadAssertTrue(f.isDone()); |
368 |
< |
return NoResult; |
369 |
< |
} |
370 |
< |
}; |
371 |
< |
mainPool.invoke(a); |
363 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
364 |
> |
public Integer realCompute() { |
365 |
> |
FailingFibTask f = new FailingFibTask(8); |
366 |
> |
f.quietlyInvoke(); |
367 |
> |
assertTrue(f.getException() instanceof FJException); |
368 |
> |
checkCompletedAbnormally(f, f.getException()); |
369 |
> |
return NoResult; |
370 |
> |
}}; |
371 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
372 |
|
} |
373 |
|
|
374 |
< |
/** |
374 |
> |
/** |
375 |
|
* join of a forked task throws exception when task completes abnormally |
376 |
|
*/ |
377 |
|
public void testAbnormalForkJoin() { |
378 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
379 |
< |
public Integer compute() { |
380 |
< |
try { |
381 |
< |
FailingFibTask f = new FailingFibTask(8); |
382 |
< |
f.fork(); |
383 |
< |
Integer r = f.join(); |
384 |
< |
shouldThrow(); |
385 |
< |
return r; |
386 |
< |
} catch(FJException success) { |
291 |
< |
} |
292 |
< |
return NoResult; |
378 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
379 |
> |
public Integer realCompute() { |
380 |
> |
FailingFibTask f = new FailingFibTask(8); |
381 |
> |
assertSame(f, f.fork()); |
382 |
> |
try { |
383 |
> |
Integer r = f.join(); |
384 |
> |
shouldThrow(); |
385 |
> |
} catch (FJException success) { |
386 |
> |
checkCompletedAbnormally(f, success); |
387 |
|
} |
388 |
< |
}; |
389 |
< |
mainPool.invoke(a); |
388 |
> |
return NoResult; |
389 |
> |
}}; |
390 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
391 |
|
} |
392 |
|
|
393 |
< |
/** |
393 |
> |
/** |
394 |
|
* get of a forked task throws exception when task completes abnormally |
395 |
|
*/ |
396 |
|
public void testAbnormalForkGet() { |
397 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
398 |
< |
public Integer compute() { |
399 |
< |
try { |
400 |
< |
FailingFibTask f = new FailingFibTask(8); |
401 |
< |
f.fork(); |
402 |
< |
Integer r = f.get(); |
403 |
< |
shouldThrow(); |
404 |
< |
return r; |
405 |
< |
} catch(Exception success) { |
406 |
< |
} |
407 |
< |
return NoResult; |
397 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
398 |
> |
public Integer realCompute() throws Exception { |
399 |
> |
FailingFibTask f = new FailingFibTask(8); |
400 |
> |
assertSame(f, f.fork()); |
401 |
> |
try { |
402 |
> |
Integer r = f.get(); |
403 |
> |
shouldThrow(); |
404 |
> |
} catch (ExecutionException success) { |
405 |
> |
Throwable cause = success.getCause(); |
406 |
> |
assertTrue(cause instanceof FJException); |
407 |
> |
checkCompletedAbnormally(f, cause); |
408 |
|
} |
409 |
< |
}; |
410 |
< |
mainPool.invoke(a); |
409 |
> |
return NoResult; |
410 |
> |
}}; |
411 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
412 |
|
} |
413 |
|
|
414 |
< |
/** |
414 |
> |
/** |
415 |
|
* timed get of a forked task throws exception when task completes abnormally |
416 |
|
*/ |
417 |
|
public void testAbnormalForkTimedGet() { |
418 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
419 |
< |
public Integer compute() { |
420 |
< |
try { |
421 |
< |
FailingFibTask f = new FailingFibTask(8); |
422 |
< |
f.fork(); |
423 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
424 |
< |
shouldThrow(); |
425 |
< |
return r; |
426 |
< |
} catch(Exception success) { |
427 |
< |
} |
428 |
< |
return NoResult; |
333 |
< |
} |
334 |
< |
}; |
335 |
< |
mainPool.invoke(a); |
336 |
< |
} |
337 |
< |
|
338 |
< |
/** |
339 |
< |
* join of a forked task throws exception when task completes abnormally |
340 |
< |
*/ |
341 |
< |
public void testAbnormalForkHelpJoin() { |
342 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
343 |
< |
public Integer compute() { |
344 |
< |
try { |
345 |
< |
FailingFibTask f = new FailingFibTask(8); |
346 |
< |
f.fork(); |
347 |
< |
Integer r = f.helpJoin(); |
348 |
< |
shouldThrow(); |
349 |
< |
return r; |
350 |
< |
} catch(FJException success) { |
351 |
< |
} |
352 |
< |
return NoResult; |
353 |
< |
} |
354 |
< |
}; |
355 |
< |
mainPool.invoke(a); |
356 |
< |
} |
357 |
< |
|
358 |
< |
/** |
359 |
< |
* quietlyHelpJoin of a forked task returns when task completes abnormally. |
360 |
< |
* getException of failed task returns its exception. |
361 |
< |
* isCompletedAbnormally of a failed task returns true. |
362 |
< |
* isCancelled of a failed uncancelled task returns false |
363 |
< |
*/ |
364 |
< |
public void testAbnormalForkQuietlyHelpJoin() { |
365 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
366 |
< |
public Integer compute() { |
367 |
< |
FailingFibTask f = new FailingFibTask(8); |
368 |
< |
f.fork(); |
369 |
< |
f.quietlyHelpJoin(); |
370 |
< |
threadAssertTrue(f.isDone()); |
371 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
372 |
< |
threadAssertFalse(f.isCancelled()); |
373 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
374 |
< |
return NoResult; |
418 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
419 |
> |
public Integer realCompute() throws Exception { |
420 |
> |
FailingFibTask f = new FailingFibTask(8); |
421 |
> |
assertSame(f, f.fork()); |
422 |
> |
try { |
423 |
> |
Integer r = f.get(5L, SECONDS); |
424 |
> |
shouldThrow(); |
425 |
> |
} catch (ExecutionException success) { |
426 |
> |
Throwable cause = success.getCause(); |
427 |
> |
assertTrue(cause instanceof FJException); |
428 |
> |
checkCompletedAbnormally(f, cause); |
429 |
|
} |
430 |
< |
}; |
431 |
< |
mainPool.invoke(a); |
430 |
> |
return NoResult; |
431 |
> |
}}; |
432 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
433 |
|
} |
434 |
|
|
435 |
< |
/** |
435 |
> |
/** |
436 |
|
* quietlyJoin of a forked task returns when task completes abnormally |
437 |
|
*/ |
438 |
|
public void testAbnormalForkQuietlyJoin() { |
439 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
440 |
< |
public Integer compute() { |
441 |
< |
FailingFibTask f = new FailingFibTask(8); |
442 |
< |
f.fork(); |
443 |
< |
f.quietlyJoin(); |
444 |
< |
threadAssertTrue(f.isDone()); |
445 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
446 |
< |
threadAssertTrue(f.getException() instanceof FJException); |
447 |
< |
return NoResult; |
448 |
< |
} |
394 |
< |
}; |
395 |
< |
mainPool.invoke(a); |
439 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
440 |
> |
public Integer realCompute() { |
441 |
> |
FailingFibTask f = new FailingFibTask(8); |
442 |
> |
assertSame(f, f.fork()); |
443 |
> |
f.quietlyJoin(); |
444 |
> |
assertTrue(f.getException() instanceof FJException); |
445 |
> |
checkCompletedAbnormally(f, f.getException()); |
446 |
> |
return NoResult; |
447 |
> |
}}; |
448 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
449 |
|
} |
450 |
|
|
451 |
< |
/** |
451 |
> |
/** |
452 |
|
* invoke task throws exception when task cancelled |
453 |
|
*/ |
454 |
|
public void testCancelledInvoke() { |
455 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
456 |
< |
public Integer compute() { |
457 |
< |
try { |
458 |
< |
FibTask f = new FibTask(8); |
459 |
< |
f.cancel(true); |
460 |
< |
Integer r = f.invoke(); |
461 |
< |
shouldThrow(); |
462 |
< |
return r; |
463 |
< |
} catch(CancellationException success) { |
411 |
< |
} |
412 |
< |
return NoResult; |
455 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
456 |
> |
public Integer realCompute() { |
457 |
> |
FibTask f = new FibTask(8); |
458 |
> |
assertTrue(f.cancel(true)); |
459 |
> |
try { |
460 |
> |
Integer r = f.invoke(); |
461 |
> |
shouldThrow(); |
462 |
> |
} catch (CancellationException success) { |
463 |
> |
checkCancelled(f); |
464 |
|
} |
465 |
< |
}; |
466 |
< |
mainPool.invoke(a); |
465 |
> |
return NoResult; |
466 |
> |
}}; |
467 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
468 |
|
} |
469 |
|
|
470 |
< |
/** |
470 |
> |
/** |
471 |
|
* join of a forked task throws exception when task cancelled |
472 |
|
*/ |
473 |
|
public void testCancelledForkJoin() { |
474 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
475 |
< |
public Integer compute() { |
476 |
< |
try { |
477 |
< |
FibTask f = new FibTask(8); |
478 |
< |
f.cancel(true); |
479 |
< |
f.fork(); |
480 |
< |
Integer r = f.join(); |
481 |
< |
shouldThrow(); |
482 |
< |
return r; |
483 |
< |
} catch(CancellationException success) { |
432 |
< |
} |
433 |
< |
return NoResult; |
474 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
475 |
> |
public Integer realCompute() { |
476 |
> |
FibTask f = new FibTask(8); |
477 |
> |
assertTrue(f.cancel(true)); |
478 |
> |
assertSame(f, f.fork()); |
479 |
> |
try { |
480 |
> |
Integer r = f.join(); |
481 |
> |
shouldThrow(); |
482 |
> |
} catch (CancellationException success) { |
483 |
> |
checkCancelled(f); |
484 |
|
} |
485 |
< |
}; |
486 |
< |
mainPool.invoke(a); |
485 |
> |
return NoResult; |
486 |
> |
}}; |
487 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
488 |
|
} |
489 |
|
|
490 |
< |
/** |
490 |
> |
/** |
491 |
|
* get of a forked task throws exception when task cancelled |
492 |
|
*/ |
493 |
|
public void testCancelledForkGet() { |
494 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
495 |
< |
public Integer compute() { |
496 |
< |
try { |
497 |
< |
FibTask f = new FibTask(8); |
498 |
< |
f.cancel(true); |
499 |
< |
f.fork(); |
500 |
< |
Integer r = f.get(); |
501 |
< |
shouldThrow(); |
502 |
< |
return r; |
503 |
< |
} catch(Exception success) { |
453 |
< |
} |
454 |
< |
return NoResult; |
494 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
495 |
> |
public Integer realCompute() throws Exception { |
496 |
> |
FibTask f = new FibTask(8); |
497 |
> |
assertTrue(f.cancel(true)); |
498 |
> |
assertSame(f, f.fork()); |
499 |
> |
try { |
500 |
> |
Integer r = f.get(); |
501 |
> |
shouldThrow(); |
502 |
> |
} catch (CancellationException success) { |
503 |
> |
checkCancelled(f); |
504 |
|
} |
505 |
< |
}; |
506 |
< |
mainPool.invoke(a); |
505 |
> |
return NoResult; |
506 |
> |
}}; |
507 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
508 |
|
} |
509 |
|
|
510 |
< |
/** |
510 |
> |
/** |
511 |
|
* timed get of a forked task throws exception when task cancelled |
512 |
|
*/ |
513 |
|
public void testCancelledForkTimedGet() { |
514 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
515 |
< |
public Integer compute() { |
516 |
< |
try { |
517 |
< |
FibTask f = new FibTask(8); |
518 |
< |
f.cancel(true); |
519 |
< |
f.fork(); |
520 |
< |
Integer r = f.get(5L, TimeUnit.SECONDS); |
521 |
< |
shouldThrow(); |
522 |
< |
return r; |
523 |
< |
} catch(Exception success) { |
474 |
< |
} |
475 |
< |
return NoResult; |
476 |
< |
} |
477 |
< |
}; |
478 |
< |
mainPool.invoke(a); |
479 |
< |
} |
480 |
< |
|
481 |
< |
/** |
482 |
< |
* join of a forked task throws exception when task cancelled |
483 |
< |
*/ |
484 |
< |
public void testCancelledForkHelpJoin() { |
485 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
486 |
< |
public Integer compute() { |
487 |
< |
try { |
488 |
< |
FibTask f = new FibTask(8); |
489 |
< |
f.cancel(true); |
490 |
< |
f.fork(); |
491 |
< |
Integer r = f.helpJoin(); |
492 |
< |
shouldThrow(); |
493 |
< |
return r; |
494 |
< |
} catch(CancellationException success) { |
495 |
< |
} |
496 |
< |
return NoResult; |
514 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
515 |
> |
public Integer realCompute() throws Exception { |
516 |
> |
FibTask f = new FibTask(8); |
517 |
> |
assertTrue(f.cancel(true)); |
518 |
> |
assertSame(f, f.fork()); |
519 |
> |
try { |
520 |
> |
Integer r = f.get(5L, SECONDS); |
521 |
> |
shouldThrow(); |
522 |
> |
} catch (CancellationException success) { |
523 |
> |
checkCancelled(f); |
524 |
|
} |
525 |
< |
}; |
526 |
< |
mainPool.invoke(a); |
527 |
< |
} |
501 |
< |
|
502 |
< |
/** |
503 |
< |
* quietlyHelpJoin of a forked task returns when task cancelled. |
504 |
< |
* getException of cancelled task returns its exception |
505 |
< |
* isCompletedAbnormally of a cancelled task returns true. |
506 |
< |
* isCancelled of a cancelled task returns true |
507 |
< |
*/ |
508 |
< |
public void testCancelledForkQuietlyHelpJoin() { |
509 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
510 |
< |
public Integer compute() { |
511 |
< |
FibTask f = new FibTask(8); |
512 |
< |
f.cancel(true); |
513 |
< |
f.fork(); |
514 |
< |
f.quietlyHelpJoin(); |
515 |
< |
threadAssertTrue(f.isDone()); |
516 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
517 |
< |
threadAssertTrue(f.isCancelled()); |
518 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
519 |
< |
return NoResult; |
520 |
< |
} |
521 |
< |
}; |
522 |
< |
mainPool.invoke(a); |
525 |
> |
return NoResult; |
526 |
> |
}}; |
527 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
528 |
|
} |
529 |
|
|
530 |
< |
/** |
530 |
> |
/** |
531 |
|
* quietlyJoin of a forked task returns when task cancelled |
532 |
|
*/ |
533 |
|
public void testCancelledForkQuietlyJoin() { |
534 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
535 |
< |
public Integer compute() { |
536 |
< |
FibTask f = new FibTask(8); |
537 |
< |
f.cancel(true); |
538 |
< |
f.fork(); |
539 |
< |
f.quietlyJoin(); |
540 |
< |
threadAssertTrue(f.isDone()); |
541 |
< |
threadAssertTrue(f.isCompletedAbnormally()); |
542 |
< |
threadAssertTrue(f.getException() instanceof CancellationException); |
543 |
< |
return NoResult; |
539 |
< |
} |
540 |
< |
}; |
541 |
< |
mainPool.invoke(a); |
534 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
535 |
> |
public Integer realCompute() { |
536 |
> |
FibTask f = new FibTask(8); |
537 |
> |
assertTrue(f.cancel(true)); |
538 |
> |
assertSame(f, f.fork()); |
539 |
> |
f.quietlyJoin(); |
540 |
> |
checkCancelled(f); |
541 |
> |
return NoResult; |
542 |
> |
}}; |
543 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
|
* getPool of executing task returns its pool |
548 |
|
*/ |
549 |
|
public void testGetPool() { |
550 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
551 |
< |
public Integer compute() { |
552 |
< |
threadAssertTrue(getPool() == mainPool); |
553 |
< |
return NoResult; |
554 |
< |
} |
555 |
< |
}; |
556 |
< |
mainPool.invoke(a); |
550 |
> |
final ForkJoinPool mainPool = mainPool(); |
551 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
552 |
> |
public Integer realCompute() { |
553 |
> |
assertSame(mainPool, getPool()); |
554 |
> |
return NoResult; |
555 |
> |
}}; |
556 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool, a)); |
557 |
|
} |
558 |
|
|
559 |
|
/** |
560 |
|
* getPool of non-FJ task returns null |
561 |
|
*/ |
562 |
|
public void testGetPool2() { |
563 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
564 |
< |
public Integer compute() { |
565 |
< |
threadAssertTrue(getPool() == null); |
563 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
564 |
> |
public Integer realCompute() { |
565 |
> |
assertNull(getPool()); |
566 |
|
return NoResult; |
567 |
< |
} |
568 |
< |
}; |
567 |
< |
a.invoke(); |
567 |
> |
}}; |
568 |
> |
assertSame(NoResult, a.invoke()); |
569 |
|
} |
570 |
< |
|
570 |
> |
|
571 |
|
/** |
572 |
|
* inForkJoinPool of executing task returns true |
573 |
|
*/ |
574 |
|
public void testInForkJoinPool() { |
575 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
576 |
< |
public Integer compute() { |
577 |
< |
threadAssertTrue(inForkJoinPool()); |
578 |
< |
return NoResult; |
579 |
< |
} |
580 |
< |
}; |
580 |
< |
mainPool.invoke(a); |
575 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
576 |
> |
public Integer realCompute() { |
577 |
> |
assertTrue(inForkJoinPool()); |
578 |
> |
return NoResult; |
579 |
> |
}}; |
580 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
|
* inForkJoinPool of non-FJ task returns false |
585 |
|
*/ |
586 |
|
public void testInForkJoinPool2() { |
587 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
588 |
< |
public Integer compute() { |
589 |
< |
threadAssertTrue(!inForkJoinPool()); |
590 |
< |
return NoResult; |
591 |
< |
} |
592 |
< |
}; |
593 |
< |
a.invoke(); |
587 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
588 |
> |
public Integer realCompute() { |
589 |
> |
assertFalse(inForkJoinPool()); |
590 |
> |
return NoResult; |
591 |
> |
}}; |
592 |
> |
assertSame(NoResult, a.invoke()); |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
< |
* setRawResult(null) succeeds |
596 |
> |
* The value set by setRawResult is returned by getRawResult |
597 |
|
*/ |
598 |
|
public void testSetRawResult() { |
599 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
600 |
< |
public Integer compute() { |
601 |
< |
setRawResult(NoResult); |
602 |
< |
return NoResult; |
603 |
< |
} |
604 |
< |
}; |
605 |
< |
assertEquals(a.invoke(), NoResult); |
599 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
600 |
> |
public Integer realCompute() { |
601 |
> |
setRawResult(NoResult); |
602 |
> |
assertSame(NoResult, getRawResult()); |
603 |
> |
return NoResult; |
604 |
> |
} |
605 |
> |
}; |
606 |
> |
assertSame(NoResult, a.invoke()); |
607 |
|
} |
608 |
|
|
609 |
< |
/** |
610 |
< |
* A reinitialized task may be re-invoked |
609 |
> |
/** |
610 |
> |
* A reinitialized normally completed task may be re-invoked |
611 |
|
*/ |
612 |
|
public void testReinitialize() { |
613 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
614 |
< |
public Integer compute() { |
615 |
< |
FibTask f = new FibTask(8); |
613 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
614 |
> |
public Integer realCompute() { |
615 |
> |
FibTask f = new FibTask(8); |
616 |
> |
checkNotDone(f); |
617 |
> |
|
618 |
> |
for (int i = 0; i < 3; i++) { |
619 |
|
Integer r = f.invoke(); |
620 |
< |
threadAssertTrue(r == 21); |
621 |
< |
threadAssertTrue(f.isDone()); |
619 |
< |
threadAssertFalse(f.isCancelled()); |
620 |
< |
threadAssertFalse(f.isCompletedAbnormally()); |
620 |
> |
assertEquals(21, (int) r); |
621 |
> |
checkCompletedNormally(f, r); |
622 |
|
f.reinitialize(); |
623 |
< |
r = f.invoke(); |
624 |
< |
threadAssertTrue(r == 21); |
624 |
< |
return NoResult; |
623 |
> |
f.publicSetRawResult(null); |
624 |
> |
checkNotDone(f); |
625 |
|
} |
626 |
< |
}; |
627 |
< |
mainPool.invoke(a); |
626 |
> |
return NoResult; |
627 |
> |
}}; |
628 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
629 |
|
} |
630 |
|
|
631 |
< |
/** |
632 |
< |
* invoke task throws exception after invoking completeExceptionally |
631 |
> |
/** |
632 |
> |
* A reinitialized abnormally completed task may be re-invoked |
633 |
|
*/ |
634 |
< |
public void testCompleteExceptionally() { |
635 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
636 |
< |
public Integer compute() { |
634 |
> |
public void testReinitializeAbnormal() { |
635 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
636 |
> |
public Integer realCompute() { |
637 |
> |
FailingFibTask f = new FailingFibTask(8); |
638 |
> |
checkNotDone(f); |
639 |
> |
|
640 |
> |
for (int i = 0; i < 3; i++) { |
641 |
|
try { |
642 |
< |
FibTask f = new FibTask(8); |
638 |
< |
f.completeExceptionally(new FJException()); |
639 |
< |
Integer r = f.invoke(); |
642 |
> |
f.invoke(); |
643 |
|
shouldThrow(); |
644 |
< |
return r; |
645 |
< |
} catch(FJException success) { |
644 |
> |
} catch (FJException success) { |
645 |
> |
checkCompletedAbnormally(f, success); |
646 |
|
} |
647 |
< |
return NoResult; |
647 |
> |
f.reinitialize(); |
648 |
> |
checkNotDone(f); |
649 |
|
} |
650 |
< |
}; |
651 |
< |
mainPool.invoke(a); |
650 |
> |
return NoResult; |
651 |
> |
}}; |
652 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
653 |
|
} |
654 |
|
|
655 |
< |
/** |
656 |
< |
* invoke task suppresses execution invoking complete |
655 |
> |
/** |
656 |
> |
* invoke task throws exception after invoking completeExceptionally |
657 |
|
*/ |
658 |
< |
public void testComplete() { |
659 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
660 |
< |
public Integer compute() { |
661 |
< |
FibTask f = new FibTask(8); |
662 |
< |
f.complete(NoResult); |
658 |
> |
public void testCompleteExceptionally() { |
659 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
660 |
> |
public Integer realCompute() { |
661 |
> |
FibTask f = new FibTask(8); |
662 |
> |
f.completeExceptionally(new FJException()); |
663 |
> |
try { |
664 |
|
Integer r = f.invoke(); |
665 |
< |
threadAssertTrue(f.isDone()); |
666 |
< |
threadAssertTrue(r == NoResult); |
667 |
< |
return r; |
665 |
> |
shouldThrow(); |
666 |
> |
} catch (FJException success) { |
667 |
> |
checkCompletedAbnormally(f, success); |
668 |
|
} |
669 |
< |
}; |
670 |
< |
mainPool.invoke(a); |
669 |
> |
return NoResult; |
670 |
> |
}}; |
671 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
672 |
> |
} |
673 |
> |
|
674 |
> |
/** |
675 |
> |
* invoke task suppresses execution invoking complete |
676 |
> |
*/ |
677 |
> |
public void testComplete() { |
678 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
679 |
> |
public Integer realCompute() { |
680 |
> |
FibTask f = new FibTask(8); |
681 |
> |
f.complete(NoResult); |
682 |
> |
Integer r = f.invoke(); |
683 |
> |
assertSame(NoResult, r); |
684 |
> |
checkCompletedNormally(f, NoResult); |
685 |
> |
return r; |
686 |
> |
}}; |
687 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
688 |
|
} |
689 |
|
|
690 |
< |
/** |
690 |
> |
/** |
691 |
|
* invokeAll(t1, t2) invokes all task arguments |
692 |
|
*/ |
693 |
|
public void testInvokeAll2() { |
694 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
695 |
< |
public Integer compute() { |
696 |
< |
FibTask f = new FibTask(8); |
697 |
< |
FibTask g = new FibTask(9); |
698 |
< |
invokeAll(f, g); |
699 |
< |
threadAssertTrue(f.isDone()); |
700 |
< |
threadAssertTrue(f.join() == 21); |
701 |
< |
threadAssertTrue(g.isDone()); |
702 |
< |
threadAssertTrue(g.join() == 34); |
703 |
< |
return NoResult; |
681 |
< |
} |
682 |
< |
}; |
683 |
< |
mainPool.invoke(a); |
694 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
695 |
> |
public Integer realCompute() { |
696 |
> |
FibTask f = new FibTask(8); |
697 |
> |
FibTask g = new FibTask(9); |
698 |
> |
invokeAll(f, g); |
699 |
> |
checkCompletedNormally(f, 21); |
700 |
> |
checkCompletedNormally(g, 34); |
701 |
> |
return NoResult; |
702 |
> |
}}; |
703 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
704 |
|
} |
705 |
|
|
706 |
< |
/** |
706 |
> |
/** |
707 |
|
* invokeAll(tasks) with 1 argument invokes task |
708 |
|
*/ |
709 |
|
public void testInvokeAll1() { |
710 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
711 |
< |
public Integer compute() { |
712 |
< |
FibTask f = new FibTask(8); |
713 |
< |
invokeAll(f); |
714 |
< |
threadAssertTrue(f.isDone()); |
715 |
< |
threadAssertTrue(f.join() == 21); |
716 |
< |
return NoResult; |
717 |
< |
} |
698 |
< |
}; |
699 |
< |
mainPool.invoke(a); |
710 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
711 |
> |
public Integer realCompute() { |
712 |
> |
FibTask f = new FibTask(8); |
713 |
> |
invokeAll(f); |
714 |
> |
checkCompletedNormally(f, 21); |
715 |
> |
return NoResult; |
716 |
> |
}}; |
717 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
718 |
|
} |
719 |
|
|
720 |
< |
/** |
720 |
> |
/** |
721 |
|
* invokeAll(tasks) with > 2 argument invokes tasks |
722 |
|
*/ |
723 |
|
public void testInvokeAll3() { |
724 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
725 |
< |
public Integer compute() { |
726 |
< |
FibTask f = new FibTask(8); |
727 |
< |
FibTask g = new FibTask(9); |
728 |
< |
FibTask h = new FibTask(7); |
729 |
< |
invokeAll(f, g, h); |
730 |
< |
threadAssertTrue(f.isDone()); |
731 |
< |
threadAssertTrue(f.join() == 21); |
732 |
< |
threadAssertTrue(g.isDone()); |
733 |
< |
threadAssertTrue(g.join() == 34); |
734 |
< |
threadAssertTrue(h.isDone()); |
735 |
< |
threadAssertTrue(h.join() == 13); |
736 |
< |
return NoResult; |
737 |
< |
} |
738 |
< |
}; |
721 |
< |
mainPool.invoke(a); |
724 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
725 |
> |
public Integer realCompute() { |
726 |
> |
FibTask f = new FibTask(8); |
727 |
> |
FibTask g = new FibTask(9); |
728 |
> |
FibTask h = new FibTask(7); |
729 |
> |
invokeAll(f, g, h); |
730 |
> |
assertTrue(f.isDone()); |
731 |
> |
assertTrue(g.isDone()); |
732 |
> |
assertTrue(h.isDone()); |
733 |
> |
checkCompletedNormally(f, 21); |
734 |
> |
checkCompletedNormally(g, 34); |
735 |
> |
checkCompletedNormally(h, 13); |
736 |
> |
return NoResult; |
737 |
> |
}}; |
738 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
739 |
|
} |
740 |
|
|
741 |
< |
/** |
741 |
> |
/** |
742 |
|
* invokeAll(collection) invokes all tasks in the collection |
743 |
|
*/ |
744 |
|
public void testInvokeAllCollection() { |
745 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
746 |
< |
public Integer compute() { |
747 |
< |
FibTask f = new FibTask(8); |
748 |
< |
FibTask g = new FibTask(9); |
749 |
< |
FibTask h = new FibTask(7); |
750 |
< |
HashSet set = new HashSet(); |
751 |
< |
set.add(f); |
752 |
< |
set.add(g); |
753 |
< |
set.add(h); |
754 |
< |
invokeAll(set); |
755 |
< |
threadAssertTrue(f.isDone()); |
756 |
< |
threadAssertTrue(f.join() == 21); |
757 |
< |
threadAssertTrue(g.isDone()); |
758 |
< |
threadAssertTrue(g.join() == 34); |
759 |
< |
threadAssertTrue(h.isDone()); |
760 |
< |
threadAssertTrue(h.join() == 13); |
761 |
< |
return NoResult; |
762 |
< |
} |
763 |
< |
}; |
747 |
< |
mainPool.invoke(a); |
745 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
746 |
> |
public Integer realCompute() { |
747 |
> |
FibTask f = new FibTask(8); |
748 |
> |
FibTask g = new FibTask(9); |
749 |
> |
FibTask h = new FibTask(7); |
750 |
> |
HashSet set = new HashSet(); |
751 |
> |
set.add(f); |
752 |
> |
set.add(g); |
753 |
> |
set.add(h); |
754 |
> |
invokeAll(set); |
755 |
> |
assertTrue(f.isDone()); |
756 |
> |
assertTrue(g.isDone()); |
757 |
> |
assertTrue(h.isDone()); |
758 |
> |
checkCompletedNormally(f, 21); |
759 |
> |
checkCompletedNormally(g, 34); |
760 |
> |
checkCompletedNormally(h, 13); |
761 |
> |
return NoResult; |
762 |
> |
}}; |
763 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
764 |
|
} |
765 |
|
|
766 |
|
|
767 |
< |
/** |
767 |
> |
/** |
768 |
> |
* invokeAll(tasks) with any null task throws NPE |
769 |
> |
*/ |
770 |
> |
public void testInvokeAllNPE() { |
771 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
772 |
> |
public Integer realCompute() { |
773 |
> |
FibTask f = new FibTask(8); |
774 |
> |
FibTask g = new FibTask(9); |
775 |
> |
FibTask h = null; |
776 |
> |
try { |
777 |
> |
invokeAll(f, g, h); |
778 |
> |
shouldThrow(); |
779 |
> |
} catch (NullPointerException success) {} |
780 |
> |
return NoResult; |
781 |
> |
}}; |
782 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
783 |
> |
} |
784 |
> |
|
785 |
> |
/** |
786 |
|
* invokeAll(t1, t2) throw exception if any task does |
787 |
|
*/ |
788 |
|
public void testAbnormalInvokeAll2() { |
789 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
790 |
< |
public Integer compute() { |
791 |
< |
try { |
792 |
< |
FibTask f = new FibTask(8); |
793 |
< |
FailingFibTask g = new FailingFibTask(9); |
794 |
< |
invokeAll(f, g); |
795 |
< |
shouldThrow(); |
796 |
< |
return NoResult; |
797 |
< |
} catch(FJException success) { |
764 |
< |
} |
765 |
< |
return NoResult; |
789 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
790 |
> |
public Integer realCompute() { |
791 |
> |
FibTask f = new FibTask(8); |
792 |
> |
FailingFibTask g = new FailingFibTask(9); |
793 |
> |
try { |
794 |
> |
invokeAll(f, g); |
795 |
> |
shouldThrow(); |
796 |
> |
} catch (FJException success) { |
797 |
> |
checkCompletedAbnormally(g, success); |
798 |
|
} |
799 |
< |
}; |
800 |
< |
mainPool.invoke(a); |
799 |
> |
return NoResult; |
800 |
> |
}}; |
801 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
802 |
|
} |
803 |
|
|
804 |
< |
/** |
804 |
> |
/** |
805 |
|
* invokeAll(tasks) with 1 argument throws exception if task does |
806 |
|
*/ |
807 |
|
public void testAbnormalInvokeAll1() { |
808 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
809 |
< |
public Integer compute() { |
810 |
< |
try { |
811 |
< |
FailingFibTask g = new FailingFibTask(9); |
812 |
< |
invokeAll(g); |
813 |
< |
shouldThrow(); |
814 |
< |
return NoResult; |
815 |
< |
} catch(FJException success) { |
783 |
< |
} |
784 |
< |
return NoResult; |
808 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
809 |
> |
public Integer realCompute() { |
810 |
> |
FailingFibTask g = new FailingFibTask(9); |
811 |
> |
try { |
812 |
> |
invokeAll(g); |
813 |
> |
shouldThrow(); |
814 |
> |
} catch (FJException success) { |
815 |
> |
checkCompletedAbnormally(g, success); |
816 |
|
} |
817 |
< |
}; |
818 |
< |
mainPool.invoke(a); |
817 |
> |
return NoResult; |
818 |
> |
}}; |
819 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
820 |
|
} |
821 |
|
|
822 |
< |
/** |
822 |
> |
/** |
823 |
|
* invokeAll(tasks) with > 2 argument throws exception if any task does |
824 |
|
*/ |
825 |
|
public void testAbnormalInvokeAll3() { |
826 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
827 |
< |
public Integer compute() { |
828 |
< |
try { |
829 |
< |
FibTask f = new FibTask(8); |
830 |
< |
FailingFibTask g = new FailingFibTask(9); |
831 |
< |
FibTask h = new FibTask(7); |
832 |
< |
invokeAll(f, g, h); |
833 |
< |
shouldThrow(); |
834 |
< |
return NoResult; |
835 |
< |
} catch(FJException success) { |
804 |
< |
} |
805 |
< |
return NoResult; |
826 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
827 |
> |
public Integer realCompute() { |
828 |
> |
FibTask f = new FibTask(8); |
829 |
> |
FailingFibTask g = new FailingFibTask(9); |
830 |
> |
FibTask h = new FibTask(7); |
831 |
> |
try { |
832 |
> |
invokeAll(f, g, h); |
833 |
> |
shouldThrow(); |
834 |
> |
} catch (FJException success) { |
835 |
> |
checkCompletedAbnormally(g, success); |
836 |
|
} |
837 |
< |
}; |
838 |
< |
mainPool.invoke(a); |
837 |
> |
return NoResult; |
838 |
> |
}}; |
839 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
840 |
|
} |
841 |
|
|
842 |
< |
/** |
843 |
< |
* invokeAll(collection) throws exception if any task does |
842 |
> |
/** |
843 |
> |
* invokeAll(collection) throws exception if any task does |
844 |
|
*/ |
845 |
|
public void testAbnormalInvokeAllCollection() { |
846 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
847 |
< |
public Integer compute() { |
848 |
< |
try { |
849 |
< |
FailingFibTask f = new FailingFibTask(8); |
850 |
< |
FibTask g = new FibTask(9); |
851 |
< |
FibTask h = new FibTask(7); |
852 |
< |
HashSet set = new HashSet(); |
853 |
< |
set.add(f); |
854 |
< |
set.add(g); |
855 |
< |
set.add(h); |
856 |
< |
invokeAll(set); |
857 |
< |
shouldThrow(); |
858 |
< |
return NoResult; |
859 |
< |
} catch(FJException success) { |
829 |
< |
} |
830 |
< |
return NoResult; |
846 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
847 |
> |
public Integer realCompute() { |
848 |
> |
FailingFibTask f = new FailingFibTask(8); |
849 |
> |
FibTask g = new FibTask(9); |
850 |
> |
FibTask h = new FibTask(7); |
851 |
> |
HashSet set = new HashSet(); |
852 |
> |
set.add(f); |
853 |
> |
set.add(g); |
854 |
> |
set.add(h); |
855 |
> |
try { |
856 |
> |
invokeAll(set); |
857 |
> |
shouldThrow(); |
858 |
> |
} catch (FJException success) { |
859 |
> |
checkCompletedAbnormally(f, success); |
860 |
|
} |
861 |
< |
}; |
862 |
< |
mainPool.invoke(a); |
861 |
> |
return NoResult; |
862 |
> |
}}; |
863 |
> |
assertSame(NoResult, testInvokeOnPool(mainPool(), a)); |
864 |
|
} |
865 |
|
|
866 |
< |
/** |
866 |
> |
/** |
867 |
|
* tryUnfork returns true for most recent unexecuted task, |
868 |
|
* and suppresses execution |
869 |
|
*/ |
870 |
|
public void testTryUnfork() { |
871 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
872 |
< |
public Integer compute() { |
873 |
< |
FibTask g = new FibTask(9); |
874 |
< |
g.fork(); |
875 |
< |
FibTask f = new FibTask(8); |
876 |
< |
f.fork(); |
877 |
< |
threadAssertTrue(f.tryUnfork()); |
878 |
< |
helpQuiesce(); |
879 |
< |
threadAssertFalse(f.isDone()); |
880 |
< |
threadAssertTrue(g.isDone()); |
881 |
< |
return NoResult; |
882 |
< |
} |
883 |
< |
}; |
854 |
< |
singletonPool.invoke(a); |
871 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
872 |
> |
public Integer realCompute() { |
873 |
> |
FibTask g = new FibTask(9); |
874 |
> |
assertSame(g, g.fork()); |
875 |
> |
FibTask f = new FibTask(8); |
876 |
> |
assertSame(f, f.fork()); |
877 |
> |
assertTrue(f.tryUnfork()); |
878 |
> |
helpQuiesce(); |
879 |
> |
checkNotDone(f); |
880 |
> |
checkCompletedNormally(g, 34); |
881 |
> |
return NoResult; |
882 |
> |
}}; |
883 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
884 |
|
} |
885 |
|
|
886 |
< |
/** |
886 |
> |
/** |
887 |
|
* getSurplusQueuedTaskCount returns > 0 when |
888 |
|
* there are more tasks than threads |
889 |
|
*/ |
890 |
|
public void testGetSurplusQueuedTaskCount() { |
891 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
892 |
< |
public Integer compute() { |
893 |
< |
FibTask h = new FibTask(7); |
894 |
< |
h.fork(); |
895 |
< |
FibTask g = new FibTask(9); |
896 |
< |
g.fork(); |
897 |
< |
FibTask f = new FibTask(8); |
898 |
< |
f.fork(); |
899 |
< |
threadAssertTrue(getSurplusQueuedTaskCount() > 0); |
900 |
< |
helpQuiesce(); |
901 |
< |
return NoResult; |
902 |
< |
} |
903 |
< |
}; |
904 |
< |
singletonPool.invoke(a); |
891 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
892 |
> |
public Integer realCompute() { |
893 |
> |
FibTask h = new FibTask(7); |
894 |
> |
assertSame(h, h.fork()); |
895 |
> |
FibTask g = new FibTask(9); |
896 |
> |
assertSame(g, g.fork()); |
897 |
> |
FibTask f = new FibTask(8); |
898 |
> |
assertSame(f, f.fork()); |
899 |
> |
assertTrue(getSurplusQueuedTaskCount() > 0); |
900 |
> |
helpQuiesce(); |
901 |
> |
assertEquals(0, getSurplusQueuedTaskCount()); |
902 |
> |
checkCompletedNormally(f, 21); |
903 |
> |
checkCompletedNormally(g, 34); |
904 |
> |
checkCompletedNormally(h, 13); |
905 |
> |
return NoResult; |
906 |
> |
}}; |
907 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
908 |
|
} |
909 |
|
|
910 |
< |
/** |
910 |
> |
/** |
911 |
|
* peekNextLocalTask returns most recent unexecuted task. |
912 |
|
*/ |
913 |
|
public void testPeekNextLocalTask() { |
914 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
915 |
< |
public Integer compute() { |
916 |
< |
FibTask g = new FibTask(9); |
917 |
< |
g.fork(); |
918 |
< |
FibTask f = new FibTask(8); |
919 |
< |
f.fork(); |
920 |
< |
threadAssertTrue(peekNextLocalTask() == f); |
921 |
< |
f.join(); |
922 |
< |
threadAssertTrue(f.isDone()); |
923 |
< |
helpQuiesce(); |
924 |
< |
return NoResult; |
925 |
< |
} |
926 |
< |
}; |
895 |
< |
singletonPool.invoke(a); |
914 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
915 |
> |
public Integer realCompute() { |
916 |
> |
FibTask g = new FibTask(9); |
917 |
> |
assertSame(g, g.fork()); |
918 |
> |
FibTask f = new FibTask(8); |
919 |
> |
assertSame(f, f.fork()); |
920 |
> |
assertSame(f, peekNextLocalTask()); |
921 |
> |
checkCompletesNormally(f, 21); |
922 |
> |
helpQuiesce(); |
923 |
> |
checkCompletedNormally(g, 34); |
924 |
> |
return NoResult; |
925 |
> |
}}; |
926 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
927 |
|
} |
928 |
|
|
929 |
< |
/** |
929 |
> |
/** |
930 |
|
* pollNextLocalTask returns most recent unexecuted task |
931 |
|
* without executing it |
932 |
|
*/ |
933 |
|
public void testPollNextLocalTask() { |
934 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
935 |
< |
public Integer compute() { |
936 |
< |
FibTask g = new FibTask(9); |
937 |
< |
g.fork(); |
938 |
< |
FibTask f = new FibTask(8); |
939 |
< |
f.fork(); |
940 |
< |
threadAssertTrue(pollNextLocalTask() == f); |
941 |
< |
helpQuiesce(); |
942 |
< |
threadAssertFalse(f.isDone()); |
943 |
< |
return NoResult; |
944 |
< |
} |
945 |
< |
}; |
946 |
< |
singletonPool.invoke(a); |
934 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
935 |
> |
public Integer realCompute() { |
936 |
> |
FibTask g = new FibTask(9); |
937 |
> |
assertSame(g, g.fork()); |
938 |
> |
FibTask f = new FibTask(8); |
939 |
> |
assertSame(f, f.fork()); |
940 |
> |
assertSame(f, pollNextLocalTask()); |
941 |
> |
helpQuiesce(); |
942 |
> |
checkNotDone(f); |
943 |
> |
checkCompletedNormally(g, 34); |
944 |
> |
return NoResult; |
945 |
> |
}}; |
946 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
947 |
|
} |
948 |
|
|
949 |
< |
/** |
950 |
< |
* pollTask returns an unexecuted task |
920 |
< |
* without executing it |
949 |
> |
/** |
950 |
> |
* pollTask returns an unexecuted task without executing it |
951 |
|
*/ |
952 |
|
public void testPollTask() { |
953 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
954 |
< |
public Integer compute() { |
955 |
< |
FibTask g = new FibTask(9); |
956 |
< |
g.fork(); |
957 |
< |
FibTask f = new FibTask(8); |
958 |
< |
f.fork(); |
959 |
< |
threadAssertTrue(pollTask() == f); |
960 |
< |
helpQuiesce(); |
961 |
< |
threadAssertFalse(f.isDone()); |
962 |
< |
threadAssertTrue(g.isDone()); |
963 |
< |
return NoResult; |
964 |
< |
} |
965 |
< |
}; |
936 |
< |
singletonPool.invoke(a); |
953 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
954 |
> |
public Integer realCompute() { |
955 |
> |
FibTask g = new FibTask(9); |
956 |
> |
assertSame(g, g.fork()); |
957 |
> |
FibTask f = new FibTask(8); |
958 |
> |
assertSame(f, f.fork()); |
959 |
> |
assertSame(f, pollTask()); |
960 |
> |
helpQuiesce(); |
961 |
> |
checkNotDone(f); |
962 |
> |
checkCompletedNormally(g, 34); |
963 |
> |
return NoResult; |
964 |
> |
}}; |
965 |
> |
assertSame(NoResult, testInvokeOnPool(singletonPool(), a)); |
966 |
|
} |
967 |
|
|
968 |
< |
/** |
968 |
> |
/** |
969 |
|
* peekNextLocalTask returns least recent unexecuted task in async mode |
970 |
|
*/ |
971 |
|
public void testPeekNextLocalTaskAsync() { |
972 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
973 |
< |
public Integer compute() { |
974 |
< |
FibTask g = new FibTask(9); |
975 |
< |
g.fork(); |
976 |
< |
FibTask f = new FibTask(8); |
977 |
< |
f.fork(); |
978 |
< |
threadAssertTrue(peekNextLocalTask() == g); |
979 |
< |
f.join(); |
980 |
< |
helpQuiesce(); |
981 |
< |
threadAssertTrue(f.isDone()); |
982 |
< |
return NoResult; |
983 |
< |
} |
984 |
< |
}; |
985 |
< |
asyncSingletonPool.invoke(a); |
986 |
< |
} |
987 |
< |
|
988 |
< |
/** |
989 |
< |
* pollNextLocalTask returns least recent unexecuted task |
990 |
< |
* without executing it, in async mode |
972 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
973 |
> |
public Integer realCompute() { |
974 |
> |
FibTask g = new FibTask(9); |
975 |
> |
assertSame(g, g.fork()); |
976 |
> |
FibTask f = new FibTask(8); |
977 |
> |
assertSame(f, f.fork()); |
978 |
> |
assertSame(g, peekNextLocalTask()); |
979 |
> |
assertEquals(21, (int) f.join()); |
980 |
> |
helpQuiesce(); |
981 |
> |
checkCompletedNormally(f, 21); |
982 |
> |
checkCompletedNormally(g, 34); |
983 |
> |
return NoResult; |
984 |
> |
}}; |
985 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
986 |
> |
} |
987 |
> |
|
988 |
> |
/** |
989 |
> |
* pollNextLocalTask returns least recent unexecuted task without |
990 |
> |
* executing it, in async mode |
991 |
|
*/ |
992 |
|
public void testPollNextLocalTaskAsync() { |
993 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
994 |
< |
public Integer compute() { |
995 |
< |
FibTask g = new FibTask(9); |
996 |
< |
g.fork(); |
997 |
< |
FibTask f = new FibTask(8); |
998 |
< |
f.fork(); |
999 |
< |
threadAssertTrue(pollNextLocalTask() == g); |
1000 |
< |
helpQuiesce(); |
1001 |
< |
threadAssertTrue(f.isDone()); |
1002 |
< |
threadAssertFalse(g.isDone()); |
1003 |
< |
return NoResult; |
1004 |
< |
} |
1005 |
< |
}; |
1006 |
< |
asyncSingletonPool.invoke(a); |
1007 |
< |
} |
1008 |
< |
|
1009 |
< |
/** |
1010 |
< |
* pollTask returns an unexecuted task |
982 |
< |
* without executing it, in async mode |
993 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
994 |
> |
public Integer realCompute() { |
995 |
> |
FibTask g = new FibTask(9); |
996 |
> |
assertSame(g, g.fork()); |
997 |
> |
FibTask f = new FibTask(8); |
998 |
> |
assertSame(f, f.fork()); |
999 |
> |
assertSame(g, pollNextLocalTask()); |
1000 |
> |
helpQuiesce(); |
1001 |
> |
checkCompletedNormally(f, 21); |
1002 |
> |
checkNotDone(g); |
1003 |
> |
return NoResult; |
1004 |
> |
}}; |
1005 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1006 |
> |
} |
1007 |
> |
|
1008 |
> |
/** |
1009 |
> |
* pollTask returns an unexecuted task without executing it, in |
1010 |
> |
* async mode |
1011 |
|
*/ |
1012 |
|
public void testPollTaskAsync() { |
1013 |
< |
RecursiveTask<Integer> a = new RecursiveTask<Integer>() { |
1014 |
< |
public Integer compute() { |
1015 |
< |
FibTask g = new FibTask(9); |
1016 |
< |
g.fork(); |
1017 |
< |
FibTask f = new FibTask(8); |
1018 |
< |
f.fork(); |
1019 |
< |
threadAssertTrue(pollTask() == g); |
1020 |
< |
helpQuiesce(); |
1021 |
< |
threadAssertTrue(f.isDone()); |
1022 |
< |
threadAssertFalse(g.isDone()); |
1023 |
< |
return NoResult; |
1024 |
< |
} |
1025 |
< |
}; |
998 |
< |
asyncSingletonPool.invoke(a); |
1013 |
> |
RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() { |
1014 |
> |
public Integer realCompute() { |
1015 |
> |
FibTask g = new FibTask(9); |
1016 |
> |
assertSame(g, g.fork()); |
1017 |
> |
FibTask f = new FibTask(8); |
1018 |
> |
assertSame(f, f.fork()); |
1019 |
> |
assertSame(g, pollTask()); |
1020 |
> |
helpQuiesce(); |
1021 |
> |
checkCompletedNormally(f, 21); |
1022 |
> |
checkNotDone(g); |
1023 |
> |
return NoResult; |
1024 |
> |
}}; |
1025 |
> |
assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a)); |
1026 |
|
} |
1027 |
|
|
1028 |
|
} |