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 { |
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(); |
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() { |
1481 |
|
} |
1482 |
|
|
1483 |
|
/** |
1484 |
< |
* awaitQuiescent by a worker is equivalent in effect to |
1484 |
> |
* awaitQuiescence by a worker is equivalent in effect to |
1485 |
|
* ForkJoinTask.helpQuiesce() |
1486 |
|
*/ |
1487 |
< |
public void testAwaitQuiescent1() throws Exception { |
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 |
< |
boolean t = ForkJoinTask.getPool().awaitQuiescence(MEDIUM_DELAY_MS, TimeUnit.SECONDS); |
1497 |
< |
assertTrue(t); |
1498 |
< |
while (!f.isDone()) { |
1499 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1500 |
< |
threadFail("timed out"); |
1501 |
< |
assertFalse(p.getAsyncMode()); |
1502 |
< |
assertFalse(p.isShutdown()); |
1503 |
< |
assertFalse(p.isTerminating()); |
1504 |
< |
assertFalse(p.isTerminated()); |
1505 |
< |
Thread.yield(); |
1510 |
< |
} |
1511 |
< |
assertFalse(p.isQuiescent()); |
1512 |
< |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1513 |
< |
try { |
1514 |
< |
assertEquals(21, f.result); |
1515 |
< |
} catch (Throwable fail) { |
1516 |
< |
threadFail(fail.getMessage()); |
1517 |
< |
} |
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 |
< |
}; |
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()) { |
1522 |
– |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1523 |
– |
throw new AssertionFailedError("timed out"); |
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()); |
1533 |
|
} |
1534 |
|
|
1535 |
|
/** |
1536 |
< |
* awaitQuiescent returns when pool isQuiescent() or the indicated |
1537 |
< |
* timeout elapse |
1536 |
> |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1537 |
> |
* timeout elapsed |
1538 |
|
*/ |
1539 |
< |
public void testAwaitQuiescent2() throws Exception { |
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 { |
1551 |
– |
final long startTime = System.nanoTime(); |
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 |
< |
ForkJoinTask.helpQuiesce(); |
1557 |
< |
while (!f.isDone()) { |
1558 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1559 |
< |
threadFail("timed out"); |
1560 |
< |
assertFalse(p.getAsyncMode()); |
1561 |
< |
assertFalse(p.isShutdown()); |
1562 |
< |
assertFalse(p.isTerminating()); |
1564 |
< |
assertFalse(p.isTerminated()); |
1565 |
< |
Thread.yield(); |
1566 |
< |
} |
1567 |
< |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1568 |
< |
try { |
1569 |
< |
assertEquals(21, f.result); |
1570 |
< |
} catch (Throwable fail) { System.out.println("fail " + fail.getMessage()); } |
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 |
< |
}; |
1564 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1565 |
> |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1566 |
> |
assertEquals(21, f.result); |
1567 |
> |
}}; |
1568 |
|
p.execute(a); |
1569 |
< |
while (!p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) { |
1570 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1576 |
< |
threadFail("timed out"); |
1577 |
< |
assertFalse(p.getAsyncMode()); |
1578 |
< |
assertFalse(p.isShutdown()); |
1579 |
< |
assertFalse(p.isTerminating()); |
1580 |
< |
assertFalse(p.isTerminated()); |
1581 |
< |
Thread.yield(); |
1582 |
< |
} |
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()); |