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 |
|
} |
22 |
|
|
23 |
|
private static final String TEST_STRING = "a test string"; |
24 |
|
|
25 |
– |
private static class MyTask implements Runnable { |
26 |
– |
public void run() { completed = true; } |
27 |
– |
public boolean isCompleted() { return completed; } |
28 |
– |
public void reset() { completed = false; } |
29 |
– |
private boolean completed = false; |
30 |
– |
} |
31 |
– |
|
25 |
|
private static class StringTask implements Callable<String> { |
26 |
|
public String call() { return TEST_STRING; } |
27 |
|
} |
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(Executor, Runnable) will throw |
251 |
< |
* RejectedExecutionException Attempting to execute a runnable on |
252 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
253 |
< |
* runnables are attempted on a pool capable on handling one |
254 |
< |
* until it throws an exception |
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 of a null callable throws NPE |
409 |
> |
*/ |
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)); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
458 |
< |
* execute(Executor, Callable) will throw |
459 |
< |
* RejectedExecutionException Attempting to execute a callable on |
106 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
107 |
< |
* runnables are attempted on a pool capable on handling one |
108 |
< |
* until it throws an exception |
458 |
> |
* execute(Executor, Callable)throws RejectedExecutionException |
459 |
> |
* if saturated. |
460 |
|
*/ |
461 |
|
public void testExecute2() { |
462 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
471 |
|
|
472 |
|
|
473 |
|
/** |
474 |
< |
* invoke(Executor, Runnable) throws InterruptedException |
475 |
< |
* A single use of invoke starts that will wait long enough |
125 |
< |
* 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() { |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* invoke(Executor, Runnable) will throw |
510 |
< |
* ExecutionException An ExecutionException occurs when the |
161 |
< |
* underlying Runnable throws an exception, here the |
162 |
< |
* DivideByZeroException will cause an ExecutionException |
509 |
> |
* invoke(Executor, Runnable) throws ExecutionException if |
510 |
> |
* runnable throws exception. |
511 |
|
*/ |
512 |
|
public void testInvoke3() { |
513 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
533 |
|
|
534 |
|
|
535 |
|
/** |
536 |
< |
* invoke(Executor, Callable) throws |
537 |
< |
* InterruptedException A single use of invoke starts that will |
190 |
< |
* 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() { |
540 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
573 |
|
} |
574 |
|
|
575 |
|
/** |
576 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
577 |
< |
* An ExecutionException occurs when the underlying Runnable throws |
231 |
< |
* 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() { |
580 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
592 |
|
} |
593 |
|
|
594 |
|
shouldThrow(); |
595 |
< |
} catch(RejectedExecutionException e){} |
596 |
< |
catch(InterruptedException e2){} |
597 |
< |
catch(ExecutionException e3){} |
252 |
< |
joinPool(p); |
253 |
< |
} |
254 |
< |
|
255 |
< |
/** |
256 |
< |
* |
257 |
< |
*/ |
258 |
< |
public void testExecuteRunnable () { |
259 |
< |
try { |
260 |
< |
Executor e = new DirectExecutor(); |
261 |
< |
MyTask task = new MyTask(); |
262 |
< |
assertFalse(task.isCompleted()); |
263 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
264 |
< |
String result = future.get(); |
265 |
< |
assertTrue(task.isCompleted()); |
266 |
< |
assertSame(TEST_STRING, result); |
267 |
< |
} |
268 |
< |
catch (ExecutionException ex) { |
269 |
< |
unexpectedException(); |
270 |
< |
} |
271 |
< |
catch (InterruptedException ex) { |
272 |
< |
unexpectedException(); |
273 |
< |
} |
274 |
< |
} |
275 |
< |
|
276 |
< |
/** |
277 |
< |
* |
278 |
< |
*/ |
279 |
< |
public void testInvokeRunnable () { |
280 |
< |
try { |
281 |
< |
Executor e = new DirectExecutor(); |
282 |
< |
MyTask task = new MyTask(); |
283 |
< |
assertFalse(task.isCompleted()); |
284 |
< |
Executors.invoke(e, task); |
285 |
< |
assertTrue(task.isCompleted()); |
286 |
< |
} |
287 |
< |
catch (ExecutionException ex) { |
288 |
< |
unexpectedException(); |
289 |
< |
} |
290 |
< |
catch (InterruptedException ex) { |
595 |
> |
} |
596 |
> |
catch(ExecutionException success){ |
597 |
> |
} catch(Exception e) { |
598 |
|
unexpectedException(); |
599 |
|
} |
600 |
+ |
joinPool(p); |
601 |
|
} |
602 |
|
|
295 |
– |
/** |
296 |
– |
* |
297 |
– |
*/ |
298 |
– |
public void testExecuteCallable () { |
299 |
– |
try { |
300 |
– |
Executor e = new DirectExecutor(); |
301 |
– |
Future<String> future = Executors.execute(e, new StringTask()); |
302 |
– |
String result = future.get(); |
303 |
– |
assertSame(TEST_STRING, result); |
304 |
– |
} |
305 |
– |
catch (ExecutionException ex) { |
306 |
– |
unexpectedException(); |
307 |
– |
} |
308 |
– |
catch (InterruptedException ex) { |
309 |
– |
unexpectedException(); |
310 |
– |
} |
311 |
– |
} |
603 |
|
|
313 |
– |
/** |
314 |
– |
* |
315 |
– |
*/ |
316 |
– |
public void testInvokeCallable () { |
317 |
– |
try { |
318 |
– |
Executor e = new DirectExecutor(); |
319 |
– |
String result = Executors.invoke(e, new StringTask()); |
320 |
– |
|
321 |
– |
assertSame(TEST_STRING, result); |
322 |
– |
} |
323 |
– |
catch (ExecutionException ex) { |
324 |
– |
unexpectedException(); |
325 |
– |
} |
326 |
– |
catch (InterruptedException ex) { |
327 |
– |
unexpectedException(); |
328 |
– |
} |
329 |
– |
} |
604 |
|
|
605 |
|
/** |
606 |
|
* timeouts from execute will time out if they compute too long. |
643 |
|
} |
644 |
|
|
645 |
|
|
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 |
+ |
} |
725 |
|
|
726 |
|
} |