262 |
|
assertEquals(1, p.getActiveCount()); |
263 |
|
done.await(); |
264 |
|
}}); |
265 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
265 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
266 |
|
assertEquals(1, p.getActiveCount()); |
267 |
|
done.countDown(); |
268 |
|
} |
476 |
|
*/ |
477 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
478 |
|
final int THREADS = 3; |
479 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
480 |
|
final ThreadPoolExecutor p = |
481 |
|
new CustomTPE(THREADS, THREADS, |
482 |
|
LONG_DELAY_MS, MILLISECONDS, |
483 |
|
new ArrayBlockingQueue<Runnable>(10)); |
484 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
484 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
485 |
< |
final CountDownLatch done = new CountDownLatch(1); |
484 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
485 |
|
assertEquals(0, p.getLargestPoolSize()); |
486 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
487 |
|
for (int i = 0; i < THREADS; i++) |
488 |
|
p.execute(new CheckedRunnable() { |
489 |
|
public void realRun() throws InterruptedException { |
491 |
|
done.await(); |
492 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
|
}}); |
494 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
494 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
495 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
– |
done.countDown(); // release pool |
496 |
|
} |
497 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
498 |
|
} |
520 |
|
* become active |
521 |
|
*/ |
522 |
|
public void testGetPoolSize() throws InterruptedException { |
523 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
524 |
|
final ThreadPoolExecutor p = |
525 |
|
new CustomTPE(1, 1, |
526 |
|
LONG_DELAY_MS, MILLISECONDS, |
527 |
|
new ArrayBlockingQueue<Runnable>(10)); |
528 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
< |
final CountDownLatch done = new CountDownLatch(1); |
528 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
529 |
|
assertEquals(0, p.getPoolSize()); |
530 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
531 |
|
p.execute(new CheckedRunnable() { |
532 |
|
public void realRun() throws InterruptedException { |
533 |
|
threadStarted.countDown(); |
534 |
|
assertEquals(1, p.getPoolSize()); |
535 |
|
done.await(); |
536 |
|
}}); |
537 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
537 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
538 |
|
assertEquals(1, p.getPoolSize()); |
540 |
– |
done.countDown(); // release pool |
539 |
|
} |
540 |
|
} |
541 |
|
|
543 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
544 |
|
*/ |
545 |
|
public void testGetTaskCount() throws InterruptedException { |
546 |
+ |
final int TASKS = 3; |
547 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
548 |
|
final ThreadPoolExecutor p = |
549 |
|
new CustomTPE(1, 1, |
550 |
|
LONG_DELAY_MS, MILLISECONDS, |
551 |
|
new ArrayBlockingQueue<Runnable>(10)); |
552 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
552 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
553 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
– |
final CountDownLatch done = new CountDownLatch(1); |
554 |
|
assertEquals(0, p.getTaskCount()); |
555 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
556 |
|
p.execute(new CheckedRunnable() { |
557 |
|
public void realRun() throws InterruptedException { |
558 |
|
threadStarted.countDown(); |
559 |
– |
assertEquals(1, p.getTaskCount()); |
559 |
|
done.await(); |
560 |
|
}}); |
561 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
561 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
562 |
|
assertEquals(1, p.getTaskCount()); |
563 |
< |
done.countDown(); |
563 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
564 |
> |
for (int i = 0; i < TASKS; i++) { |
565 |
> |
assertEquals(1 + i, p.getTaskCount()); |
566 |
> |
p.execute(new CheckedRunnable() { |
567 |
> |
public void realRun() throws InterruptedException { |
568 |
> |
threadStarted.countDown(); |
569 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
570 |
> |
done.await(); |
571 |
> |
}}); |
572 |
> |
} |
573 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
574 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
575 |
|
} |
576 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
577 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
578 |
|
} |
579 |
|
|
580 |
|
/** |
610 |
|
threadStarted.countDown(); |
611 |
|
done.await(); |
612 |
|
}}); |
613 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
613 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
614 |
|
assertFalse(p.isTerminating()); |
615 |
|
done.countDown(); |
616 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
638 |
|
threadStarted.countDown(); |
639 |
|
done.await(); |
640 |
|
}}); |
641 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
641 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
642 |
|
assertFalse(p.isTerminating()); |
643 |
|
done.countDown(); |
644 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
672 |
|
tasks[i] = new FutureTask(task); |
673 |
|
p.execute(tasks[i]); |
674 |
|
} |
675 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
675 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
676 |
|
assertSame(q, p.getQueue()); |
677 |
|
assertFalse(q.contains(tasks[0])); |
678 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
702 |
|
}}; |
703 |
|
p.execute(tasks[i]); |
704 |
|
} |
705 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
705 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
706 |
|
assertFalse(p.remove(tasks[0])); |
707 |
|
assertTrue(q.contains(tasks[4])); |
708 |
|
assertTrue(q.contains(tasks[3])); |
727 |
|
new CustomTPE(1, 1, |
728 |
|
LONG_DELAY_MS, MILLISECONDS, |
729 |
|
q); |
730 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
730 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
731 |
|
FutureTask[] tasks = new FutureTask[5]; |
732 |
|
for (int i = 0; i < tasks.length; i++) { |
733 |
|
Callable task = new CheckedCallable<Boolean>() { |
739 |
|
tasks[i] = new FutureTask(task); |
740 |
|
p.execute(tasks[i]); |
741 |
|
} |
742 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
742 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
743 |
|
assertEquals(tasks.length, p.getTaskCount()); |
744 |
|
assertEquals(tasks.length - 1, q.size()); |
745 |
|
assertEquals(1L, p.getActiveCount()); |
752 |
|
p.purge(); // Nothing to do |
753 |
|
assertEquals(tasks.length - 3, q.size()); |
754 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
743 |
– |
done.countDown(); |
755 |
|
} |
756 |
|
} |
757 |
|
|
767 |
|
new CustomTPE(poolSize, poolSize, |
768 |
|
LONG_DELAY_MS, MILLISECONDS, |
769 |
|
new ArrayBlockingQueue<Runnable>(10)); |
770 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
770 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
771 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
772 |
|
threadsStarted.countDown(); |
773 |
|
try { |
1354 |
|
new CustomTPE(2, 3, |
1355 |
|
LONG_DELAY_MS, MILLISECONDS, |
1356 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1357 |
< |
try { |
1358 |
< |
p.setMaximumPoolSize(1); |
1359 |
< |
shouldThrow(); |
1360 |
< |
} catch (IllegalArgumentException success) { |
1361 |
< |
} finally { |
1351 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1357 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1358 |
> |
try { |
1359 |
> |
p.setMaximumPoolSize(1); |
1360 |
> |
shouldThrow(); |
1361 |
> |
} catch (IllegalArgumentException success) {} |
1362 |
|
} |
1353 |
– |
joinPool(p); |
1363 |
|
} |
1364 |
|
|
1365 |
|
/** |
1371 |
|
new CustomTPE(2, 3, |
1372 |
|
LONG_DELAY_MS, |
1373 |
|
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1374 |
< |
try { |
1375 |
< |
p.setMaximumPoolSize(-1); |
1376 |
< |
shouldThrow(); |
1377 |
< |
} catch (IllegalArgumentException success) { |
1378 |
< |
} finally { |
1370 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1374 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1375 |
> |
try { |
1376 |
> |
p.setMaximumPoolSize(-1); |
1377 |
> |
shouldThrow(); |
1378 |
> |
} catch (IllegalArgumentException success) {} |
1379 |
|
} |
1372 |
– |
joinPool(p); |
1380 |
|
} |
1381 |
|
|
1382 |
|
/** |
1388 |
|
new CustomTPE(2, 3, |
1389 |
|
LONG_DELAY_MS, MILLISECONDS, |
1390 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1391 |
< |
|
1392 |
< |
try { |
1393 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1394 |
< |
shouldThrow(); |
1395 |
< |
} catch (IllegalArgumentException success) { |
1389 |
< |
} finally { |
1390 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1391 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1392 |
> |
try { |
1393 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1394 |
> |
shouldThrow(); |
1395 |
> |
} catch (IllegalArgumentException success) {} |
1396 |
|
} |
1392 |
– |
joinPool(p); |
1397 |
|
} |
1398 |
|
|
1399 |
|
/** |
1401 |
|
*/ |
1402 |
|
public void testTerminated() { |
1403 |
|
CustomTPE p = new CustomTPE(); |
1404 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1405 |
< |
assertTrue(p.terminatedCalled()); |
1406 |
< |
joinPool(p); |
1404 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1405 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1406 |
> |
assertTrue(p.terminatedCalled()); |
1407 |
> |
assertTrue(p.isShutdown()); |
1408 |
> |
} |
1409 |
|
} |
1410 |
|
|
1411 |
|
/** |
1413 |
|
*/ |
1414 |
|
public void testBeforeAfter() throws InterruptedException { |
1415 |
|
CustomTPE p = new CustomTPE(); |
1416 |
< |
try { |
1416 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1417 |
|
final CountDownLatch done = new CountDownLatch(1); |
1418 |
|
p.execute(new CheckedRunnable() { |
1419 |
|
public void realRun() { |
1423 |
|
assertEquals(0, done.getCount()); |
1424 |
|
assertTrue(p.afterCalled()); |
1425 |
|
assertTrue(p.beforeCalled()); |
1420 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1421 |
– |
} finally { |
1422 |
– |
joinPool(p); |
1426 |
|
} |
1427 |
|
} |
1428 |
|
|
1434 |
|
new CustomTPE(2, 2, |
1435 |
|
LONG_DELAY_MS, MILLISECONDS, |
1436 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1437 |
< |
try { |
1437 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1438 |
|
Future<String> future = e.submit(new StringTask()); |
1439 |
|
String result = future.get(); |
1440 |
|
assertSame(TEST_STRING, result); |
1438 |
– |
} finally { |
1439 |
– |
joinPool(e); |
1441 |
|
} |
1442 |
|
} |
1443 |
|
|
1449 |
|
new CustomTPE(2, 2, |
1450 |
|
LONG_DELAY_MS, MILLISECONDS, |
1451 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1452 |
< |
try { |
1452 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1453 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1454 |
|
future.get(); |
1455 |
|
assertTrue(future.isDone()); |
1455 |
– |
} finally { |
1456 |
– |
joinPool(e); |
1456 |
|
} |
1457 |
|
} |
1458 |
|
|
1464 |
|
new CustomTPE(2, 2, |
1465 |
|
LONG_DELAY_MS, MILLISECONDS, |
1466 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1467 |
< |
try { |
1467 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1468 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1469 |
|
String result = future.get(); |
1470 |
|
assertSame(TEST_STRING, result); |
1472 |
– |
} finally { |
1473 |
– |
joinPool(e); |
1471 |
|
} |
1472 |
|
} |
1473 |
|
|
1479 |
|
new CustomTPE(2, 2, |
1480 |
|
LONG_DELAY_MS, MILLISECONDS, |
1481 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1482 |
< |
try { |
1483 |
< |
e.invokeAny(null); |
1484 |
< |
shouldThrow(); |
1485 |
< |
} catch (NullPointerException success) { |
1486 |
< |
} finally { |
1490 |
< |
joinPool(e); |
1482 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1483 |
> |
try { |
1484 |
> |
e.invokeAny(null); |
1485 |
> |
shouldThrow(); |
1486 |
> |
} catch (NullPointerException success) {} |
1487 |
|
} |
1488 |
|
} |
1489 |
|
|
1495 |
|
new CustomTPE(2, 2, |
1496 |
|
LONG_DELAY_MS, MILLISECONDS, |
1497 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1498 |
< |
try { |
1499 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1500 |
< |
shouldThrow(); |
1501 |
< |
} catch (IllegalArgumentException success) { |
1502 |
< |
} finally { |
1507 |
< |
joinPool(e); |
1498 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1499 |
> |
try { |
1500 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1501 |
> |
shouldThrow(); |
1502 |
> |
} catch (IllegalArgumentException success) {} |
1503 |
|
} |
1504 |
|
} |
1505 |
|
|
1512 |
|
new CustomTPE(2, 2, |
1513 |
|
LONG_DELAY_MS, MILLISECONDS, |
1514 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1515 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1516 |
< |
l.add(latchAwaitingStringTask(latch)); |
1517 |
< |
l.add(null); |
1518 |
< |
try { |
1519 |
< |
e.invokeAny(l); |
1520 |
< |
shouldThrow(); |
1521 |
< |
} catch (NullPointerException success) { |
1522 |
< |
} finally { |
1515 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1516 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1517 |
> |
l.add(latchAwaitingStringTask(latch)); |
1518 |
> |
l.add(null); |
1519 |
> |
try { |
1520 |
> |
e.invokeAny(l); |
1521 |
> |
shouldThrow(); |
1522 |
> |
} catch (NullPointerException success) {} |
1523 |
|
latch.countDown(); |
1529 |
– |
joinPool(e); |
1524 |
|
} |
1525 |
|
} |
1526 |
|
|
1532 |
|
new CustomTPE(2, 2, |
1533 |
|
LONG_DELAY_MS, MILLISECONDS, |
1534 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1535 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1536 |
< |
l.add(new NPETask()); |
1537 |
< |
try { |
1538 |
< |
e.invokeAny(l); |
1539 |
< |
shouldThrow(); |
1540 |
< |
} catch (ExecutionException success) { |
1541 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1542 |
< |
} finally { |
1543 |
< |
joinPool(e); |
1535 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1536 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1537 |
> |
l.add(new NPETask()); |
1538 |
> |
try { |
1539 |
> |
e.invokeAny(l); |
1540 |
> |
shouldThrow(); |
1541 |
> |
} catch (ExecutionException success) { |
1542 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1543 |
> |
} |
1544 |
|
} |
1545 |
|
} |
1546 |
|
|
1552 |
|
new CustomTPE(2, 2, |
1553 |
|
LONG_DELAY_MS, MILLISECONDS, |
1554 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1555 |
< |
try { |
1555 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1556 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1557 |
|
l.add(new StringTask()); |
1558 |
|
l.add(new StringTask()); |
1559 |
|
String result = e.invokeAny(l); |
1560 |
|
assertSame(TEST_STRING, result); |
1567 |
– |
} finally { |
1568 |
– |
joinPool(e); |
1561 |
|
} |
1562 |
|
} |
1563 |
|
|
1569 |
|
new CustomTPE(2, 2, |
1570 |
|
LONG_DELAY_MS, MILLISECONDS, |
1571 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1572 |
< |
try { |
1573 |
< |
e.invokeAll(null); |
1574 |
< |
shouldThrow(); |
1575 |
< |
} catch (NullPointerException success) { |
1576 |
< |
} finally { |
1585 |
< |
joinPool(e); |
1572 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1573 |
> |
try { |
1574 |
> |
e.invokeAll(null); |
1575 |
> |
shouldThrow(); |
1576 |
> |
} catch (NullPointerException success) {} |
1577 |
|
} |
1578 |
|
} |
1579 |
|
|
1585 |
|
new CustomTPE(2, 2, |
1586 |
|
LONG_DELAY_MS, MILLISECONDS, |
1587 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1588 |
< |
try { |
1588 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1589 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1590 |
|
assertTrue(r.isEmpty()); |
1600 |
– |
} finally { |
1601 |
– |
joinPool(e); |
1591 |
|
} |
1592 |
|
} |
1593 |
|
|
1599 |
|
new CustomTPE(2, 2, |
1600 |
|
LONG_DELAY_MS, MILLISECONDS, |
1601 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1602 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1603 |
< |
l.add(new StringTask()); |
1604 |
< |
l.add(null); |
1605 |
< |
try { |
1606 |
< |
e.invokeAll(l); |
1607 |
< |
shouldThrow(); |
1608 |
< |
} catch (NullPointerException success) { |
1609 |
< |
} finally { |
1621 |
< |
joinPool(e); |
1602 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1603 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1604 |
> |
l.add(new StringTask()); |
1605 |
> |
l.add(null); |
1606 |
> |
try { |
1607 |
> |
e.invokeAll(l); |
1608 |
> |
shouldThrow(); |
1609 |
> |
} catch (NullPointerException success) {} |
1610 |
|
} |
1611 |
|
} |
1612 |
|
|
1618 |
|
new CustomTPE(2, 2, |
1619 |
|
LONG_DELAY_MS, MILLISECONDS, |
1620 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1622 |
< |
l.add(new NPETask()); |
1623 |
< |
List<Future<String>> futures = e.invokeAll(l); |
1624 |
< |
assertEquals(1, futures.size()); |
1625 |
< |
try { |
1626 |
< |
futures.get(0).get(); |
1627 |
< |
shouldThrow(); |
1628 |
< |
} catch (ExecutionException success) { |
1629 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1630 |
< |
} finally { |
1631 |
< |
joinPool(e); |
1621 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1622 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1623 |
> |
l.add(new NPETask()); |
1624 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1625 |
> |
assertEquals(1, futures.size()); |
1626 |
> |
try { |
1627 |
> |
futures.get(0).get(); |
1628 |
> |
shouldThrow(); |
1629 |
> |
} catch (ExecutionException success) { |
1630 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1631 |
> |
} |
1632 |
|
} |
1633 |
|
} |
1634 |
|
|
1640 |
|
new CustomTPE(2, 2, |
1641 |
|
LONG_DELAY_MS, MILLISECONDS, |
1642 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1643 |
< |
try { |
1643 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1644 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1645 |
|
l.add(new StringTask()); |
1646 |
|
l.add(new StringTask()); |
1648 |
|
assertEquals(2, futures.size()); |
1649 |
|
for (Future<String> future : futures) |
1650 |
|
assertSame(TEST_STRING, future.get()); |
1663 |
– |
} finally { |
1664 |
– |
joinPool(e); |
1651 |
|
} |
1652 |
|
} |
1653 |
|
|
1659 |
|
new CustomTPE(2, 2, |
1660 |
|
LONG_DELAY_MS, MILLISECONDS, |
1661 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1662 |
< |
try { |
1663 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1664 |
< |
shouldThrow(); |
1665 |
< |
} catch (NullPointerException success) { |
1666 |
< |
} finally { |
1681 |
< |
joinPool(e); |
1662 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1663 |
> |
try { |
1664 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1665 |
> |
shouldThrow(); |
1666 |
> |
} catch (NullPointerException success) {} |
1667 |
|
} |
1668 |
|
} |
1669 |
|
|
1675 |
|
new CustomTPE(2, 2, |
1676 |
|
LONG_DELAY_MS, MILLISECONDS, |
1677 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1678 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1679 |
< |
l.add(new StringTask()); |
1680 |
< |
try { |
1681 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1682 |
< |
shouldThrow(); |
1683 |
< |
} catch (NullPointerException success) { |
1684 |
< |
} finally { |
1700 |
< |
joinPool(e); |
1678 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1679 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1680 |
> |
l.add(new StringTask()); |
1681 |
> |
try { |
1682 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1683 |
> |
shouldThrow(); |
1684 |
> |
} catch (NullPointerException success) {} |
1685 |
|
} |
1686 |
|
} |
1687 |
|
|
1693 |
|
new CustomTPE(2, 2, |
1694 |
|
LONG_DELAY_MS, MILLISECONDS, |
1695 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1696 |
< |
try { |
1697 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1698 |
< |
shouldThrow(); |
1699 |
< |
} catch (IllegalArgumentException success) { |
1700 |
< |
} finally { |
1701 |
< |
joinPool(e); |
1696 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1697 |
> |
try { |
1698 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1699 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1700 |
> |
shouldThrow(); |
1701 |
> |
} catch (IllegalArgumentException success) {} |
1702 |
|
} |
1703 |
|
} |
1704 |
|
|
1711 |
|
new CustomTPE(2, 2, |
1712 |
|
LONG_DELAY_MS, MILLISECONDS, |
1713 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1714 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1715 |
< |
l.add(latchAwaitingStringTask(latch)); |
1716 |
< |
l.add(null); |
1717 |
< |
try { |
1718 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1719 |
< |
shouldThrow(); |
1720 |
< |
} catch (NullPointerException success) { |
1721 |
< |
} finally { |
1714 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1715 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1716 |
> |
l.add(latchAwaitingStringTask(latch)); |
1717 |
> |
l.add(null); |
1718 |
> |
try { |
1719 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1720 |
> |
shouldThrow(); |
1721 |
> |
} catch (NullPointerException success) {} |
1722 |
|
latch.countDown(); |
1739 |
– |
joinPool(e); |
1723 |
|
} |
1724 |
|
} |
1725 |
|
|
1731 |
|
new CustomTPE(2, 2, |
1732 |
|
LONG_DELAY_MS, MILLISECONDS, |
1733 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1734 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1735 |
< |
l.add(new NPETask()); |
1736 |
< |
try { |
1737 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1738 |
< |
shouldThrow(); |
1739 |
< |
} catch (ExecutionException success) { |
1740 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1741 |
< |
} finally { |
1742 |
< |
joinPool(e); |
1734 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1735 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1736 |
> |
l.add(new NPETask()); |
1737 |
> |
try { |
1738 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1739 |
> |
shouldThrow(); |
1740 |
> |
} catch (ExecutionException success) { |
1741 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1742 |
> |
} |
1743 |
|
} |
1744 |
|
} |
1745 |
|
|
1751 |
|
new CustomTPE(2, 2, |
1752 |
|
LONG_DELAY_MS, MILLISECONDS, |
1753 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1754 |
< |
try { |
1754 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1755 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1756 |
|
l.add(new StringTask()); |
1757 |
|
l.add(new StringTask()); |
1758 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1759 |
|
assertSame(TEST_STRING, result); |
1777 |
– |
} finally { |
1778 |
– |
joinPool(e); |
1760 |
|
} |
1761 |
|
} |
1762 |
|
|
1768 |
|
new CustomTPE(2, 2, |
1769 |
|
LONG_DELAY_MS, MILLISECONDS, |
1770 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1771 |
< |
try { |
1772 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1773 |
< |
shouldThrow(); |
1774 |
< |
} catch (NullPointerException success) { |
1775 |
< |
} finally { |
1795 |
< |
joinPool(e); |
1771 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1772 |
> |
try { |
1773 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1774 |
> |
shouldThrow(); |
1775 |
> |
} catch (NullPointerException success) {} |
1776 |
|
} |
1777 |
|
} |
1778 |
|
|
1784 |
|
new CustomTPE(2, 2, |
1785 |
|
LONG_DELAY_MS, MILLISECONDS, |
1786 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1787 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1788 |
< |
l.add(new StringTask()); |
1789 |
< |
try { |
1790 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1791 |
< |
shouldThrow(); |
1792 |
< |
} catch (NullPointerException success) { |
1793 |
< |
} finally { |
1814 |
< |
joinPool(e); |
1787 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1788 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1789 |
> |
l.add(new StringTask()); |
1790 |
> |
try { |
1791 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1792 |
> |
shouldThrow(); |
1793 |
> |
} catch (NullPointerException success) {} |
1794 |
|
} |
1795 |
|
} |
1796 |
|
|
1802 |
|
new CustomTPE(2, 2, |
1803 |
|
LONG_DELAY_MS, MILLISECONDS, |
1804 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1805 |
< |
try { |
1806 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1805 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1806 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1807 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1808 |
|
assertTrue(r.isEmpty()); |
1829 |
– |
} finally { |
1830 |
– |
joinPool(e); |
1809 |
|
} |
1810 |
|
} |
1811 |
|
|
1817 |
|
new CustomTPE(2, 2, |
1818 |
|
LONG_DELAY_MS, MILLISECONDS, |
1819 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1820 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1821 |
< |
l.add(new StringTask()); |
1822 |
< |
l.add(null); |
1823 |
< |
try { |
1824 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1825 |
< |
shouldThrow(); |
1826 |
< |
} catch (NullPointerException success) { |
1827 |
< |
} finally { |
1850 |
< |
joinPool(e); |
1820 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1821 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1822 |
> |
l.add(new StringTask()); |
1823 |
> |
l.add(null); |
1824 |
> |
try { |
1825 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1826 |
> |
shouldThrow(); |
1827 |
> |
} catch (NullPointerException success) {} |
1828 |
|
} |
1829 |
|
} |
1830 |
|
|
1836 |
|
new CustomTPE(2, 2, |
1837 |
|
LONG_DELAY_MS, MILLISECONDS, |
1838 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1839 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1840 |
< |
l.add(new NPETask()); |
1841 |
< |
List<Future<String>> futures = |
1842 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1843 |
< |
assertEquals(1, futures.size()); |
1844 |
< |
try { |
1845 |
< |
futures.get(0).get(); |
1846 |
< |
shouldThrow(); |
1847 |
< |
} catch (ExecutionException success) { |
1848 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1849 |
< |
} finally { |
1850 |
< |
joinPool(e); |
1839 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1840 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1841 |
> |
l.add(new NPETask()); |
1842 |
> |
List<Future<String>> futures = |
1843 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1844 |
> |
assertEquals(1, futures.size()); |
1845 |
> |
try { |
1846 |
> |
futures.get(0).get(); |
1847 |
> |
shouldThrow(); |
1848 |
> |
} catch (ExecutionException success) { |
1849 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1850 |
> |
} |
1851 |
|
} |
1852 |
|
} |
1853 |
|
|
1859 |
|
new CustomTPE(2, 2, |
1860 |
|
LONG_DELAY_MS, MILLISECONDS, |
1861 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1862 |
< |
try { |
1862 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1863 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1864 |
|
l.add(new StringTask()); |
1865 |
|
l.add(new StringTask()); |
1868 |
|
assertEquals(2, futures.size()); |
1869 |
|
for (Future<String> future : futures) |
1870 |
|
assertSame(TEST_STRING, future.get()); |
1894 |
– |
} finally { |
1895 |
– |
joinPool(e); |
1871 |
|
} |
1872 |
|
} |
1873 |
|
|
1879 |
|
new CustomTPE(2, 2, |
1880 |
|
LONG_DELAY_MS, MILLISECONDS, |
1881 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1882 |
< |
try { |
1882 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1883 |
|
for (long timeout = timeoutMillis();;) { |
1884 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1885 |
|
tasks.add(new StringTask("0")); |
1903 |
|
fail("expected exactly one task to be cancelled"); |
1904 |
|
} |
1905 |
|
} |
1931 |
– |
} finally { |
1932 |
– |
joinPool(e); |
1906 |
|
} |
1907 |
|
} |
1908 |
|
|
1916 |
|
LONG_DELAY_MS, MILLISECONDS, |
1917 |
|
new LinkedBlockingQueue<Runnable>(), |
1918 |
|
new FailingThreadFactory()); |
1919 |
< |
try { |
1919 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1920 |
|
final int TASKS = 100; |
1921 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1922 |
|
for (int k = 0; k < TASKS; ++k) |
1925 |
|
done.countDown(); |
1926 |
|
}}); |
1927 |
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1955 |
– |
} finally { |
1956 |
– |
joinPool(e); |
1928 |
|
} |
1929 |
|
} |
1930 |
|
|
1936 |
|
new CustomTPE(2, 2, |
1937 |
|
1000, MILLISECONDS, |
1938 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1939 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1940 |
< |
joinPool(p); |
1939 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1940 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1941 |
> |
} |
1942 |
|
} |
1943 |
|
|
1944 |
|
/** |
1950 |
|
new CustomTPE(2, 10, |
1951 |
|
keepAliveTime, MILLISECONDS, |
1952 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1953 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1954 |
< |
try { |
1953 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1954 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1955 |
|
p.allowCoreThreadTimeOut(true); |
1956 |
|
p.execute(new CheckedRunnable() { |
1957 |
|
public void realRun() { |
1966 |
|
Thread.yield(); |
1967 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1968 |
|
assertEquals(0, p.getPoolSize()); |
1997 |
– |
} finally { |
1998 |
– |
joinPool(p); |
1969 |
|
} |
1970 |
|
} |
1971 |
|
|
1978 |
|
new CustomTPE(2, 10, |
1979 |
|
keepAliveTime, MILLISECONDS, |
1980 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1981 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1982 |
< |
try { |
1981 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1982 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1983 |
|
p.allowCoreThreadTimeOut(false); |
1984 |
|
p.execute(new CheckedRunnable() { |
1985 |
|
public void realRun() throws InterruptedException { |
1988 |
|
}}); |
1989 |
|
delay(2 * keepAliveTime); |
1990 |
|
assertTrue(p.getPoolSize() >= 1); |
2021 |
– |
} finally { |
2022 |
– |
joinPool(p); |
1991 |
|
} |
1992 |
|
} |
1993 |
|
|
2000 |
|
new CustomTPE(1, 1, |
2001 |
|
LONG_DELAY_MS, MILLISECONDS, |
2002 |
|
new LinkedBlockingQueue<Runnable>()); |
2003 |
< |
try { |
2003 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
2004 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2005 |
|
final CountDownLatch done = new CountDownLatch(1); |
2006 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2027 |
|
assertTrue(future.isDone()); |
2028 |
|
} |
2029 |
|
done.countDown(); |
2062 |
– |
} finally { |
2063 |
– |
joinPool(e); |
2030 |
|
} |
2031 |
|
} |
2032 |
|
|