545 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
546 |
|
*/ |
547 |
|
public void testGetTaskCount() throws InterruptedException { |
548 |
+ |
final int TASKS = 3; |
549 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
550 |
|
final ThreadPoolExecutor p = |
551 |
|
new CustomTPE(1, 1, |
552 |
|
LONG_DELAY_MS, MILLISECONDS, |
553 |
|
new ArrayBlockingQueue<Runnable>(10)); |
554 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
554 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
555 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
– |
final CountDownLatch done = new CountDownLatch(1); |
556 |
|
assertEquals(0, p.getTaskCount()); |
557 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
558 |
|
p.execute(new CheckedRunnable() { |
559 |
|
public void realRun() throws InterruptedException { |
560 |
|
threadStarted.countDown(); |
559 |
– |
assertEquals(1, p.getTaskCount()); |
561 |
|
done.await(); |
562 |
|
}}); |
563 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
564 |
|
assertEquals(1, p.getTaskCount()); |
565 |
< |
done.countDown(); |
565 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
566 |
> |
for (int i = 0; i < TASKS; i++) { |
567 |
> |
assertEquals(1 + i, p.getTaskCount()); |
568 |
> |
p.execute(new CheckedRunnable() { |
569 |
> |
public void realRun() throws InterruptedException { |
570 |
> |
threadStarted.countDown(); |
571 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
572 |
> |
done.await(); |
573 |
> |
}}); |
574 |
> |
} |
575 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
576 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
577 |
|
} |
578 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
579 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
580 |
|
} |
581 |
|
|
582 |
|
/** |
767 |
|
final int count = 5; |
768 |
|
final AtomicInteger ran = new AtomicInteger(0); |
769 |
|
final ThreadPoolExecutor p = |
770 |
< |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
770 |
> |
new CustomTPE(poolSize, poolSize, |
771 |
> |
LONG_DELAY_MS, MILLISECONDS, |
772 |
|
new ArrayBlockingQueue<Runnable>(10)); |
773 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
773 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
774 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
775 |
|
threadsStarted.countDown(); |
776 |
|
try { |
1337 |
|
*/ |
1338 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1339 |
|
final ThreadPoolExecutor p = |
1340 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1341 |
< |
try { |
1342 |
< |
p.setCorePoolSize(-1); |
1343 |
< |
shouldThrow(); |
1344 |
< |
} catch (IllegalArgumentException success) { |
1345 |
< |
} finally { |
1346 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1340 |
> |
new CustomTPE(1, 2, |
1341 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1342 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1343 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1344 |
> |
try { |
1345 |
> |
p.setCorePoolSize(-1); |
1346 |
> |
shouldThrow(); |
1347 |
> |
} catch (IllegalArgumentException success) {} |
1348 |
|
} |
1333 |
– |
joinPool(p); |
1349 |
|
} |
1350 |
|
|
1351 |
|
/** |
1354 |
|
*/ |
1355 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1356 |
|
final ThreadPoolExecutor p = |
1357 |
< |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1358 |
< |
try { |
1359 |
< |
p.setMaximumPoolSize(1); |
1360 |
< |
shouldThrow(); |
1361 |
< |
} catch (IllegalArgumentException success) { |
1362 |
< |
} finally { |
1363 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1357 |
> |
new CustomTPE(2, 3, |
1358 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1359 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1360 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1361 |
> |
try { |
1362 |
> |
p.setMaximumPoolSize(1); |
1363 |
> |
shouldThrow(); |
1364 |
> |
} catch (IllegalArgumentException success) {} |
1365 |
|
} |
1350 |
– |
joinPool(p); |
1366 |
|
} |
1367 |
|
|
1368 |
|
/** |
1371 |
|
*/ |
1372 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1373 |
|
final ThreadPoolExecutor p = |
1374 |
< |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1375 |
< |
try { |
1376 |
< |
p.setMaximumPoolSize(-1); |
1377 |
< |
shouldThrow(); |
1378 |
< |
} catch (IllegalArgumentException success) { |
1379 |
< |
} finally { |
1380 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1374 |
> |
new CustomTPE(2, 3, |
1375 |
> |
LONG_DELAY_MS, |
1376 |
> |
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1377 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1378 |
> |
try { |
1379 |
> |
p.setMaximumPoolSize(-1); |
1380 |
> |
shouldThrow(); |
1381 |
> |
} catch (IllegalArgumentException success) {} |
1382 |
|
} |
1367 |
– |
joinPool(p); |
1383 |
|
} |
1384 |
|
|
1385 |
|
/** |
1388 |
|
*/ |
1389 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1390 |
|
final ThreadPoolExecutor p = |
1391 |
< |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1392 |
< |
|
1393 |
< |
try { |
1394 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1395 |
< |
shouldThrow(); |
1396 |
< |
} catch (IllegalArgumentException success) { |
1397 |
< |
} finally { |
1398 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1391 |
> |
new CustomTPE(2, 3, |
1392 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1393 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1394 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1395 |
> |
try { |
1396 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1397 |
> |
shouldThrow(); |
1398 |
> |
} catch (IllegalArgumentException success) {} |
1399 |
|
} |
1385 |
– |
joinPool(p); |
1400 |
|
} |
1401 |
|
|
1402 |
|
/** |
1404 |
|
*/ |
1405 |
|
public void testTerminated() { |
1406 |
|
CustomTPE p = new CustomTPE(); |
1407 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1408 |
< |
assertTrue(p.terminatedCalled()); |
1409 |
< |
joinPool(p); |
1407 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1408 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1409 |
> |
assertTrue(p.terminatedCalled()); |
1410 |
> |
assertTrue(p.isShutdown()); |
1411 |
> |
} |
1412 |
|
} |
1413 |
|
|
1414 |
|
/** |
1416 |
|
*/ |
1417 |
|
public void testBeforeAfter() throws InterruptedException { |
1418 |
|
CustomTPE p = new CustomTPE(); |
1419 |
< |
try { |
1419 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1420 |
|
final CountDownLatch done = new CountDownLatch(1); |
1421 |
|
p.execute(new CheckedRunnable() { |
1422 |
|
public void realRun() { |
1426 |
|
assertEquals(0, done.getCount()); |
1427 |
|
assertTrue(p.afterCalled()); |
1428 |
|
assertTrue(p.beforeCalled()); |
1413 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1414 |
– |
} finally { |
1415 |
– |
joinPool(p); |
1429 |
|
} |
1430 |
|
} |
1431 |
|
|
1437 |
|
new CustomTPE(2, 2, |
1438 |
|
LONG_DELAY_MS, MILLISECONDS, |
1439 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1440 |
< |
try { |
1440 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1441 |
|
Future<String> future = e.submit(new StringTask()); |
1442 |
|
String result = future.get(); |
1443 |
|
assertSame(TEST_STRING, result); |
1431 |
– |
} finally { |
1432 |
– |
joinPool(e); |
1444 |
|
} |
1445 |
|
} |
1446 |
|
|
1452 |
|
new CustomTPE(2, 2, |
1453 |
|
LONG_DELAY_MS, MILLISECONDS, |
1454 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1455 |
< |
try { |
1455 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1456 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1457 |
|
future.get(); |
1458 |
|
assertTrue(future.isDone()); |
1448 |
– |
} finally { |
1449 |
– |
joinPool(e); |
1459 |
|
} |
1460 |
|
} |
1461 |
|
|
1467 |
|
new CustomTPE(2, 2, |
1468 |
|
LONG_DELAY_MS, MILLISECONDS, |
1469 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1470 |
< |
try { |
1470 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1471 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1472 |
|
String result = future.get(); |
1473 |
|
assertSame(TEST_STRING, result); |
1465 |
– |
} finally { |
1466 |
– |
joinPool(e); |
1474 |
|
} |
1475 |
|
} |
1476 |
|
|
1482 |
|
new CustomTPE(2, 2, |
1483 |
|
LONG_DELAY_MS, MILLISECONDS, |
1484 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1485 |
< |
try { |
1486 |
< |
e.invokeAny(null); |
1487 |
< |
shouldThrow(); |
1488 |
< |
} catch (NullPointerException success) { |
1489 |
< |
} finally { |
1483 |
< |
joinPool(e); |
1485 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1486 |
> |
try { |
1487 |
> |
e.invokeAny(null); |
1488 |
> |
shouldThrow(); |
1489 |
> |
} catch (NullPointerException success) {} |
1490 |
|
} |
1491 |
|
} |
1492 |
|
|
1498 |
|
new CustomTPE(2, 2, |
1499 |
|
LONG_DELAY_MS, MILLISECONDS, |
1500 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1501 |
< |
try { |
1502 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1503 |
< |
shouldThrow(); |
1504 |
< |
} catch (IllegalArgumentException success) { |
1505 |
< |
} finally { |
1500 |
< |
joinPool(e); |
1501 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1502 |
> |
try { |
1503 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1504 |
> |
shouldThrow(); |
1505 |
> |
} catch (IllegalArgumentException success) {} |
1506 |
|
} |
1507 |
|
} |
1508 |
|
|
1515 |
|
new CustomTPE(2, 2, |
1516 |
|
LONG_DELAY_MS, MILLISECONDS, |
1517 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1518 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1519 |
< |
l.add(latchAwaitingStringTask(latch)); |
1520 |
< |
l.add(null); |
1521 |
< |
try { |
1522 |
< |
e.invokeAny(l); |
1523 |
< |
shouldThrow(); |
1524 |
< |
} catch (NullPointerException success) { |
1525 |
< |
} finally { |
1518 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1519 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1520 |
> |
l.add(latchAwaitingStringTask(latch)); |
1521 |
> |
l.add(null); |
1522 |
> |
try { |
1523 |
> |
e.invokeAny(l); |
1524 |
> |
shouldThrow(); |
1525 |
> |
} catch (NullPointerException success) {} |
1526 |
|
latch.countDown(); |
1522 |
– |
joinPool(e); |
1527 |
|
} |
1528 |
|
} |
1529 |
|
|
1535 |
|
new CustomTPE(2, 2, |
1536 |
|
LONG_DELAY_MS, MILLISECONDS, |
1537 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1538 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1539 |
< |
l.add(new NPETask()); |
1540 |
< |
try { |
1541 |
< |
e.invokeAny(l); |
1542 |
< |
shouldThrow(); |
1543 |
< |
} catch (ExecutionException success) { |
1544 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1545 |
< |
} finally { |
1546 |
< |
joinPool(e); |
1538 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1539 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1540 |
> |
l.add(new NPETask()); |
1541 |
> |
try { |
1542 |
> |
e.invokeAny(l); |
1543 |
> |
shouldThrow(); |
1544 |
> |
} catch (ExecutionException success) { |
1545 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1546 |
> |
} |
1547 |
|
} |
1548 |
|
} |
1549 |
|
|
1555 |
|
new CustomTPE(2, 2, |
1556 |
|
LONG_DELAY_MS, MILLISECONDS, |
1557 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1558 |
< |
try { |
1558 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1559 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1560 |
|
l.add(new StringTask()); |
1561 |
|
l.add(new StringTask()); |
1562 |
|
String result = e.invokeAny(l); |
1563 |
|
assertSame(TEST_STRING, result); |
1560 |
– |
} finally { |
1561 |
– |
joinPool(e); |
1564 |
|
} |
1565 |
|
} |
1566 |
|
|
1572 |
|
new CustomTPE(2, 2, |
1573 |
|
LONG_DELAY_MS, MILLISECONDS, |
1574 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1575 |
< |
try { |
1576 |
< |
e.invokeAll(null); |
1577 |
< |
shouldThrow(); |
1578 |
< |
} catch (NullPointerException success) { |
1579 |
< |
} finally { |
1578 |
< |
joinPool(e); |
1575 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1576 |
> |
try { |
1577 |
> |
e.invokeAll(null); |
1578 |
> |
shouldThrow(); |
1579 |
> |
} catch (NullPointerException success) {} |
1580 |
|
} |
1581 |
|
} |
1582 |
|
|
1588 |
|
new CustomTPE(2, 2, |
1589 |
|
LONG_DELAY_MS, MILLISECONDS, |
1590 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1591 |
< |
try { |
1591 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1592 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1593 |
|
assertTrue(r.isEmpty()); |
1593 |
– |
} finally { |
1594 |
– |
joinPool(e); |
1594 |
|
} |
1595 |
|
} |
1596 |
|
|
1602 |
|
new CustomTPE(2, 2, |
1603 |
|
LONG_DELAY_MS, MILLISECONDS, |
1604 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1605 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1606 |
< |
l.add(new StringTask()); |
1607 |
< |
l.add(null); |
1608 |
< |
try { |
1609 |
< |
e.invokeAll(l); |
1610 |
< |
shouldThrow(); |
1611 |
< |
} catch (NullPointerException success) { |
1612 |
< |
} finally { |
1614 |
< |
joinPool(e); |
1605 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1606 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1607 |
> |
l.add(new StringTask()); |
1608 |
> |
l.add(null); |
1609 |
> |
try { |
1610 |
> |
e.invokeAll(l); |
1611 |
> |
shouldThrow(); |
1612 |
> |
} catch (NullPointerException success) {} |
1613 |
|
} |
1614 |
|
} |
1615 |
|
|
1621 |
|
new CustomTPE(2, 2, |
1622 |
|
LONG_DELAY_MS, MILLISECONDS, |
1623 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1624 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1625 |
< |
l.add(new NPETask()); |
1626 |
< |
List<Future<String>> futures = e.invokeAll(l); |
1627 |
< |
assertEquals(1, futures.size()); |
1628 |
< |
try { |
1629 |
< |
futures.get(0).get(); |
1630 |
< |
shouldThrow(); |
1631 |
< |
} catch (ExecutionException success) { |
1632 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1633 |
< |
} finally { |
1634 |
< |
joinPool(e); |
1624 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1625 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1626 |
> |
l.add(new NPETask()); |
1627 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1628 |
> |
assertEquals(1, futures.size()); |
1629 |
> |
try { |
1630 |
> |
futures.get(0).get(); |
1631 |
> |
shouldThrow(); |
1632 |
> |
} catch (ExecutionException success) { |
1633 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1634 |
> |
} |
1635 |
|
} |
1636 |
|
} |
1637 |
|
|
1643 |
|
new CustomTPE(2, 2, |
1644 |
|
LONG_DELAY_MS, MILLISECONDS, |
1645 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1646 |
< |
try { |
1646 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1647 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1648 |
|
l.add(new StringTask()); |
1649 |
|
l.add(new StringTask()); |
1651 |
|
assertEquals(2, futures.size()); |
1652 |
|
for (Future<String> future : futures) |
1653 |
|
assertSame(TEST_STRING, future.get()); |
1656 |
– |
} finally { |
1657 |
– |
joinPool(e); |
1654 |
|
} |
1655 |
|
} |
1656 |
|
|
1662 |
|
new CustomTPE(2, 2, |
1663 |
|
LONG_DELAY_MS, MILLISECONDS, |
1664 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1665 |
< |
try { |
1666 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1667 |
< |
shouldThrow(); |
1668 |
< |
} catch (NullPointerException success) { |
1669 |
< |
} finally { |
1674 |
< |
joinPool(e); |
1665 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1666 |
> |
try { |
1667 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1668 |
> |
shouldThrow(); |
1669 |
> |
} catch (NullPointerException success) {} |
1670 |
|
} |
1671 |
|
} |
1672 |
|
|
1678 |
|
new CustomTPE(2, 2, |
1679 |
|
LONG_DELAY_MS, MILLISECONDS, |
1680 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1681 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1682 |
< |
l.add(new StringTask()); |
1683 |
< |
try { |
1684 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1685 |
< |
shouldThrow(); |
1686 |
< |
} catch (NullPointerException success) { |
1687 |
< |
} finally { |
1693 |
< |
joinPool(e); |
1681 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1682 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1683 |
> |
l.add(new StringTask()); |
1684 |
> |
try { |
1685 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1686 |
> |
shouldThrow(); |
1687 |
> |
} catch (NullPointerException success) {} |
1688 |
|
} |
1689 |
|
} |
1690 |
|
|
1696 |
|
new CustomTPE(2, 2, |
1697 |
|
LONG_DELAY_MS, MILLISECONDS, |
1698 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1699 |
< |
try { |
1700 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1701 |
< |
shouldThrow(); |
1702 |
< |
} catch (IllegalArgumentException success) { |
1703 |
< |
} finally { |
1704 |
< |
joinPool(e); |
1699 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1700 |
> |
try { |
1701 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1702 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1703 |
> |
shouldThrow(); |
1704 |
> |
} catch (IllegalArgumentException success) {} |
1705 |
|
} |
1706 |
|
} |
1707 |
|
|
1714 |
|
new CustomTPE(2, 2, |
1715 |
|
LONG_DELAY_MS, MILLISECONDS, |
1716 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1717 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1718 |
< |
l.add(latchAwaitingStringTask(latch)); |
1719 |
< |
l.add(null); |
1720 |
< |
try { |
1721 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1722 |
< |
shouldThrow(); |
1723 |
< |
} catch (NullPointerException success) { |
1724 |
< |
} finally { |
1717 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1718 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1719 |
> |
l.add(latchAwaitingStringTask(latch)); |
1720 |
> |
l.add(null); |
1721 |
> |
try { |
1722 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1723 |
> |
shouldThrow(); |
1724 |
> |
} catch (NullPointerException success) {} |
1725 |
|
latch.countDown(); |
1732 |
– |
joinPool(e); |
1726 |
|
} |
1727 |
|
} |
1728 |
|
|
1734 |
|
new CustomTPE(2, 2, |
1735 |
|
LONG_DELAY_MS, MILLISECONDS, |
1736 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1737 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1738 |
< |
l.add(new NPETask()); |
1739 |
< |
try { |
1740 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1741 |
< |
shouldThrow(); |
1742 |
< |
} catch (ExecutionException success) { |
1743 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1744 |
< |
} finally { |
1745 |
< |
joinPool(e); |
1737 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1738 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1739 |
> |
l.add(new NPETask()); |
1740 |
> |
try { |
1741 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1742 |
> |
shouldThrow(); |
1743 |
> |
} catch (ExecutionException success) { |
1744 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1745 |
> |
} |
1746 |
|
} |
1747 |
|
} |
1748 |
|
|
1754 |
|
new CustomTPE(2, 2, |
1755 |
|
LONG_DELAY_MS, MILLISECONDS, |
1756 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1757 |
< |
try { |
1757 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1758 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1759 |
|
l.add(new StringTask()); |
1760 |
|
l.add(new StringTask()); |
1761 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1762 |
|
assertSame(TEST_STRING, result); |
1770 |
– |
} finally { |
1771 |
– |
joinPool(e); |
1763 |
|
} |
1764 |
|
} |
1765 |
|
|
1771 |
|
new CustomTPE(2, 2, |
1772 |
|
LONG_DELAY_MS, MILLISECONDS, |
1773 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1774 |
< |
try { |
1775 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1776 |
< |
shouldThrow(); |
1777 |
< |
} catch (NullPointerException success) { |
1778 |
< |
} finally { |
1788 |
< |
joinPool(e); |
1774 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1775 |
> |
try { |
1776 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1777 |
> |
shouldThrow(); |
1778 |
> |
} catch (NullPointerException success) {} |
1779 |
|
} |
1780 |
|
} |
1781 |
|
|
1787 |
|
new CustomTPE(2, 2, |
1788 |
|
LONG_DELAY_MS, MILLISECONDS, |
1789 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1790 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1791 |
< |
l.add(new StringTask()); |
1792 |
< |
try { |
1793 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1794 |
< |
shouldThrow(); |
1795 |
< |
} catch (NullPointerException success) { |
1796 |
< |
} finally { |
1807 |
< |
joinPool(e); |
1790 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1791 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1792 |
> |
l.add(new StringTask()); |
1793 |
> |
try { |
1794 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1795 |
> |
shouldThrow(); |
1796 |
> |
} catch (NullPointerException success) {} |
1797 |
|
} |
1798 |
|
} |
1799 |
|
|
1805 |
|
new CustomTPE(2, 2, |
1806 |
|
LONG_DELAY_MS, MILLISECONDS, |
1807 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1808 |
< |
try { |
1809 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1808 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1809 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1810 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1811 |
|
assertTrue(r.isEmpty()); |
1822 |
– |
} finally { |
1823 |
– |
joinPool(e); |
1812 |
|
} |
1813 |
|
} |
1814 |
|
|
1820 |
|
new CustomTPE(2, 2, |
1821 |
|
LONG_DELAY_MS, MILLISECONDS, |
1822 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1823 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1824 |
< |
l.add(new StringTask()); |
1825 |
< |
l.add(null); |
1826 |
< |
try { |
1827 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1828 |
< |
shouldThrow(); |
1829 |
< |
} catch (NullPointerException success) { |
1830 |
< |
} finally { |
1843 |
< |
joinPool(e); |
1823 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1824 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1825 |
> |
l.add(new StringTask()); |
1826 |
> |
l.add(null); |
1827 |
> |
try { |
1828 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1829 |
> |
shouldThrow(); |
1830 |
> |
} catch (NullPointerException success) {} |
1831 |
|
} |
1832 |
|
} |
1833 |
|
|
1839 |
|
new CustomTPE(2, 2, |
1840 |
|
LONG_DELAY_MS, MILLISECONDS, |
1841 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1842 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1843 |
< |
l.add(new NPETask()); |
1844 |
< |
List<Future<String>> futures = |
1845 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1846 |
< |
assertEquals(1, futures.size()); |
1847 |
< |
try { |
1848 |
< |
futures.get(0).get(); |
1849 |
< |
shouldThrow(); |
1850 |
< |
} catch (ExecutionException success) { |
1851 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1852 |
< |
} finally { |
1853 |
< |
joinPool(e); |
1842 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1843 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1844 |
> |
l.add(new NPETask()); |
1845 |
> |
List<Future<String>> futures = |
1846 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1847 |
> |
assertEquals(1, futures.size()); |
1848 |
> |
try { |
1849 |
> |
futures.get(0).get(); |
1850 |
> |
shouldThrow(); |
1851 |
> |
} catch (ExecutionException success) { |
1852 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1853 |
> |
} |
1854 |
|
} |
1855 |
|
} |
1856 |
|
|
1862 |
|
new CustomTPE(2, 2, |
1863 |
|
LONG_DELAY_MS, MILLISECONDS, |
1864 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1865 |
< |
try { |
1865 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1866 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1867 |
|
l.add(new StringTask()); |
1868 |
|
l.add(new StringTask()); |
1871 |
|
assertEquals(2, futures.size()); |
1872 |
|
for (Future<String> future : futures) |
1873 |
|
assertSame(TEST_STRING, future.get()); |
1887 |
– |
} finally { |
1888 |
– |
joinPool(e); |
1874 |
|
} |
1875 |
|
} |
1876 |
|
|
1882 |
|
new CustomTPE(2, 2, |
1883 |
|
LONG_DELAY_MS, MILLISECONDS, |
1884 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1885 |
< |
try { |
1885 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1886 |
|
for (long timeout = timeoutMillis();;) { |
1887 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1888 |
|
tasks.add(new StringTask("0")); |
1906 |
|
fail("expected exactly one task to be cancelled"); |
1907 |
|
} |
1908 |
|
} |
1924 |
– |
} finally { |
1925 |
– |
joinPool(e); |
1909 |
|
} |
1910 |
|
} |
1911 |
|
|
1919 |
|
LONG_DELAY_MS, MILLISECONDS, |
1920 |
|
new LinkedBlockingQueue<Runnable>(), |
1921 |
|
new FailingThreadFactory()); |
1922 |
< |
try { |
1922 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1923 |
|
final int TASKS = 100; |
1924 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1925 |
|
for (int k = 0; k < TASKS; ++k) |
1928 |
|
done.countDown(); |
1929 |
|
}}); |
1930 |
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1948 |
– |
} finally { |
1949 |
– |
joinPool(e); |
1931 |
|
} |
1932 |
|
} |
1933 |
|
|
1939 |
|
new CustomTPE(2, 2, |
1940 |
|
1000, MILLISECONDS, |
1941 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1942 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1943 |
< |
joinPool(p); |
1942 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1943 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1944 |
> |
} |
1945 |
|
} |
1946 |
|
|
1947 |
|
/** |
1953 |
|
new CustomTPE(2, 10, |
1954 |
|
keepAliveTime, MILLISECONDS, |
1955 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1956 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1957 |
< |
try { |
1956 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1957 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1958 |
|
p.allowCoreThreadTimeOut(true); |
1959 |
|
p.execute(new CheckedRunnable() { |
1960 |
|
public void realRun() { |
1969 |
|
Thread.yield(); |
1970 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1971 |
|
assertEquals(0, p.getPoolSize()); |
1990 |
– |
} finally { |
1991 |
– |
joinPool(p); |
1972 |
|
} |
1973 |
|
} |
1974 |
|
|
1981 |
|
new CustomTPE(2, 10, |
1982 |
|
keepAliveTime, MILLISECONDS, |
1983 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1984 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1985 |
< |
try { |
1984 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1985 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1986 |
|
p.allowCoreThreadTimeOut(false); |
1987 |
|
p.execute(new CheckedRunnable() { |
1988 |
|
public void realRun() throws InterruptedException { |
1991 |
|
}}); |
1992 |
|
delay(2 * keepAliveTime); |
1993 |
|
assertTrue(p.getPoolSize() >= 1); |
2014 |
– |
} finally { |
2015 |
– |
joinPool(p); |
1994 |
|
} |
1995 |
|
} |
1996 |
|
|
2003 |
|
new CustomTPE(1, 1, |
2004 |
|
LONG_DELAY_MS, MILLISECONDS, |
2005 |
|
new LinkedBlockingQueue<Runnable>()); |
2006 |
< |
try { |
2006 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
2007 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2008 |
|
final CountDownLatch done = new CountDownLatch(1); |
2009 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2030 |
|
assertTrue(future.isDone()); |
2031 |
|
} |
2032 |
|
done.countDown(); |
2055 |
– |
} finally { |
2056 |
– |
joinPool(e); |
2033 |
|
} |
2034 |
|
} |
2035 |
|
|