22 |
|
return new TestSuite(ExecutorsTest.class); |
23 |
|
} |
24 |
|
|
25 |
– |
static class TimedCallable<T> implements Callable<T> { |
26 |
– |
private final ExecutorService exec; |
27 |
– |
private final Callable<T> func; |
28 |
– |
private final long msecs; |
29 |
– |
|
30 |
– |
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
31 |
– |
this.exec = exec; |
32 |
– |
this.func = func; |
33 |
– |
this.msecs = msecs; |
34 |
– |
} |
35 |
– |
|
36 |
– |
public T call() throws Exception { |
37 |
– |
Future<T> ftask = exec.submit(func); |
38 |
– |
try { |
39 |
– |
return ftask.get(msecs, MILLISECONDS); |
40 |
– |
} finally { |
41 |
– |
ftask.cancel(true); |
42 |
– |
} |
43 |
– |
} |
44 |
– |
} |
45 |
– |
|
46 |
– |
|
47 |
– |
private static class Fib implements Callable<BigInteger> { |
48 |
– |
private final BigInteger n; |
49 |
– |
Fib(long n) { |
50 |
– |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
51 |
– |
this.n = BigInteger.valueOf(n); |
52 |
– |
} |
53 |
– |
public BigInteger call() { |
54 |
– |
BigInteger f1 = BigInteger.ONE; |
55 |
– |
BigInteger f2 = f1; |
56 |
– |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
57 |
– |
BigInteger t = f1.add(f2); |
58 |
– |
f1 = f2; |
59 |
– |
f2 = t; |
60 |
– |
} |
61 |
– |
return f1; |
62 |
– |
} |
63 |
– |
}; |
64 |
– |
|
25 |
|
/** |
26 |
|
* A newCachedThreadPool can execute runnables |
27 |
|
*/ |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* timeouts from execute will time out if they compute too long. |
223 |
> |
* Future.get on submitted tasks will time out if they compute too long. |
224 |
|
*/ |
225 |
|
public void testTimedCallable() throws Exception { |
226 |
< |
int N = 10000; |
227 |
< |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
228 |
< |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
229 |
< |
try { |
230 |
< |
long startTime = System.currentTimeMillis(); |
231 |
< |
|
232 |
< |
long i = 0; |
233 |
< |
while (tasks.size() < N) { |
234 |
< |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
235 |
< |
i += 10; |
236 |
< |
} |
237 |
< |
|
238 |
< |
int iters = 0; |
239 |
< |
BigInteger sum = BigInteger.ZERO; |
280 |
< |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
226 |
> |
final Runnable sleeper = |
227 |
> |
new RunnableShouldThrow(InterruptedException.class) { |
228 |
> |
public void realRun() throws InterruptedException { |
229 |
> |
Thread.sleep(LONG_DELAY_MS); |
230 |
> |
}}; |
231 |
> |
for (ExecutorService executor : |
232 |
> |
new ExecutorService[] { |
233 |
> |
Executors.newSingleThreadExecutor(), |
234 |
> |
Executors.newCachedThreadPool(), |
235 |
> |
Executors.newFixedThreadPool(2), |
236 |
> |
Executors.newScheduledThreadPool(2), |
237 |
> |
}) { |
238 |
> |
try { |
239 |
> |
Future future = executor.submit(sleeper); |
240 |
|
try { |
241 |
< |
++iters; |
242 |
< |
sum = sum.add(it.next().call()); |
243 |
< |
} |
244 |
< |
catch (TimeoutException success) { |
245 |
< |
assertTrue(iters > 0); |
287 |
< |
return; |
241 |
> |
future.get(SHORT_DELAY_MS, MILLISECONDS); |
242 |
> |
shouldThrow(); |
243 |
> |
} catch (TimeoutException success) { |
244 |
> |
} finally { |
245 |
> |
future.cancel(true); |
246 |
|
} |
247 |
|
} |
248 |
< |
// if by chance we didn't ever time out, total time must be small |
249 |
< |
long elapsed = System.currentTimeMillis() - startTime; |
250 |
< |
assertTrue(elapsed < N); |
293 |
< |
} |
294 |
< |
finally { |
295 |
< |
joinPool(executor); |
248 |
> |
finally { |
249 |
> |
joinPool(executor); |
250 |
> |
} |
251 |
|
} |
252 |
|
} |
253 |
|
|
345 |
|
} finally { |
346 |
|
joinPool(e); |
347 |
|
} |
393 |
– |
|
348 |
|
} |
349 |
|
|
350 |
|
void checkCCL() { |
493 |
|
*/ |
494 |
|
public void testCallable2() throws Exception { |
495 |
|
Callable c = Executors.callable(new NoOpRunnable(), one); |
496 |
< |
assertEquals(one, c.call()); |
496 |
> |
assertSame(one, c.call()); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
502 |
|
public void testCallable3() throws Exception { |
503 |
|
Callable c = Executors.callable(new PrivilegedAction() { |
504 |
|
public Object run() { return one; }}); |
505 |
< |
assertEquals(one, c.call()); |
505 |
> |
assertSame(one, c.call()); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
511 |
|
public void testCallable4() throws Exception { |
512 |
|
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
513 |
|
public Object run() { return one; }}); |
514 |
< |
assertEquals(one, c.call()); |
514 |
> |
assertSame(one, c.call()); |
515 |
|
} |
516 |
|
|
517 |
|
|