1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
53 |
|
} catch (NullPointerException success) {} |
54 |
|
} |
55 |
|
|
57 |
– |
|
56 |
|
/** |
57 |
|
* A new SingleThreadExecutor can execute runnables |
58 |
|
*/ |
99 |
|
} |
100 |
|
} |
101 |
|
|
104 |
– |
|
102 |
|
/** |
103 |
|
* A new newFixedThreadPool can execute runnables |
104 |
|
*/ |
141 |
|
} catch (IllegalArgumentException success) {} |
142 |
|
} |
143 |
|
|
147 |
– |
|
144 |
|
/** |
145 |
|
* An unconfigurable newFixedThreadPool can execute runnables |
146 |
|
*/ |
172 |
|
} catch (NullPointerException success) {} |
173 |
|
} |
174 |
|
|
179 |
– |
|
175 |
|
/** |
176 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
177 |
|
*/ |
247 |
|
* Future.get on submitted tasks will time out if they compute too long. |
248 |
|
*/ |
249 |
|
public void testTimedCallable() throws Exception { |
250 |
+ |
final ExecutorService[] executors = { |
251 |
+ |
Executors.newSingleThreadExecutor(), |
252 |
+ |
Executors.newCachedThreadPool(), |
253 |
+ |
Executors.newFixedThreadPool(2), |
254 |
+ |
Executors.newScheduledThreadPool(2), |
255 |
+ |
}; |
256 |
+ |
|
257 |
|
final Runnable sleeper = new CheckedInterruptedRunnable() { |
258 |
|
public void realRun() throws InterruptedException { |
259 |
< |
Thread.sleep(LONG_DELAY_MS); |
259 |
> |
delay(LONG_DELAY_MS); |
260 |
|
}}; |
261 |
< |
for (ExecutorService executor : |
262 |
< |
new ExecutorService[] { |
263 |
< |
Executors.newSingleThreadExecutor(), |
264 |
< |
Executors.newCachedThreadPool(), |
265 |
< |
Executors.newFixedThreadPool(2), |
266 |
< |
Executors.newScheduledThreadPool(2), |
267 |
< |
}) { |
268 |
< |
try { |
269 |
< |
Future future = executor.submit(sleeper); |
268 |
< |
try { |
269 |
< |
future.get(SHORT_DELAY_MS, MILLISECONDS); |
270 |
< |
shouldThrow(); |
271 |
< |
} catch (TimeoutException success) { |
272 |
< |
} finally { |
273 |
< |
future.cancel(true); |
274 |
< |
} |
275 |
< |
} |
276 |
< |
finally { |
277 |
< |
joinPool(executor); |
278 |
< |
} |
261 |
> |
|
262 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
263 |
> |
for (final ExecutorService executor : executors) { |
264 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
265 |
> |
public void realRun() { |
266 |
> |
long startTime = System.nanoTime(); |
267 |
> |
Future future = executor.submit(sleeper); |
268 |
> |
assertFutureTimesOut(future); |
269 |
> |
}})); |
270 |
|
} |
271 |
+ |
for (Thread thread : threads) |
272 |
+ |
awaitTermination(thread); |
273 |
+ |
for (ExecutorService executor : executors) |
274 |
+ |
joinPool(executor); |
275 |
|
} |
276 |
|
|
282 |
– |
|
277 |
|
/** |
278 |
|
* ThreadPoolExecutor using defaultThreadFactory has |
279 |
|
* specified group, priority, daemon status, and name |
307 |
|
} |
308 |
|
|
309 |
|
try { |
310 |
< |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
delay(SHORT_DELAY_MS); |
311 |
|
} finally { |
312 |
|
joinPool(e); |
313 |
|
} |
343 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
344 |
|
e.execute(r); |
345 |
|
e.shutdown(); |
346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
346 |
> |
delay(SHORT_DELAY_MS); |
347 |
|
joinPool(e); |
348 |
|
}}; |
349 |
|
|
381 |
|
} |
382 |
|
} |
383 |
|
|
390 |
– |
|
384 |
|
/** |
385 |
|
* Without class loader permissions, creating |
386 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
497 |
|
Executors.privilegedCallable(new CheckCCL()).call(); |
498 |
|
}}; |
499 |
|
|
500 |
< |
runWithPermissions(r, |
500 |
> |
runWithPermissions(r, |
501 |
|
new RuntimePermission("getClassLoader"), |
502 |
|
new RuntimePermission("setContextClassLoader")); |
503 |
|
} |
536 |
|
assertSame(one, c.call()); |
537 |
|
} |
538 |
|
|
546 |
– |
|
539 |
|
/** |
540 |
|
* callable(null Runnable) throws NPE |
541 |
|
*/ |
576 |
|
} catch (NullPointerException success) {} |
577 |
|
} |
578 |
|
|
587 |
– |
|
579 |
|
} |