ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ForkJoinPool8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ForkJoinPool8Test.java (file contents):
Revision 1.10 by jsr166, Sun Jul 14 16:36:37 2013 UTC vs.
Revision 1.28 by jsr166, Wed Dec 31 16:44:01 2014 UTC

# Line 9 | Line 9 | import java.util.concurrent.Cancellation
9   import java.util.concurrent.ExecutionException;
10   import java.util.concurrent.ForkJoinPool;
11   import java.util.concurrent.ForkJoinTask;
12 import java.util.concurrent.ForkJoinWorkerThread;
12   import java.util.concurrent.RecursiveAction;
13   import java.util.concurrent.CountedCompleter;
15 import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.TimeUnit;
14   import java.util.concurrent.TimeoutException;
15   import static java.util.concurrent.TimeUnit.SECONDS;
16   import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 import java.util.Arrays;
17   import java.util.HashSet;
18  
19   public class ForkJoinPool8Test extends JSR166TestCase {
# Line 479 | Line 475 | public class ForkJoinPool8Test extends J
475                  FailingFibAction f = new FailingFibAction(8);
476                  assertSame(f, f.fork());
477                  try {
478 <                    f.get(5L, TimeUnit.SECONDS);
478 >                    f.get(5L, SECONDS);
479                      shouldThrow();
480                  } catch (ExecutionException success) {
481                      Throwable cause = success.getCause();
# Line 1462 | Line 1458 | public class ForkJoinPool8Test extends J
1458      }
1459  
1460      /**
1461 <     * invokeAll(collection)  throws exception if any task does
1461 >     * invokeAll(collection) throws exception if any task does
1462       */
1463      public void testAbnormalInvokeAllCollectionCC() {
1464          ForkJoinTask a = new CheckedRecursiveAction() {
# Line 1484 | Line 1480 | public class ForkJoinPool8Test extends J
1480          checkInvoke(a);
1481      }
1482  
1483 +    /**
1484 +     * awaitQuiescence by a worker is equivalent in effect to
1485 +     * ForkJoinTask.helpQuiesce()
1486 +     */
1487 +    public void testAwaitQuiescence1() throws Exception {
1488 +        final ForkJoinPool p = new ForkJoinPool();
1489 +        try {
1490 +            final long startTime = System.nanoTime();
1491 +            assertTrue(p.isQuiescent());
1492 +            ForkJoinTask a = new CheckedRecursiveAction() {
1493 +                protected void realCompute() {
1494 +                    FibAction f = new FibAction(8);
1495 +                    assertSame(f, f.fork());
1496 +                    assertSame(p, ForkJoinTask.getPool());
1497 +                    boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS);
1498 +                    assertTrue(quiescent);
1499 +                    assertFalse(p.isQuiescent());
1500 +                    while (!f.isDone()) {
1501 +                        assertFalse(p.getAsyncMode());
1502 +                        assertFalse(p.isShutdown());
1503 +                        assertFalse(p.isTerminating());
1504 +                        assertFalse(p.isTerminated());
1505 +                        Thread.yield();
1506 +                    }
1507 +                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1508 +                    assertFalse(p.isQuiescent());
1509 +                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1510 +                    assertEquals(21, f.result);
1511 +                }};
1512 +            p.execute(a);
1513 +            while (!a.isDone() || !p.isQuiescent()) {
1514 +                assertFalse(p.getAsyncMode());
1515 +                assertFalse(p.isShutdown());
1516 +                assertFalse(p.isTerminating());
1517 +                assertFalse(p.isTerminated());
1518 +                Thread.yield();
1519 +            }
1520 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1521 +            assertEquals(0, p.getQueuedTaskCount());
1522 +            assertFalse(p.getAsyncMode());
1523 +            assertEquals(0, p.getActiveThreadCount());
1524 +            assertEquals(0, p.getQueuedTaskCount());
1525 +            assertEquals(0, p.getQueuedSubmissionCount());
1526 +            assertFalse(p.hasQueuedSubmissions());
1527 +            assertFalse(p.isShutdown());
1528 +            assertFalse(p.isTerminating());
1529 +            assertFalse(p.isTerminated());
1530 +        } finally {
1531 +            joinPool(p);
1532 +        }
1533 +    }
1534 +
1535 +    /**
1536 +     * awaitQuiescence returns when pool isQuiescent() or the indicated
1537 +     * timeout elapsed
1538 +     */
1539 +    public void testAwaitQuiescence2() throws Exception {
1540 +        /**
1541 +         * """It is possible to disable or limit the use of threads in the
1542 +         * common pool by setting the parallelism property to zero. However
1543 +         * doing so may cause unjoined tasks to never be executed."""
1544 +         */
1545 +        if ("0".equals(System.getProperty(
1546 +             "java.util.concurrent.ForkJoinPool.common.parallelism")))
1547 +            return;
1548 +        final ForkJoinPool p = new ForkJoinPool();
1549 +        try {
1550 +            assertTrue(p.isQuiescent());
1551 +            final long startTime = System.nanoTime();
1552 +            ForkJoinTask a = new CheckedRecursiveAction() {
1553 +                protected void realCompute() {
1554 +                    FibAction f = new FibAction(8);
1555 +                    assertSame(f, f.fork());
1556 +                    while (!f.isDone()
1557 +                           && millisElapsedSince(startTime) < LONG_DELAY_MS) {
1558 +                        assertFalse(p.getAsyncMode());
1559 +                        assertFalse(p.isShutdown());
1560 +                        assertFalse(p.isTerminating());
1561 +                        assertFalse(p.isTerminated());
1562 +                        Thread.yield();
1563 +                    }
1564 +                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1565 +                    assertEquals(0, ForkJoinTask.getQueuedTaskCount());
1566 +                    assertEquals(21, f.result);
1567 +                }};
1568 +            p.execute(a);
1569 +            assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS));
1570 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1571 +            assertTrue(p.isQuiescent());
1572 +            assertTrue(a.isDone());
1573 +            assertEquals(0, p.getQueuedTaskCount());
1574 +            assertFalse(p.getAsyncMode());
1575 +            assertEquals(0, p.getActiveThreadCount());
1576 +            assertEquals(0, p.getQueuedTaskCount());
1577 +            assertEquals(0, p.getQueuedSubmissionCount());
1578 +            assertFalse(p.hasQueuedSubmissions());
1579 +            assertFalse(p.isShutdown());
1580 +            assertFalse(p.isTerminating());
1581 +            assertFalse(p.isTerminated());
1582 +        } finally {
1583 +            joinPool(p);
1584 +        }
1585 +    }
1586 +
1587   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines