6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
– |
import java.math.BigInteger; |
13 |
|
import java.security.*; |
14 |
|
|
15 |
|
public class ExecutorsTest extends JSR166TestCase { |
52 |
|
} catch (NullPointerException success) {} |
53 |
|
} |
54 |
|
|
57 |
– |
|
55 |
|
/** |
56 |
|
* A new SingleThreadExecutor can execute runnables |
57 |
|
*/ |
98 |
|
} |
99 |
|
} |
100 |
|
|
104 |
– |
|
101 |
|
/** |
102 |
|
* A new newFixedThreadPool can execute runnables |
103 |
|
*/ |
140 |
|
} catch (IllegalArgumentException success) {} |
141 |
|
} |
142 |
|
|
147 |
– |
|
143 |
|
/** |
144 |
|
* An unconfigurable newFixedThreadPool can execute runnables |
145 |
|
*/ |
171 |
|
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
179 |
– |
|
174 |
|
/** |
175 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
176 |
|
*/ |
246 |
|
* Future.get on submitted tasks will time out if they compute too long. |
247 |
|
*/ |
248 |
|
public void testTimedCallable() throws Exception { |
249 |
+ |
final ExecutorService[] executors = { |
250 |
+ |
Executors.newSingleThreadExecutor(), |
251 |
+ |
Executors.newCachedThreadPool(), |
252 |
+ |
Executors.newFixedThreadPool(2), |
253 |
+ |
Executors.newScheduledThreadPool(2), |
254 |
+ |
}; |
255 |
+ |
|
256 |
|
final Runnable sleeper = new CheckedInterruptedRunnable() { |
257 |
|
public void realRun() throws InterruptedException { |
258 |
< |
Thread.sleep(LONG_DELAY_MS); |
258 |
> |
delay(LONG_DELAY_MS); |
259 |
|
}}; |
260 |
< |
for (ExecutorService executor : |
261 |
< |
new ExecutorService[] { |
262 |
< |
Executors.newSingleThreadExecutor(), |
263 |
< |
Executors.newCachedThreadPool(), |
264 |
< |
Executors.newFixedThreadPool(2), |
265 |
< |
Executors.newScheduledThreadPool(2), |
266 |
< |
}) { |
267 |
< |
try { |
268 |
< |
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 |
< |
} |
260 |
> |
|
261 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
262 |
> |
for (final ExecutorService executor : executors) { |
263 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
264 |
> |
public void realRun() { |
265 |
> |
long startTime = System.nanoTime(); |
266 |
> |
Future future = executor.submit(sleeper); |
267 |
> |
assertFutureTimesOut(future); |
268 |
> |
}})); |
269 |
|
} |
270 |
+ |
for (Thread thread : threads) |
271 |
+ |
awaitTermination(thread); |
272 |
+ |
for (ExecutorService executor : executors) |
273 |
+ |
joinPool(executor); |
274 |
|
} |
275 |
|
|
282 |
– |
|
276 |
|
/** |
277 |
|
* ThreadPoolExecutor using defaultThreadFactory has |
278 |
|
* specified group, priority, daemon status, and name |
306 |
|
} |
307 |
|
|
308 |
|
try { |
309 |
< |
Thread.sleep(SHORT_DELAY_MS); |
309 |
> |
delay(SHORT_DELAY_MS); |
310 |
|
} finally { |
311 |
|
joinPool(e); |
312 |
|
} |
342 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
343 |
|
e.execute(r); |
344 |
|
e.shutdown(); |
345 |
< |
Thread.sleep(SHORT_DELAY_MS); |
345 |
> |
delay(SHORT_DELAY_MS); |
346 |
|
joinPool(e); |
347 |
|
}}; |
348 |
|
|
380 |
|
} |
381 |
|
} |
382 |
|
|
390 |
– |
|
383 |
|
/** |
384 |
|
* Without class loader permissions, creating |
385 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
496 |
|
Executors.privilegedCallable(new CheckCCL()).call(); |
497 |
|
}}; |
498 |
|
|
499 |
< |
runWithPermissions(r, |
499 |
> |
runWithPermissions(r, |
500 |
|
new RuntimePermission("getClassLoader"), |
501 |
|
new RuntimePermission("setContextClassLoader")); |
502 |
|
} |
535 |
|
assertSame(one, c.call()); |
536 |
|
} |
537 |
|
|
546 |
– |
|
538 |
|
/** |
539 |
|
* callable(null Runnable) throws NPE |
540 |
|
*/ |
575 |
|
} catch (NullPointerException success) {} |
576 |
|
} |
577 |
|
|
587 |
– |
|
578 |
|
} |