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 |
|
*/ |
778 |
|
for(int i = 1; i < 5; ++i) { |
779 |
|
assertTrue(tasks[i].done); |
780 |
|
} |
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 |
+ |
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 |
+ |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
832 |
|
} catch(RejectedExecutionException ex){ |
833 |
|
unexpectedException(); |
834 |
|
} finally { |
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 |
|
} |