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 |
|
*/ |
349 |
|
assertSame(q, wq); |
350 |
|
assertFalse(wq.contains(tasks[0])); |
351 |
|
assertTrue(wq.contains(tasks[4])); |
352 |
+ |
for (int i = 1; i < 5; ++i) |
353 |
+ |
tasks[i].cancel(true); |
354 |
+ |
p1.shutdownNow(); |
355 |
|
} catch(Exception e) { |
356 |
|
unexpectedException(); |
357 |
|
} finally { |
1510 |
|
} |
1511 |
|
} |
1512 |
|
|
1513 |
+ |
/** |
1514 |
+ |
* Execution continues if there is at least one thread even if |
1515 |
+ |
* thread factory fails to create more |
1516 |
+ |
*/ |
1517 |
+ |
public void testFailingThreadFactory() { |
1518 |
+ |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1519 |
+ |
try { |
1520 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1521 |
+ |
for (int k = 0; k < 100; ++k) { |
1522 |
+ |
e.execute(new NoOpRunnable()); |
1523 |
+ |
} |
1524 |
+ |
Thread.sleep(LONG_DELAY_MS); |
1525 |
+ |
} catch(Exception ex) { |
1526 |
+ |
unexpectedException(); |
1527 |
+ |
} finally { |
1528 |
+ |
joinPool(e); |
1529 |
+ |
} |
1530 |
+ |
} |
1531 |
+ |
|
1532 |
+ |
/** |
1533 |
+ |
* allowsCoreThreadTimeOut is by default false. |
1534 |
+ |
*/ |
1535 |
+ |
public void testAllowsCoreThreadTimeOut() { |
1536 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1537 |
+ |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1538 |
+ |
joinPool(tpe); |
1539 |
+ |
} |
1540 |
+ |
|
1541 |
+ |
/** |
1542 |
+ |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1543 |
+ |
*/ |
1544 |
+ |
public void testAllowCoreThreadTimeOut_true() { |
1545 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1546 |
+ |
tpe.allowCoreThreadTimeOut(true); |
1547 |
+ |
tpe.execute(new NoOpRunnable()); |
1548 |
+ |
try { |
1549 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1550 |
+ |
assertEquals(0, tpe.getPoolSize()); |
1551 |
+ |
} catch(InterruptedException e){ |
1552 |
+ |
unexpectedException(); |
1553 |
+ |
} finally { |
1554 |
+ |
joinPool(tpe); |
1555 |
+ |
} |
1556 |
+ |
} |
1557 |
+ |
|
1558 |
+ |
/** |
1559 |
+ |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1560 |
+ |
*/ |
1561 |
+ |
public void testAllowCoreThreadTimeOut_false() { |
1562 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1563 |
+ |
tpe.allowCoreThreadTimeOut(false); |
1564 |
+ |
tpe.execute(new NoOpRunnable()); |
1565 |
+ |
try { |
1566 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1567 |
+ |
assertTrue(tpe.getPoolSize() >= 1); |
1568 |
+ |
} catch(InterruptedException e){ |
1569 |
+ |
unexpectedException(); |
1570 |
+ |
} finally { |
1571 |
+ |
joinPool(tpe); |
1572 |
+ |
} |
1573 |
+ |
} |
1574 |
|
|
1575 |
|
} |