36 |
|
} |
37 |
|
} |
38 |
|
|
39 |
+ |
static class FailingThreadFactory implements ThreadFactory{ |
40 |
+ |
int calls = 0; |
41 |
+ |
public Thread newThread(Runnable r){ |
42 |
+ |
if (++calls > 1) return null; |
43 |
+ |
return new Thread(r); |
44 |
+ |
} |
45 |
+ |
} |
46 |
+ |
|
47 |
+ |
|
48 |
|
/** |
49 |
|
* execute successfully executes a runnable |
50 |
|
*/ |
126 |
|
unexpectedException(); |
127 |
|
} |
128 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
129 |
< |
p2.shutdown(); |
129 |
> |
try { p2.shutdown(); } catch(SecurityException ok) { return; } |
130 |
|
joinPool(p2); |
131 |
|
} |
132 |
|
|
156 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
157 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
158 |
|
assertSame(tf, p.getThreadFactory()); |
150 |
– |
p.shutdown(); |
159 |
|
joinPool(p); |
160 |
|
} |
161 |
|
|
167 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
168 |
|
p.setThreadFactory(tf); |
169 |
|
assertSame(tf, p.getThreadFactory()); |
162 |
– |
p.shutdown(); |
170 |
|
joinPool(p); |
171 |
|
} |
172 |
|
|
192 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
193 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
194 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
188 |
– |
p.shutdown(); |
195 |
|
joinPool(p); |
196 |
|
} |
197 |
|
|
204 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
205 |
|
p.setRejectedExecutionHandler(h); |
206 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
201 |
– |
p.shutdown(); |
207 |
|
joinPool(p); |
208 |
|
} |
209 |
|
|
286 |
|
|
287 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
288 |
|
assertFalse(p1.isShutdown()); |
289 |
< |
p1.shutdown(); |
289 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
290 |
|
assertTrue(p1.isShutdown()); |
291 |
|
joinPool(p1); |
292 |
|
} |
301 |
|
try { |
302 |
|
p1.execute(new MediumRunnable()); |
303 |
|
} finally { |
304 |
< |
p1.shutdown(); |
304 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
305 |
|
} |
306 |
|
try { |
307 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
321 |
|
p1.execute(new SmallRunnable()); |
322 |
|
assertFalse(p1.isTerminating()); |
323 |
|
} finally { |
324 |
< |
p1.shutdown(); |
324 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
325 |
|
} |
326 |
|
try { |
327 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
349 |
|
assertSame(q, wq); |
350 |
|
assertFalse(wq.contains(tasks[0])); |
351 |
|
assertTrue(wq.contains(tasks[4])); |
347 |
– |
p1.shutdownNow(); |
352 |
|
} catch(Exception e) { |
353 |
|
unexpectedException(); |
354 |
|
} finally { |
378 |
|
assertTrue(q.contains(tasks[3])); |
379 |
|
assertTrue(p1.remove(tasks[3])); |
380 |
|
assertFalse(q.contains(tasks[3])); |
377 |
– |
p1.shutdownNow(); |
381 |
|
} catch(Exception e) { |
382 |
|
unexpectedException(); |
383 |
|
} finally { |
400 |
|
p1.purge(); |
401 |
|
long count = p1.getTaskCount(); |
402 |
|
assertTrue(count >= 2 && count < 5); |
400 |
– |
p1.shutdownNow(); |
403 |
|
joinPool(p1); |
404 |
|
} |
405 |
|
|
414 |
|
p1.execute(new MediumPossiblyInterruptedRunnable()); |
415 |
|
} |
416 |
|
finally { |
417 |
< |
l = p1.shutdownNow(); |
417 |
> |
try { |
418 |
> |
l = p1.shutdownNow(); |
419 |
> |
} catch (SecurityException ok) { return; } |
420 |
> |
|
421 |
|
} |
422 |
|
assertTrue(p1.isShutdown()); |
423 |
|
assertTrue(l.size() <= 4); |
778 |
|
for(int i = 1; i < 5; ++i) { |
779 |
|
assertTrue(tasks[i].done); |
780 |
|
} |
781 |
< |
p.shutdownNow(); |
781 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
782 |
|
} catch(RejectedExecutionException ex){ |
783 |
|
unexpectedException(); |
784 |
|
} finally { |
805 |
|
for(int i = 0; i < 5; ++i){ |
806 |
|
assertFalse(tasks[i].done); |
807 |
|
} |
808 |
< |
p.shutdownNow(); |
808 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
809 |
|
} catch(RejectedExecutionException ex){ |
810 |
|
unexpectedException(); |
811 |
|
} finally { |
828 |
|
p.execute(r3); |
829 |
|
assertFalse(p.getQueue().contains(r2)); |
830 |
|
assertTrue(p.getQueue().contains(r3)); |
831 |
< |
p.shutdownNow(); |
831 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
832 |
|
} catch(RejectedExecutionException ex){ |
833 |
|
unexpectedException(); |
834 |
|
} finally { |
842 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
843 |
|
ThreadPoolExecutor tpe = |
844 |
|
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
845 |
< |
tpe.shutdown(); |
845 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
846 |
|
try { |
847 |
|
tpe.execute(new NoOpRunnable()); |
848 |
|
shouldThrow(); |
858 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
859 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
860 |
|
|
861 |
< |
p.shutdown(); |
861 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
862 |
|
try { |
863 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
864 |
|
p.execute(r); |
877 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
878 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
879 |
|
|
880 |
< |
p.shutdown(); |
880 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
881 |
|
try { |
882 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
883 |
|
p.execute(r); |
897 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
898 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
899 |
|
|
900 |
< |
p.shutdown(); |
900 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
901 |
|
try { |
902 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
903 |
|
p.execute(r); |
937 |
|
shouldThrow(); |
938 |
|
} catch(IllegalArgumentException success){ |
939 |
|
} finally { |
940 |
< |
tpe.shutdown(); |
940 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
941 |
|
} |
942 |
|
joinPool(tpe); |
943 |
|
} |
956 |
|
shouldThrow(); |
957 |
|
} catch(IllegalArgumentException success){ |
958 |
|
} finally { |
959 |
< |
tpe.shutdown(); |
959 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
960 |
|
} |
961 |
|
joinPool(tpe); |
962 |
|
} |
975 |
|
shouldThrow(); |
976 |
|
} catch(IllegalArgumentException success){ |
977 |
|
} finally { |
978 |
< |
tpe.shutdown(); |
978 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
979 |
|
} |
980 |
|
joinPool(tpe); |
981 |
|
} |
996 |
|
shouldThrow(); |
997 |
|
} catch(IllegalArgumentException success){ |
998 |
|
} finally { |
999 |
< |
tpe.shutdown(); |
999 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1000 |
|
} |
1001 |
|
joinPool(tpe); |
1002 |
|
} |
1006 |
|
*/ |
1007 |
|
public void testTerminated() { |
1008 |
|
ExtendedTPE tpe = new ExtendedTPE(); |
1009 |
< |
tpe.shutdown(); |
1009 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1010 |
|
assertTrue(tpe.terminatedCalled); |
1011 |
|
joinPool(tpe); |
1012 |
|
} |
1023 |
|
assertTrue(r.done); |
1024 |
|
assertTrue(tpe.beforeCalled); |
1025 |
|
assertTrue(tpe.afterCalled); |
1026 |
< |
tpe.shutdown(); |
1026 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1027 |
|
} |
1028 |
|
catch(Exception ex) { |
1029 |
|
unexpectedException(); |
1488 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1489 |
|
l.add(new StringTask()); |
1490 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1491 |
+ |
l.add(new StringTask()); |
1492 |
|
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1493 |
< |
assertEquals(2, result.size()); |
1493 |
> |
assertEquals(3, result.size()); |
1494 |
|
Iterator<Future<String>> it = result.iterator(); |
1495 |
|
Future<String> f1 = it.next(); |
1496 |
|
Future<String> f2 = it.next(); |
1497 |
+ |
Future<String> f3 = it.next(); |
1498 |
|
assertTrue(f1.isDone()); |
1492 |
– |
assertFalse(f1.isCancelled()); |
1499 |
|
assertTrue(f2.isDone()); |
1500 |
+ |
assertTrue(f3.isDone()); |
1501 |
+ |
assertFalse(f1.isCancelled()); |
1502 |
|
assertTrue(f2.isCancelled()); |
1503 |
|
} catch(Exception ex) { |
1504 |
|
unexpectedException(); |
1507 |
|
} |
1508 |
|
} |
1509 |
|
|
1510 |
+ |
/** |
1511 |
+ |
* Execution continues if there is at least one thread even if |
1512 |
+ |
* thread factory fails to create more |
1513 |
+ |
*/ |
1514 |
+ |
public void testFailingThreadFactory() { |
1515 |
+ |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1516 |
+ |
try { |
1517 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1518 |
+ |
for (int k = 0; k < 100; ++k) { |
1519 |
+ |
e.execute(new NoOpRunnable()); |
1520 |
+ |
} |
1521 |
+ |
Thread.sleep(LONG_DELAY_MS); |
1522 |
+ |
} catch(Exception ex) { |
1523 |
+ |
unexpectedException(); |
1524 |
+ |
} finally { |
1525 |
+ |
joinPool(e); |
1526 |
+ |
} |
1527 |
+ |
} |
1528 |
+ |
|
1529 |
+ |
/** |
1530 |
+ |
* allowsCoreThreadTimeOut is by default false. |
1531 |
+ |
*/ |
1532 |
+ |
public void testAllowsCoreThreadTimeOut() { |
1533 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1534 |
+ |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1535 |
+ |
joinPool(tpe); |
1536 |
+ |
} |
1537 |
+ |
|
1538 |
+ |
/** |
1539 |
+ |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1540 |
+ |
*/ |
1541 |
+ |
public void testAllowCoreThreadTimeOut_true() { |
1542 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1543 |
+ |
tpe.allowCoreThreadTimeOut(true); |
1544 |
+ |
tpe.execute(new NoOpRunnable()); |
1545 |
+ |
try { |
1546 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1547 |
+ |
assertEquals(0, tpe.getPoolSize()); |
1548 |
+ |
} catch(InterruptedException e){ |
1549 |
+ |
unexpectedException(); |
1550 |
+ |
} finally { |
1551 |
+ |
joinPool(tpe); |
1552 |
+ |
} |
1553 |
+ |
} |
1554 |
+ |
|
1555 |
+ |
/** |
1556 |
+ |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1557 |
+ |
*/ |
1558 |
+ |
public void testAllowCoreThreadTimeOut_false() { |
1559 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1560 |
+ |
tpe.allowCoreThreadTimeOut(false); |
1561 |
+ |
tpe.execute(new NoOpRunnable()); |
1562 |
+ |
try { |
1563 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1564 |
+ |
assertTrue(tpe.getPoolSize() >= 1); |
1565 |
+ |
} catch(InterruptedException e){ |
1566 |
+ |
unexpectedException(); |
1567 |
+ |
} finally { |
1568 |
+ |
joinPool(tpe); |
1569 |
+ |
} |
1570 |
+ |
} |
1571 |
|
|
1572 |
|
} |