4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
9 |
> |
|
10 |
> |
import java.util.HashSet; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
+ |
import java.util.concurrent.CountedCompleter; |
13 |
|
import java.util.concurrent.ExecutionException; |
14 |
|
import java.util.concurrent.ForkJoinPool; |
15 |
|
import java.util.concurrent.ForkJoinTask; |
12 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
16 |
|
import java.util.concurrent.RecursiveAction; |
14 |
– |
import java.util.concurrent.CountedCompleter; |
15 |
– |
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
20 |
< |
import java.util.Arrays; |
21 |
< |
import java.util.HashSet; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
> |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class ForkJoinPool8Test extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
478 |
|
FailingFibAction f = new FailingFibAction(8); |
479 |
|
assertSame(f, f.fork()); |
480 |
|
try { |
481 |
< |
f.get(5L, TimeUnit.SECONDS); |
481 |
> |
f.get(5L, SECONDS); |
482 |
|
shouldThrow(); |
483 |
|
} catch (ExecutionException success) { |
484 |
|
Throwable cause = success.getCause(); |
1461 |
|
} |
1462 |
|
|
1463 |
|
/** |
1464 |
< |
* invokeAll(collection) throws exception if any task does |
1464 |
> |
* invokeAll(collection) throws exception if any task does |
1465 |
|
*/ |
1466 |
|
public void testAbnormalInvokeAllCollectionCC() { |
1467 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1484 |
|
} |
1485 |
|
|
1486 |
|
/** |
1487 |
< |
* awaitQuiescent by a worker is equivalent in effect to |
1487 |
> |
* awaitQuiescence by a worker is equivalent in effect to |
1488 |
|
* ForkJoinTask.helpQuiesce() |
1489 |
|
*/ |
1490 |
< |
public void testAwaitQuiescent1() throws Exception { |
1490 |
> |
public void testAwaitQuiescence1() throws Exception { |
1491 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1492 |
|
try { |
1493 |
|
final long startTime = System.nanoTime(); |
1494 |
|
assertTrue(p.isQuiescent()); |
1495 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1496 |
< |
protected void realCompute() { |
1497 |
< |
FibAction f = new FibAction(8); |
1498 |
< |
assertSame(f, f.fork()); |
1499 |
< |
boolean t = ForkJoinTask.getPool().awaitQuiescence(MEDIUM_DELAY_MS, TimeUnit.SECONDS); |
1500 |
< |
assertTrue(t); |
1501 |
< |
while (!f.isDone()) { |
1502 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1503 |
< |
threadFail("timed out"); |
1504 |
< |
assertFalse(p.getAsyncMode()); |
1505 |
< |
assertFalse(p.isShutdown()); |
1506 |
< |
assertFalse(p.isTerminating()); |
1507 |
< |
assertFalse(p.isTerminated()); |
1508 |
< |
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 |
< |
} |
1496 |
> |
protected void realCompute() { |
1497 |
> |
FibAction f = new FibAction(8); |
1498 |
> |
assertSame(f, f.fork()); |
1499 |
> |
assertSame(p, ForkJoinTask.getPool()); |
1500 |
> |
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); |
1501 |
> |
assertTrue(quiescent); |
1502 |
> |
assertFalse(p.isQuiescent()); |
1503 |
> |
while (!f.isDone()) { |
1504 |
> |
assertFalse(p.getAsyncMode()); |
1505 |
> |
assertFalse(p.isShutdown()); |
1506 |
> |
assertFalse(p.isTerminating()); |
1507 |
> |
assertFalse(p.isTerminated()); |
1508 |
> |
Thread.yield(); |
1509 |
|
} |
1510 |
< |
}; |
1510 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1511 |
> |
assertFalse(p.isQuiescent()); |
1512 |
> |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1513 |
> |
assertEquals(21, f.result); |
1514 |
> |
}}; |
1515 |
|
p.execute(a); |
1516 |
|
while (!a.isDone() || !p.isQuiescent()) { |
1522 |
– |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1523 |
– |
throw new AssertionFailedError("timed out"); |
1517 |
|
assertFalse(p.getAsyncMode()); |
1518 |
|
assertFalse(p.isShutdown()); |
1519 |
|
assertFalse(p.isTerminating()); |
1520 |
|
assertFalse(p.isTerminated()); |
1521 |
|
Thread.yield(); |
1522 |
|
} |
1523 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1524 |
|
assertEquals(0, p.getQueuedTaskCount()); |
1525 |
|
assertFalse(p.getAsyncMode()); |
1526 |
|
assertEquals(0, p.getActiveThreadCount()); |
1536 |
|
} |
1537 |
|
|
1538 |
|
/** |
1539 |
< |
* awaitQuiescent returns when pool isQuiescent() or the indicated |
1540 |
< |
* timeout elapse |
1539 |
> |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1540 |
> |
* timeout elapsed |
1541 |
|
*/ |
1542 |
< |
public void testAwaitQuiescent2() throws Exception { |
1542 |
> |
public void testAwaitQuiescence2() throws Exception { |
1543 |
> |
/** |
1544 |
> |
* """It is possible to disable or limit the use of threads in the |
1545 |
> |
* common pool by setting the parallelism property to zero. However |
1546 |
> |
* doing so may cause unjoined tasks to never be executed.""" |
1547 |
> |
*/ |
1548 |
> |
if ("0".equals(System.getProperty( |
1549 |
> |
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1550 |
> |
return; |
1551 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1552 |
|
try { |
1551 |
– |
final long startTime = System.nanoTime(); |
1553 |
|
assertTrue(p.isQuiescent()); |
1554 |
+ |
final long startTime = System.nanoTime(); |
1555 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1556 |
< |
protected void realCompute() { |
1557 |
< |
FibAction f = new FibAction(8); |
1558 |
< |
assertSame(f, f.fork()); |
1559 |
< |
ForkJoinTask.helpQuiesce(); |
1560 |
< |
while (!f.isDone()) { |
1561 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1562 |
< |
threadFail("timed out"); |
1563 |
< |
assertFalse(p.getAsyncMode()); |
1564 |
< |
assertFalse(p.isShutdown()); |
1565 |
< |
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()); } |
1556 |
> |
protected void realCompute() { |
1557 |
> |
FibAction f = new FibAction(8); |
1558 |
> |
assertSame(f, f.fork()); |
1559 |
> |
while (!f.isDone() |
1560 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) { |
1561 |
> |
assertFalse(p.getAsyncMode()); |
1562 |
> |
assertFalse(p.isShutdown()); |
1563 |
> |
assertFalse(p.isTerminating()); |
1564 |
> |
assertFalse(p.isTerminated()); |
1565 |
> |
Thread.yield(); |
1566 |
|
} |
1567 |
< |
}; |
1567 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1568 |
> |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1569 |
> |
assertEquals(21, f.result); |
1570 |
> |
}}; |
1571 |
|
p.execute(a); |
1572 |
< |
while (!p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) { |
1573 |
< |
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 |
< |
} |
1572 |
> |
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1573 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1574 |
|
assertTrue(p.isQuiescent()); |
1575 |
|
assertTrue(a.isDone()); |
1576 |
|
assertEquals(0, p.getQueuedTaskCount()); |