1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import java.util.concurrent.atomic.*; |
11 |
|
import junit.framework.*; |
12 |
|
import java.util.*; |
13 |
|
|
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
+ |
static class FailingThreadFactory implements ThreadFactory{ |
41 |
+ |
int calls = 0; |
42 |
+ |
public Thread newThread(Runnable r){ |
43 |
+ |
if (++calls > 1) return null; |
44 |
+ |
return new Thread(r); |
45 |
+ |
} |
46 |
+ |
} |
47 |
+ |
|
48 |
+ |
|
49 |
|
/** |
50 |
|
* execute successfully executes a runnable |
51 |
|
*/ |
127 |
|
unexpectedException(); |
128 |
|
} |
129 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
130 |
< |
p2.shutdown(); |
130 |
> |
try { p2.shutdown(); } catch(SecurityException ok) { return; } |
131 |
|
joinPool(p2); |
132 |
|
} |
133 |
|
|
157 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
158 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
159 |
|
assertSame(tf, p.getThreadFactory()); |
149 |
– |
p.shutdown(); |
160 |
|
joinPool(p); |
161 |
|
} |
162 |
|
|
168 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
169 |
|
p.setThreadFactory(tf); |
170 |
|
assertSame(tf, p.getThreadFactory()); |
161 |
– |
p.shutdown(); |
171 |
|
joinPool(p); |
172 |
|
} |
173 |
|
|
193 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
194 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
195 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
187 |
– |
p.shutdown(); |
196 |
|
joinPool(p); |
197 |
|
} |
198 |
|
|
205 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
206 |
|
p.setRejectedExecutionHandler(h); |
207 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
200 |
– |
p.shutdown(); |
208 |
|
joinPool(p); |
209 |
|
} |
210 |
|
|
287 |
|
|
288 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
289 |
|
assertFalse(p1.isShutdown()); |
290 |
< |
p1.shutdown(); |
290 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
291 |
|
assertTrue(p1.isShutdown()); |
292 |
|
joinPool(p1); |
293 |
|
} |
302 |
|
try { |
303 |
|
p1.execute(new MediumRunnable()); |
304 |
|
} finally { |
305 |
< |
p1.shutdown(); |
305 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
306 |
|
} |
307 |
|
try { |
308 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
322 |
|
p1.execute(new SmallRunnable()); |
323 |
|
assertFalse(p1.isTerminating()); |
324 |
|
} finally { |
325 |
< |
p1.shutdown(); |
325 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
326 |
|
} |
327 |
|
try { |
328 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
350 |
|
assertSame(q, wq); |
351 |
|
assertFalse(wq.contains(tasks[0])); |
352 |
|
assertTrue(wq.contains(tasks[4])); |
353 |
+ |
for (int i = 1; i < 5; ++i) |
354 |
+ |
tasks[i].cancel(true); |
355 |
|
p1.shutdownNow(); |
356 |
|
} catch(Exception e) { |
357 |
|
unexpectedException(); |
382 |
|
assertTrue(q.contains(tasks[3])); |
383 |
|
assertTrue(p1.remove(tasks[3])); |
384 |
|
assertFalse(q.contains(tasks[3])); |
376 |
– |
p1.shutdownNow(); |
385 |
|
} catch(Exception e) { |
386 |
|
unexpectedException(); |
387 |
|
} finally { |
404 |
|
p1.purge(); |
405 |
|
long count = p1.getTaskCount(); |
406 |
|
assertTrue(count >= 2 && count < 5); |
399 |
– |
p1.shutdownNow(); |
407 |
|
joinPool(p1); |
408 |
|
} |
409 |
|
|
418 |
|
p1.execute(new MediumPossiblyInterruptedRunnable()); |
419 |
|
} |
420 |
|
finally { |
421 |
< |
l = p1.shutdownNow(); |
421 |
> |
try { |
422 |
> |
l = p1.shutdownNow(); |
423 |
> |
} catch (SecurityException ok) { return; } |
424 |
> |
|
425 |
|
} |
426 |
|
assertTrue(p1.isShutdown()); |
427 |
|
assertTrue(l.size() <= 4); |
782 |
|
for(int i = 1; i < 5; ++i) { |
783 |
|
assertTrue(tasks[i].done); |
784 |
|
} |
785 |
< |
p.shutdownNow(); |
785 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
786 |
|
} catch(RejectedExecutionException ex){ |
787 |
|
unexpectedException(); |
788 |
|
} finally { |
809 |
|
for(int i = 0; i < 5; ++i){ |
810 |
|
assertFalse(tasks[i].done); |
811 |
|
} |
812 |
< |
p.shutdownNow(); |
812 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
813 |
|
} catch(RejectedExecutionException ex){ |
814 |
|
unexpectedException(); |
815 |
|
} finally { |
832 |
|
p.execute(r3); |
833 |
|
assertFalse(p.getQueue().contains(r2)); |
834 |
|
assertTrue(p.getQueue().contains(r3)); |
835 |
< |
p.shutdownNow(); |
835 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
836 |
|
} catch(RejectedExecutionException ex){ |
837 |
|
unexpectedException(); |
838 |
|
} finally { |
846 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
847 |
|
ThreadPoolExecutor tpe = |
848 |
|
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
849 |
< |
tpe.shutdown(); |
849 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
850 |
|
try { |
851 |
|
tpe.execute(new NoOpRunnable()); |
852 |
|
shouldThrow(); |
862 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
863 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
864 |
|
|
865 |
< |
p.shutdown(); |
865 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
866 |
|
try { |
867 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
868 |
|
p.execute(r); |
881 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
882 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
883 |
|
|
884 |
< |
p.shutdown(); |
884 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
885 |
|
try { |
886 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
887 |
|
p.execute(r); |
901 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
902 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
903 |
|
|
904 |
< |
p.shutdown(); |
904 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
905 |
|
try { |
906 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
907 |
|
p.execute(r); |
941 |
|
shouldThrow(); |
942 |
|
} catch(IllegalArgumentException success){ |
943 |
|
} finally { |
944 |
< |
tpe.shutdown(); |
944 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
945 |
|
} |
946 |
|
joinPool(tpe); |
947 |
|
} |
960 |
|
shouldThrow(); |
961 |
|
} catch(IllegalArgumentException success){ |
962 |
|
} finally { |
963 |
< |
tpe.shutdown(); |
963 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
964 |
|
} |
965 |
|
joinPool(tpe); |
966 |
|
} |
979 |
|
shouldThrow(); |
980 |
|
} catch(IllegalArgumentException success){ |
981 |
|
} finally { |
982 |
< |
tpe.shutdown(); |
982 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
983 |
|
} |
984 |
|
joinPool(tpe); |
985 |
|
} |
1000 |
|
shouldThrow(); |
1001 |
|
} catch(IllegalArgumentException success){ |
1002 |
|
} finally { |
1003 |
< |
tpe.shutdown(); |
1003 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1004 |
|
} |
1005 |
|
joinPool(tpe); |
1006 |
|
} |
1010 |
|
*/ |
1011 |
|
public void testTerminated() { |
1012 |
|
ExtendedTPE tpe = new ExtendedTPE(); |
1013 |
< |
tpe.shutdown(); |
1013 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1014 |
|
assertTrue(tpe.terminatedCalled); |
1015 |
|
joinPool(tpe); |
1016 |
|
} |
1027 |
|
assertTrue(r.done); |
1028 |
|
assertTrue(tpe.beforeCalled); |
1029 |
|
assertTrue(tpe.afterCalled); |
1030 |
< |
tpe.shutdown(); |
1030 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1031 |
|
} |
1032 |
|
catch(Exception ex) { |
1033 |
|
unexpectedException(); |
1491 |
|
try { |
1492 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1493 |
|
l.add(new StringTask()); |
1494 |
< |
l.add(Executors.callable(new MediumInterruptedRunnable(), TEST_STRING)); |
1494 |
> |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1495 |
> |
l.add(new StringTask()); |
1496 |
|
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1497 |
< |
assertEquals(2, result.size()); |
1497 |
> |
assertEquals(3, result.size()); |
1498 |
|
Iterator<Future<String>> it = result.iterator(); |
1499 |
|
Future<String> f1 = it.next(); |
1500 |
|
Future<String> f2 = it.next(); |
1501 |
+ |
Future<String> f3 = it.next(); |
1502 |
|
assertTrue(f1.isDone()); |
1491 |
– |
assertFalse(f1.isCancelled()); |
1503 |
|
assertTrue(f2.isDone()); |
1504 |
+ |
assertTrue(f3.isDone()); |
1505 |
+ |
assertFalse(f1.isCancelled()); |
1506 |
|
assertTrue(f2.isCancelled()); |
1507 |
|
} catch(Exception ex) { |
1508 |
|
unexpectedException(); |
1511 |
|
} |
1512 |
|
} |
1513 |
|
|
1514 |
+ |
/** |
1515 |
+ |
* Execution continues if there is at least one thread even if |
1516 |
+ |
* thread factory fails to create more |
1517 |
+ |
*/ |
1518 |
+ |
public void testFailingThreadFactory() { |
1519 |
+ |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1520 |
+ |
try { |
1521 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1522 |
+ |
for (int k = 0; k < 100; ++k) { |
1523 |
+ |
e.execute(new NoOpRunnable()); |
1524 |
+ |
} |
1525 |
+ |
Thread.sleep(LONG_DELAY_MS); |
1526 |
+ |
} catch(Exception ex) { |
1527 |
+ |
unexpectedException(); |
1528 |
+ |
} finally { |
1529 |
+ |
joinPool(e); |
1530 |
+ |
} |
1531 |
+ |
} |
1532 |
+ |
|
1533 |
+ |
/** |
1534 |
+ |
* allowsCoreThreadTimeOut is by default false. |
1535 |
+ |
*/ |
1536 |
+ |
public void testAllowsCoreThreadTimeOut() { |
1537 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1538 |
+ |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1539 |
+ |
joinPool(tpe); |
1540 |
+ |
} |
1541 |
|
|
1542 |
+ |
/** |
1543 |
+ |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1544 |
+ |
*/ |
1545 |
+ |
public void testAllowCoreThreadTimeOut_true() { |
1546 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1547 |
+ |
tpe.allowCoreThreadTimeOut(true); |
1548 |
+ |
tpe.execute(new NoOpRunnable()); |
1549 |
+ |
try { |
1550 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1551 |
+ |
assertEquals(0, tpe.getPoolSize()); |
1552 |
+ |
} catch(InterruptedException e){ |
1553 |
+ |
unexpectedException(); |
1554 |
+ |
} finally { |
1555 |
+ |
joinPool(tpe); |
1556 |
+ |
} |
1557 |
+ |
} |
1558 |
+ |
|
1559 |
+ |
/** |
1560 |
+ |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1561 |
+ |
*/ |
1562 |
+ |
public void testAllowCoreThreadTimeOut_false() { |
1563 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1564 |
+ |
tpe.allowCoreThreadTimeOut(false); |
1565 |
+ |
tpe.execute(new NoOpRunnable()); |
1566 |
+ |
try { |
1567 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1568 |
+ |
assertTrue(tpe.getPoolSize() >= 1); |
1569 |
+ |
} catch(InterruptedException e){ |
1570 |
+ |
unexpectedException(); |
1571 |
+ |
} finally { |
1572 |
+ |
joinPool(tpe); |
1573 |
+ |
} |
1574 |
+ |
} |
1575 |
+ |
|
1576 |
+ |
/** |
1577 |
+ |
* execute allows the same task to be submitted multiple times, even |
1578 |
+ |
* if rejected |
1579 |
+ |
*/ |
1580 |
+ |
public void testRejectedRecycledTask() { |
1581 |
+ |
final int nTasks = 1000; |
1582 |
+ |
final AtomicInteger nRun = new AtomicInteger(0); |
1583 |
+ |
final Runnable recycledTask = new Runnable() { |
1584 |
+ |
public void run() { |
1585 |
+ |
nRun.getAndIncrement(); |
1586 |
+ |
} }; |
1587 |
+ |
final ThreadPoolExecutor p = |
1588 |
+ |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1589 |
+ |
new ArrayBlockingQueue(30)); |
1590 |
+ |
try { |
1591 |
+ |
for (int i = 0; i < nTasks; ++i) { |
1592 |
+ |
for (;;) { |
1593 |
+ |
try { |
1594 |
+ |
p.execute(recycledTask); |
1595 |
+ |
break; |
1596 |
+ |
} |
1597 |
+ |
catch (RejectedExecutionException ignore) { |
1598 |
+ |
} |
1599 |
+ |
} |
1600 |
+ |
} |
1601 |
+ |
Thread.sleep(5000); // enough time to run all tasks |
1602 |
+ |
assertEquals(nRun.get(), nTasks); |
1603 |
+ |
} catch(Exception ex) { |
1604 |
+ |
ex.printStackTrace(); |
1605 |
+ |
unexpectedException(); |
1606 |
+ |
} finally { |
1607 |
+ |
p.shutdown(); |
1608 |
+ |
} |
1609 |
+ |
} |
1610 |
+ |
|
1611 |
|
} |