10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.math.BigInteger; |
13 |
+ |
import java.security.*; |
14 |
|
|
15 |
|
public class ExecutorsTest extends JSR166TestCase{ |
15 |
– |
|
16 |
|
public static void main(String[] args) { |
17 |
|
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
– |
|
20 |
– |
|
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ExecutorsTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
private static final String TEST_STRING = "a test string"; |
24 |
+ |
|
25 |
+ |
private static class StringTask implements Callable<String> { |
26 |
+ |
public String call() { return TEST_STRING; } |
27 |
+ |
} |
28 |
+ |
|
29 |
+ |
static class DirectExecutor implements Executor { |
30 |
+ |
public void execute(Runnable r) { |
31 |
+ |
r.run(); |
32 |
+ |
} |
33 |
+ |
} |
34 |
+ |
|
35 |
+ |
static class TimedCallable<T> implements Callable<T> { |
36 |
+ |
private final Executor exec; |
37 |
+ |
private final Callable<T> func; |
38 |
+ |
private final long msecs; |
39 |
+ |
|
40 |
+ |
TimedCallable(Executor exec, Callable<T> func, long msecs) { |
41 |
+ |
this.exec = exec; |
42 |
+ |
this.func = func; |
43 |
+ |
this.msecs = msecs; |
44 |
+ |
} |
45 |
+ |
|
46 |
+ |
public T call() throws Exception { |
47 |
+ |
Future<T> ftask = Executors.execute(exec, func); |
48 |
+ |
try { |
49 |
+ |
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
50 |
+ |
} finally { |
51 |
+ |
ftask.cancel(true); |
52 |
+ |
} |
53 |
+ |
} |
54 |
+ |
} |
55 |
+ |
|
56 |
+ |
|
57 |
+ |
private static class Fib implements Callable<BigInteger> { |
58 |
+ |
private final BigInteger n; |
59 |
+ |
Fib(long n) { |
60 |
+ |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
61 |
+ |
this.n = BigInteger.valueOf(n); |
62 |
+ |
} |
63 |
+ |
public BigInteger call() { |
64 |
+ |
BigInteger f1 = BigInteger.ONE; |
65 |
+ |
BigInteger f2 = f1; |
66 |
+ |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
67 |
+ |
BigInteger t = f1.add(f2); |
68 |
+ |
f1 = f2; |
69 |
+ |
f2 = t; |
70 |
+ |
} |
71 |
+ |
return f1; |
72 |
+ |
} |
73 |
+ |
}; |
74 |
+ |
|
75 |
+ |
/** |
76 |
+ |
* A newCachedThreadPool can execute runnables |
77 |
+ |
*/ |
78 |
+ |
public void testNewCachedThreadPool1() { |
79 |
+ |
ExecutorService e = Executors.newCachedThreadPool(); |
80 |
+ |
e.execute(new NoOpRunnable()); |
81 |
+ |
e.execute(new NoOpRunnable()); |
82 |
+ |
e.execute(new NoOpRunnable()); |
83 |
+ |
e.shutdown(); |
84 |
+ |
} |
85 |
+ |
|
86 |
+ |
/** |
87 |
+ |
* A newCachedThreadPool with given ThreadFactory can execute runnables |
88 |
+ |
*/ |
89 |
+ |
public void testNewCachedThreadPool2() { |
90 |
+ |
ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); |
91 |
+ |
e.execute(new NoOpRunnable()); |
92 |
+ |
e.execute(new NoOpRunnable()); |
93 |
+ |
e.execute(new NoOpRunnable()); |
94 |
+ |
e.shutdown(); |
95 |
+ |
} |
96 |
+ |
|
97 |
+ |
/** |
98 |
+ |
* A newCachedThreadPool with null ThreadFactory throws NPE |
99 |
+ |
*/ |
100 |
+ |
public void testNewCachedThreadPool3() { |
101 |
+ |
try { |
102 |
+ |
ExecutorService e = Executors.newCachedThreadPool(null); |
103 |
+ |
shouldThrow(); |
104 |
+ |
} |
105 |
+ |
catch(NullPointerException success) { |
106 |
+ |
} |
107 |
+ |
} |
108 |
+ |
|
109 |
+ |
|
110 |
+ |
/** |
111 |
+ |
* A new SingleThreadExecutor can execute runnables |
112 |
+ |
*/ |
113 |
+ |
public void testNewSingleThreadExecutor1() { |
114 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(); |
115 |
+ |
e.execute(new NoOpRunnable()); |
116 |
+ |
e.execute(new NoOpRunnable()); |
117 |
+ |
e.execute(new NoOpRunnable()); |
118 |
+ |
e.shutdown(); |
119 |
+ |
} |
120 |
+ |
|
121 |
+ |
/** |
122 |
+ |
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
123 |
+ |
*/ |
124 |
+ |
public void testNewSingleThreadExecutor2() { |
125 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); |
126 |
+ |
e.execute(new NoOpRunnable()); |
127 |
+ |
e.execute(new NoOpRunnable()); |
128 |
+ |
e.execute(new NoOpRunnable()); |
129 |
+ |
e.shutdown(); |
130 |
+ |
} |
131 |
+ |
|
132 |
+ |
/** |
133 |
+ |
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
134 |
+ |
*/ |
135 |
+ |
public void testNewSingleThreadExecutor3() { |
136 |
+ |
try { |
137 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(null); |
138 |
+ |
shouldThrow(); |
139 |
+ |
} |
140 |
+ |
catch(NullPointerException success) { |
141 |
+ |
} |
142 |
+ |
} |
143 |
+ |
|
144 |
+ |
/** |
145 |
+ |
* A new newFixedThreadPool can execute runnables |
146 |
+ |
*/ |
147 |
+ |
public void testNewFixedThreadPool1() { |
148 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2); |
149 |
+ |
e.execute(new NoOpRunnable()); |
150 |
+ |
e.execute(new NoOpRunnable()); |
151 |
+ |
e.execute(new NoOpRunnable()); |
152 |
+ |
e.shutdown(); |
153 |
+ |
} |
154 |
+ |
|
155 |
+ |
/** |
156 |
+ |
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
157 |
+ |
*/ |
158 |
+ |
public void testNewFixedThreadPool2() { |
159 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); |
160 |
+ |
e.execute(new NoOpRunnable()); |
161 |
+ |
e.execute(new NoOpRunnable()); |
162 |
+ |
e.execute(new NoOpRunnable()); |
163 |
+ |
e.shutdown(); |
164 |
+ |
} |
165 |
+ |
|
166 |
+ |
/** |
167 |
+ |
* A new newFixedThreadPool with null ThreadFactory throws NPE |
168 |
+ |
*/ |
169 |
+ |
public void testNewFixedThreadPool3() { |
170 |
+ |
try { |
171 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, null); |
172 |
+ |
shouldThrow(); |
173 |
+ |
} |
174 |
+ |
catch(NullPointerException success) { |
175 |
+ |
} |
176 |
+ |
} |
177 |
+ |
|
178 |
+ |
/** |
179 |
+ |
* A new newFixedThreadPool with 0 threads throws IAE |
180 |
+ |
*/ |
181 |
+ |
public void testNewFixedThreadPool4() { |
182 |
+ |
try { |
183 |
+ |
ExecutorService e = Executors.newFixedThreadPool(0); |
184 |
+ |
shouldThrow(); |
185 |
+ |
} |
186 |
+ |
catch(IllegalArgumentException success) { |
187 |
+ |
} |
188 |
+ |
} |
189 |
+ |
|
190 |
+ |
/** |
191 |
+ |
* execute of runnable runs it to completion |
192 |
+ |
*/ |
193 |
+ |
public void testExecuteRunnable() { |
194 |
+ |
try { |
195 |
+ |
Executor e = new DirectExecutor(); |
196 |
+ |
TrackedShortRunnable task = new TrackedShortRunnable(); |
197 |
+ |
assertFalse(task.done); |
198 |
+ |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
199 |
+ |
String result = future.get(); |
200 |
+ |
assertTrue(task.done); |
201 |
+ |
assertSame(TEST_STRING, result); |
202 |
+ |
} |
203 |
+ |
catch (ExecutionException ex) { |
204 |
+ |
unexpectedException(); |
205 |
+ |
} |
206 |
+ |
catch (InterruptedException ex) { |
207 |
+ |
unexpectedException(); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
/** |
212 |
+ |
* invoke of a runnable runs it to completion |
213 |
+ |
*/ |
214 |
+ |
public void testInvokeRunnable() { |
215 |
+ |
try { |
216 |
+ |
Executor e = new DirectExecutor(); |
217 |
+ |
TrackedShortRunnable task = new TrackedShortRunnable(); |
218 |
+ |
assertFalse(task.done); |
219 |
+ |
Executors.invoke(e, task); |
220 |
+ |
assertTrue(task.done); |
221 |
+ |
} |
222 |
+ |
catch (ExecutionException ex) { |
223 |
+ |
unexpectedException(); |
224 |
+ |
} |
225 |
+ |
catch (InterruptedException ex) { |
226 |
+ |
unexpectedException(); |
227 |
+ |
} |
228 |
+ |
} |
229 |
+ |
|
230 |
+ |
/** |
231 |
+ |
* execute of a callable runs it to completion |
232 |
+ |
*/ |
233 |
+ |
public void testExecuteCallable() { |
234 |
+ |
try { |
235 |
+ |
Executor e = new DirectExecutor(); |
236 |
+ |
Future<String> future = Executors.execute(e, new StringTask()); |
237 |
+ |
String result = future.get(); |
238 |
+ |
assertSame(TEST_STRING, result); |
239 |
+ |
} |
240 |
+ |
catch (ExecutionException ex) { |
241 |
+ |
unexpectedException(); |
242 |
+ |
} |
243 |
+ |
catch (InterruptedException ex) { |
244 |
+ |
unexpectedException(); |
245 |
+ |
} |
246 |
+ |
} |
247 |
+ |
|
248 |
+ |
|
249 |
+ |
/** |
250 |
+ |
* execute of a privileged action runs it to completion |
251 |
+ |
*/ |
252 |
+ |
public void testExecutePrivilegedAction() { |
253 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
254 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
255 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
256 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
257 |
+ |
Policy.setPolicy(policy); |
258 |
+ |
try { |
259 |
+ |
Executor e = new DirectExecutor(); |
260 |
+ |
Future future = Executors.execute(e, new PrivilegedAction() { |
261 |
+ |
public Object run() { |
262 |
+ |
return TEST_STRING; |
263 |
+ |
}}); |
264 |
+ |
|
265 |
+ |
Object result = future.get(); |
266 |
+ |
assertSame(TEST_STRING, result); |
267 |
+ |
} |
268 |
+ |
catch (ExecutionException ex) { |
269 |
+ |
unexpectedException(); |
270 |
+ |
} |
271 |
+ |
catch (InterruptedException ex) { |
272 |
+ |
unexpectedException(); |
273 |
+ |
} |
274 |
+ |
finally { |
275 |
+ |
Policy.setPolicy(savedPolicy); |
276 |
+ |
} |
277 |
+ |
} |
278 |
+ |
|
279 |
+ |
/** |
280 |
+ |
* execute of a privileged exception action runs it to completion |
281 |
+ |
*/ |
282 |
+ |
public void testExecutePrivilegedExceptionAction() { |
283 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
284 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
285 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
286 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
287 |
+ |
Policy.setPolicy(policy); |
288 |
+ |
try { |
289 |
+ |
Executor e = new DirectExecutor(); |
290 |
+ |
Future future = Executors.execute(e, new PrivilegedExceptionAction() { |
291 |
+ |
public Object run() { |
292 |
+ |
return TEST_STRING; |
293 |
+ |
}}); |
294 |
+ |
|
295 |
+ |
Object result = future.get(); |
296 |
+ |
assertSame(TEST_STRING, result); |
297 |
+ |
} |
298 |
+ |
catch (ExecutionException ex) { |
299 |
+ |
unexpectedException(); |
300 |
+ |
} |
301 |
+ |
catch (InterruptedException ex) { |
302 |
+ |
unexpectedException(); |
303 |
+ |
} |
304 |
+ |
finally { |
305 |
+ |
Policy.setPolicy(savedPolicy); |
306 |
+ |
} |
307 |
+ |
} |
308 |
+ |
|
309 |
+ |
/** |
310 |
+ |
* execute of a failed privileged exception action reports exception |
311 |
+ |
*/ |
312 |
+ |
public void testExecuteFailedPrivilegedExceptionAction() { |
313 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
314 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
315 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
316 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
317 |
+ |
Policy.setPolicy(policy); |
318 |
+ |
try { |
319 |
+ |
Executor e = new DirectExecutor(); |
320 |
+ |
Future future = Executors.execute(e, new PrivilegedExceptionAction() { |
321 |
+ |
public Object run() throws Exception { |
322 |
+ |
throw new IndexOutOfBoundsException(); |
323 |
+ |
}}); |
324 |
+ |
|
325 |
+ |
Object result = future.get(); |
326 |
+ |
shouldThrow(); |
327 |
+ |
} |
328 |
+ |
catch (ExecutionException success) { |
329 |
+ |
} |
330 |
+ |
catch (InterruptedException ex) { |
331 |
+ |
unexpectedException(); |
332 |
+ |
} |
333 |
+ |
finally { |
334 |
+ |
Policy.setPolicy(savedPolicy); |
335 |
+ |
} |
336 |
+ |
} |
337 |
+ |
|
338 |
+ |
/** |
339 |
+ |
* invoke of a collable runs it to completion |
340 |
+ |
*/ |
341 |
+ |
public void testInvokeCallable() { |
342 |
+ |
try { |
343 |
+ |
Executor e = new DirectExecutor(); |
344 |
+ |
String result = Executors.invoke(e, new StringTask()); |
345 |
+ |
|
346 |
+ |
assertSame(TEST_STRING, result); |
347 |
+ |
} |
348 |
+ |
catch (ExecutionException ex) { |
349 |
+ |
unexpectedException(); |
350 |
+ |
} |
351 |
+ |
catch (InterruptedException ex) { |
352 |
+ |
unexpectedException(); |
353 |
+ |
} |
354 |
+ |
} |
355 |
+ |
|
356 |
+ |
/** |
357 |
+ |
* execute with null executor throws NPE |
358 |
+ |
*/ |
359 |
+ |
public void testNullExecuteRunnable() { |
360 |
+ |
try { |
361 |
+ |
TrackedShortRunnable task = new TrackedShortRunnable(); |
362 |
+ |
assertFalse(task.done); |
363 |
+ |
Future<String> future = Executors.execute(null, task, TEST_STRING); |
364 |
+ |
shouldThrow(); |
365 |
+ |
} |
366 |
+ |
catch (NullPointerException success) { |
367 |
+ |
} |
368 |
+ |
catch (Exception ex) { |
369 |
+ |
unexpectedException(); |
370 |
+ |
} |
371 |
+ |
} |
372 |
+ |
|
373 |
+ |
/** |
374 |
+ |
* execute with a null runnable throws NPE |
375 |
+ |
*/ |
376 |
+ |
public void testExecuteNullRunnable() { |
377 |
+ |
try { |
378 |
+ |
Executor e = new DirectExecutor(); |
379 |
+ |
TrackedShortRunnable task = null; |
380 |
+ |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
381 |
+ |
shouldThrow(); |
382 |
+ |
} |
383 |
+ |
catch (NullPointerException success) { |
384 |
+ |
} |
385 |
+ |
catch (Exception ex) { |
386 |
+ |
unexpectedException(); |
387 |
+ |
} |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
/** |
391 |
+ |
* invoke of a null runnable throws NPE |
392 |
+ |
*/ |
393 |
+ |
public void testInvokeNullRunnable() { |
394 |
+ |
try { |
395 |
+ |
Executor e = new DirectExecutor(); |
396 |
+ |
TrackedShortRunnable task = null; |
397 |
+ |
Executors.invoke(e, task); |
398 |
+ |
shouldThrow(); |
399 |
+ |
} |
400 |
+ |
catch (NullPointerException success) { |
401 |
+ |
} |
402 |
+ |
catch (Exception ex) { |
403 |
+ |
unexpectedException(); |
404 |
+ |
} |
405 |
+ |
} |
406 |
+ |
|
407 |
|
/** |
408 |
< |
* execute(Executor, Runnable) will throw |
27 |
< |
* RejectedExecutionException Attempting to execute a runnable on |
28 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
29 |
< |
* runnables are attempted on a pool capable on handling one |
30 |
< |
* until it throws an exception |
408 |
> |
* execute of a null callable throws NPE |
409 |
|
*/ |
410 |
< |
public void testExecute1(){ |
410 |
> |
public void testExecuteNullCallable() { |
411 |
> |
try { |
412 |
> |
Executor e = new DirectExecutor(); |
413 |
> |
StringTask t = null; |
414 |
> |
Future<String> future = Executors.execute(e, t); |
415 |
> |
shouldThrow(); |
416 |
> |
} |
417 |
> |
catch (NullPointerException success) { |
418 |
> |
} |
419 |
> |
catch (Exception ex) { |
420 |
> |
unexpectedException(); |
421 |
> |
} |
422 |
> |
} |
423 |
> |
|
424 |
> |
/** |
425 |
> |
* invoke of a null callable throws NPE |
426 |
> |
*/ |
427 |
> |
public void testInvokeNullCallable() { |
428 |
> |
try { |
429 |
> |
Executor e = new DirectExecutor(); |
430 |
> |
StringTask t = null; |
431 |
> |
String result = Executors.invoke(e, t); |
432 |
> |
shouldThrow(); |
433 |
> |
} |
434 |
> |
catch (NullPointerException success) { |
435 |
> |
} |
436 |
> |
catch (Exception ex) { |
437 |
> |
unexpectedException(); |
438 |
> |
} |
439 |
> |
} |
440 |
> |
|
441 |
> |
/** |
442 |
> |
* execute(Executor, Runnable) throws RejectedExecutionException |
443 |
> |
* if saturated. |
444 |
> |
*/ |
445 |
> |
public void testExecute1() { |
446 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
447 |
< |
try{ |
447 |
> |
try { |
448 |
|
|
449 |
|
for(int i = 0; i < 5; ++i){ |
450 |
|
Executors.execute(p, new MediumRunnable(), Boolean.TRUE); |
451 |
|
} |
452 |
< |
fail("should throw"); |
452 |
> |
shouldThrow(); |
453 |
|
} catch(RejectedExecutionException success){} |
454 |
|
joinPool(p); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
458 |
< |
* execute(Executor, Callable) will throw |
459 |
< |
* RejectedExecutionException Attempting to execute a callable on |
47 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
48 |
< |
* runnables are attempted on a pool capable on handling one |
49 |
< |
* until it throws an exception |
458 |
> |
* execute(Executor, Callable)throws RejectedExecutionException |
459 |
> |
* if saturated. |
460 |
|
*/ |
461 |
< |
public void testExecute2(){ |
461 |
> |
public void testExecute2() { |
462 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
463 |
< |
try{ |
463 |
> |
try { |
464 |
|
for(int i = 0; i < 5; ++i) { |
465 |
|
Executors.execute(p, new SmallCallable()); |
466 |
|
} |
467 |
< |
fail("should throw"); |
468 |
< |
}catch(RejectedExecutionException e){} |
467 |
> |
shouldThrow(); |
468 |
> |
} catch(RejectedExecutionException e){} |
469 |
|
joinPool(p); |
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
|
/** |
474 |
< |
* invoke(Executor, Runnable) throws InterruptedException |
475 |
< |
* A single use of invoke starts that will wait long enough |
66 |
< |
* for the invoking thread to be interrupted |
474 |
> |
* invoke(Executor, Runnable) throws InterruptedException if |
475 |
> |
* caller interrupted. |
476 |
|
*/ |
477 |
< |
public void testInvoke2(){ |
477 |
> |
public void testInterruptedInvoke() { |
478 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
479 |
|
Thread t = new Thread(new Runnable() { |
480 |
< |
public void run(){ |
481 |
< |
try{ |
482 |
< |
Executors.invoke(p,new Runnable(){ |
483 |
< |
public void run(){ |
484 |
< |
try{ |
480 |
> |
public void run() { |
481 |
> |
try { |
482 |
> |
Executors.invoke(p,new Runnable() { |
483 |
> |
public void run() { |
484 |
> |
try { |
485 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
486 |
< |
fail("should throw"); |
487 |
< |
}catch(InterruptedException e){ |
486 |
> |
shouldThrow(); |
487 |
> |
} catch(InterruptedException e){ |
488 |
|
} |
489 |
|
} |
490 |
|
}); |
491 |
|
} catch(InterruptedException success){ |
492 |
|
} catch(Exception e) { |
493 |
< |
fail("unexpected exception"); |
493 |
> |
unexpectedException(); |
494 |
|
} |
495 |
|
|
496 |
|
} |
497 |
|
}); |
498 |
< |
try{ |
498 |
> |
try { |
499 |
|
t.start(); |
500 |
|
Thread.sleep(SHORT_DELAY_MS); |
501 |
|
t.interrupt(); |
502 |
< |
}catch(Exception e){ |
503 |
< |
fail("unexpected exception"); |
502 |
> |
} catch(Exception e){ |
503 |
> |
unexpectedException(); |
504 |
|
} |
505 |
|
joinPool(p); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* invoke(Executor, Runnable) will throw |
510 |
< |
* ExecutionException An ExecutionException occurs when the |
102 |
< |
* underlying Runnable throws an exception, here the |
103 |
< |
* DivideByZeroException will cause an ExecutionException |
509 |
> |
* invoke(Executor, Runnable) throws ExecutionException if |
510 |
> |
* runnable throws exception. |
511 |
|
*/ |
512 |
< |
public void testInvoke3(){ |
512 |
> |
public void testInvoke3() { |
513 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
514 |
< |
try{ |
515 |
< |
Runnable r = new Runnable(){ |
516 |
< |
public void run(){ |
514 |
> |
try { |
515 |
> |
Runnable r = new Runnable() { |
516 |
> |
public void run() { |
517 |
|
int i = 5/0; |
518 |
|
} |
519 |
|
}; |
522 |
|
Executors.invoke(p,r); |
523 |
|
} |
524 |
|
|
525 |
< |
fail("should throw"); |
525 |
> |
shouldThrow(); |
526 |
|
} catch(ExecutionException success){ |
527 |
|
} catch(Exception e){ |
528 |
< |
fail("should throw EE"); |
528 |
> |
unexpectedException(); |
529 |
|
} |
530 |
|
joinPool(p); |
531 |
|
} |
533 |
|
|
534 |
|
|
535 |
|
/** |
536 |
< |
* invoke(Executor, Callable) throws |
537 |
< |
* InterruptedException A single use of invoke starts that will |
131 |
< |
* wait long enough for the invoking thread to be interrupted |
536 |
> |
* invoke(Executor, Callable) throws InterruptedException if |
537 |
> |
* callable throws exception |
538 |
|
*/ |
539 |
< |
public void testInvoke5(){ |
539 |
> |
public void testInvoke5() { |
540 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
541 |
|
|
542 |
< |
final Callable c = new Callable(){ |
543 |
< |
public Object call(){ |
544 |
< |
try{ |
542 |
> |
final Callable c = new Callable() { |
543 |
> |
public Object call() { |
544 |
> |
try { |
545 |
|
Executors.invoke(p, new SmallCallable()); |
546 |
< |
fail("should throw"); |
547 |
< |
}catch(InterruptedException e){} |
546 |
> |
shouldThrow(); |
547 |
> |
} catch(InterruptedException e){} |
548 |
|
catch(RejectedExecutionException e2){} |
549 |
|
catch(ExecutionException e3){} |
550 |
|
return Boolean.TRUE; |
553 |
|
|
554 |
|
|
555 |
|
|
556 |
< |
Thread t = new Thread(new Runnable(){ |
557 |
< |
public void run(){ |
558 |
< |
try{ |
556 |
> |
Thread t = new Thread(new Runnable() { |
557 |
> |
public void run() { |
558 |
> |
try { |
559 |
|
c.call(); |
560 |
< |
}catch(Exception e){} |
560 |
> |
} catch(Exception e){} |
561 |
|
} |
562 |
|
}); |
563 |
< |
try{ |
563 |
> |
try { |
564 |
|
t.start(); |
565 |
|
Thread.sleep(SHORT_DELAY_MS); |
566 |
|
t.interrupt(); |
567 |
|
t.join(); |
568 |
< |
}catch(InterruptedException e){ |
569 |
< |
fail("unexpected exception"); |
568 |
> |
} catch(InterruptedException e){ |
569 |
> |
unexpectedException(); |
570 |
|
} |
571 |
|
|
572 |
|
joinPool(p); |
573 |
|
} |
574 |
|
|
575 |
|
/** |
576 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
577 |
< |
* An ExecutionException occurs when the underlying Runnable throws |
172 |
< |
* an exception, here the DivideByZeroException will cause an ExecutionException |
576 |
> |
* invoke(Executor, Callable) will throw ExecutionException |
577 |
> |
* if callable throws exception |
578 |
|
*/ |
579 |
< |
public void testInvoke6(){ |
579 |
> |
public void testInvoke6() { |
580 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
581 |
|
|
582 |
< |
try{ |
583 |
< |
Callable c = new Callable(){ |
584 |
< |
public Object call(){ |
582 |
> |
try { |
583 |
> |
Callable c = new Callable() { |
584 |
> |
public Object call() { |
585 |
|
int i = 5/0; |
586 |
|
return Boolean.TRUE; |
587 |
|
} |
591 |
|
Executors.invoke(p,c); |
592 |
|
} |
593 |
|
|
594 |
< |
fail("should throw"); |
595 |
< |
}catch(RejectedExecutionException e){} |
596 |
< |
catch(InterruptedException e2){} |
597 |
< |
catch(ExecutionException e3){} |
598 |
< |
joinPool(p); |
194 |
< |
} |
195 |
< |
|
196 |
< |
public void testExecuteRunnable () { |
197 |
< |
try { |
198 |
< |
Executor e = new DirectExecutor(); |
199 |
< |
Task task = new Task(); |
200 |
< |
|
201 |
< |
assertFalse("task should not be complete", task.isCompleted()); |
202 |
< |
|
203 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
204 |
< |
String result = future.get(); |
205 |
< |
|
206 |
< |
assertTrue("task should be complete", task.isCompleted()); |
207 |
< |
assertSame("should return test string", TEST_STRING, result); |
208 |
< |
} |
209 |
< |
catch (ExecutionException ex) { |
210 |
< |
fail("Unexpected exception"); |
211 |
< |
} |
212 |
< |
catch (InterruptedException ex) { |
213 |
< |
fail("Unexpected exception"); |
214 |
< |
} |
215 |
< |
} |
216 |
< |
|
217 |
< |
public void testInvokeRunnable () { |
218 |
< |
try { |
219 |
< |
Executor e = new DirectExecutor(); |
220 |
< |
Task task = new Task(); |
221 |
< |
|
222 |
< |
assertFalse("task should not be complete", task.isCompleted()); |
223 |
< |
|
224 |
< |
Executors.invoke(e, task); |
225 |
< |
|
226 |
< |
assertTrue("task should be complete", task.isCompleted()); |
227 |
< |
} |
228 |
< |
catch (ExecutionException ex) { |
229 |
< |
fail("Unexpected exception"); |
230 |
< |
} |
231 |
< |
catch (InterruptedException ex) { |
232 |
< |
fail("Unexpected exception"); |
594 |
> |
shouldThrow(); |
595 |
> |
} |
596 |
> |
catch(ExecutionException success){ |
597 |
> |
} catch(Exception e) { |
598 |
> |
unexpectedException(); |
599 |
|
} |
600 |
+ |
joinPool(p); |
601 |
|
} |
602 |
|
|
236 |
– |
public void testExecuteCallable () { |
237 |
– |
try { |
238 |
– |
Executor e = new DirectExecutor(); |
239 |
– |
Future<String> future = Executors.execute(e, new StringTask()); |
240 |
– |
String result = future.get(); |
241 |
– |
|
242 |
– |
assertSame("should return test string", TEST_STRING, result); |
243 |
– |
} |
244 |
– |
catch (ExecutionException ex) { |
245 |
– |
fail("Unexpected exception"); |
246 |
– |
} |
247 |
– |
catch (InterruptedException ex) { |
248 |
– |
fail("Unexpected exception"); |
249 |
– |
} |
250 |
– |
} |
251 |
– |
|
252 |
– |
public void testInvokeCallable () { |
253 |
– |
try { |
254 |
– |
Executor e = new DirectExecutor(); |
255 |
– |
String result = Executors.invoke(e, new StringTask()); |
256 |
– |
|
257 |
– |
assertSame("should return test string", TEST_STRING, result); |
258 |
– |
} |
259 |
– |
catch (ExecutionException ex) { |
260 |
– |
fail("Unexpected exception" ); |
261 |
– |
} |
262 |
– |
catch (InterruptedException ex) { |
263 |
– |
fail("Unexpected exception"); |
264 |
– |
} |
265 |
– |
} |
266 |
– |
|
267 |
– |
private static final String TEST_STRING = "a test string"; |
268 |
– |
|
269 |
– |
private static class Task implements Runnable { |
270 |
– |
public void run() { completed = true; } |
271 |
– |
public boolean isCompleted() { return completed; } |
272 |
– |
public void reset() { completed = false; } |
273 |
– |
private boolean completed = false; |
274 |
– |
} |
275 |
– |
|
276 |
– |
private static class StringTask implements Callable<String> { |
277 |
– |
public String call() { return TEST_STRING; } |
278 |
– |
} |
603 |
|
|
280 |
– |
static class DirectExecutor implements Executor { |
281 |
– |
public void execute(Runnable r) { |
282 |
– |
r.run(); |
283 |
– |
} |
284 |
– |
} |
604 |
|
|
605 |
|
/** |
606 |
< |
* Check that timeouts from execute will time out if they compute |
288 |
< |
* too long. |
606 |
> |
* timeouts from execute will time out if they compute too long. |
607 |
|
*/ |
290 |
– |
|
608 |
|
public void testTimedCallable() { |
609 |
|
int N = 10000; |
610 |
|
ExecutorService executor = Executors.newSingleThreadExecutor(); |
630 |
|
return; |
631 |
|
} |
632 |
|
catch (Exception e) { |
633 |
< |
fail("unexpected exception: " + e); |
633 |
> |
unexpectedException(); |
634 |
|
} |
635 |
|
} |
636 |
|
// if by chance we didn't ever time out, total time must be small |
643 |
|
} |
644 |
|
|
645 |
|
|
646 |
< |
static class TimedCallable<T> implements Callable<T> { |
647 |
< |
private final Executor exec; |
648 |
< |
private final Callable<T> func; |
649 |
< |
private final long msecs; |
650 |
< |
|
651 |
< |
TimedCallable(Executor exec, Callable<T> func, long msecs) { |
652 |
< |
this.exec = exec; |
653 |
< |
this.func = func; |
654 |
< |
this.msecs = msecs; |
655 |
< |
} |
656 |
< |
|
657 |
< |
public T call() throws Exception { |
658 |
< |
Future<T> ftask = Executors.execute(exec, func); |
659 |
< |
try { |
660 |
< |
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
661 |
< |
} finally { |
662 |
< |
ftask.cancel(true); |
663 |
< |
} |
664 |
< |
} |
646 |
> |
/** |
647 |
> |
* ThreadPoolExecutor using defaultThreadFactory has |
648 |
> |
* specified group, priority, daemon status, and name |
649 |
> |
*/ |
650 |
> |
public void testDefaultThreadFactory() { |
651 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
652 |
> |
Runnable r = new Runnable() { |
653 |
> |
public void run() { |
654 |
> |
Thread current = Thread.currentThread(); |
655 |
> |
threadAssertTrue(!current.isDaemon()); |
656 |
> |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
657 |
> |
ThreadGroup g = current.getThreadGroup(); |
658 |
> |
SecurityManager s = System.getSecurityManager(); |
659 |
> |
if (s != null) |
660 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
661 |
> |
else |
662 |
> |
threadAssertTrue(g == egroup); |
663 |
> |
String name = current.getName(); |
664 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
665 |
> |
} |
666 |
> |
}; |
667 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
668 |
> |
|
669 |
> |
e.execute(r); |
670 |
> |
e.shutdown(); |
671 |
> |
try { |
672 |
> |
Thread.sleep(SHORT_DELAY_MS); |
673 |
> |
} catch (Exception eX) { |
674 |
> |
unexpectedException(); |
675 |
> |
} finally { |
676 |
> |
joinPool(e); |
677 |
> |
} |
678 |
|
} |
679 |
|
|
680 |
+ |
/** |
681 |
+ |
* ThreadPoolExecutor using privilegedThreadFactory has |
682 |
+ |
* specified group, priority, daemon status, name, |
683 |
+ |
* access control context and context class loader |
684 |
+ |
*/ |
685 |
+ |
public void testPrivilegedThreadFactory() { |
686 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
687 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
688 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
689 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
690 |
+ |
Policy.setPolicy(policy); |
691 |
+ |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
692 |
+ |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
693 |
+ |
final AccessControlContext thisacc = AccessController.getContext(); |
694 |
+ |
Runnable r = new Runnable() { |
695 |
+ |
public void run() { |
696 |
+ |
Thread current = Thread.currentThread(); |
697 |
+ |
threadAssertTrue(!current.isDaemon()); |
698 |
+ |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
699 |
+ |
ThreadGroup g = current.getThreadGroup(); |
700 |
+ |
SecurityManager s = System.getSecurityManager(); |
701 |
+ |
if (s != null) |
702 |
+ |
threadAssertTrue(g == s.getThreadGroup()); |
703 |
+ |
else |
704 |
+ |
threadAssertTrue(g == egroup); |
705 |
+ |
String name = current.getName(); |
706 |
+ |
threadAssertTrue(name.endsWith("thread-1")); |
707 |
+ |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
708 |
+ |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
709 |
+ |
} |
710 |
+ |
}; |
711 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
712 |
+ |
|
713 |
+ |
Policy.setPolicy(savedPolicy); |
714 |
+ |
e.execute(r); |
715 |
+ |
e.shutdown(); |
716 |
+ |
try { |
717 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
718 |
+ |
} catch (Exception ex) { |
719 |
+ |
unexpectedException(); |
720 |
+ |
} finally { |
721 |
+ |
joinPool(e); |
722 |
+ |
} |
723 |
|
|
724 |
< |
private static class Fib implements Callable<BigInteger> { |
352 |
< |
private final BigInteger n; |
353 |
< |
Fib(long n) { |
354 |
< |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
355 |
< |
this.n = BigInteger.valueOf(n); |
356 |
< |
} |
357 |
< |
public BigInteger call() { |
358 |
< |
BigInteger f1 = BigInteger.ONE; |
359 |
< |
BigInteger f2 = f1; |
360 |
< |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
361 |
< |
BigInteger t = f1.add(f2); |
362 |
< |
f1 = f2; |
363 |
< |
f2 = t; |
364 |
< |
} |
365 |
< |
return f1; |
366 |
< |
} |
367 |
< |
}; |
368 |
< |
|
369 |
< |
|
724 |
> |
} |
725 |
|
|
726 |
|
} |