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() { |
269 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
270 |
|
protected void realCompute() { |
271 |
|
FibAction f = new FibAction(8); |
272 |
< |
final Thread myself = Thread.currentThread(); |
272 |
> |
final Thread currentThread = Thread.currentThread(); |
273 |
|
|
274 |
|
// test join() |
275 |
|
assertSame(f, f.fork()); |
276 |
< |
myself.interrupt(); |
278 |
< |
assertTrue(myself.isInterrupted()); |
276 |
> |
currentThread.interrupt(); |
277 |
|
assertNull(f.join()); |
278 |
|
Thread.interrupted(); |
279 |
|
assertEquals(21, f.result); |
282 |
|
f = new FibAction(8); |
283 |
|
f.cancel(true); |
284 |
|
assertSame(f, f.fork()); |
285 |
< |
myself.interrupt(); |
288 |
< |
assertTrue(myself.isInterrupted()); |
285 |
> |
currentThread.interrupt(); |
286 |
|
try { |
287 |
|
f.join(); |
288 |
|
shouldThrow(); |
294 |
|
f = new FibAction(8); |
295 |
|
f.completeExceptionally(new FJException()); |
296 |
|
assertSame(f, f.fork()); |
297 |
< |
myself.interrupt(); |
301 |
< |
assertTrue(myself.isInterrupted()); |
297 |
> |
currentThread.interrupt(); |
298 |
|
try { |
299 |
|
f.join(); |
300 |
|
shouldThrow(); |
306 |
|
// test quietlyJoin() |
307 |
|
f = new FibAction(8); |
308 |
|
assertSame(f, f.fork()); |
309 |
< |
myself.interrupt(); |
314 |
< |
assertTrue(myself.isInterrupted()); |
309 |
> |
currentThread.interrupt(); |
310 |
|
f.quietlyJoin(); |
311 |
|
Thread.interrupted(); |
312 |
|
assertEquals(21, f.result); |
315 |
|
f = new FibAction(8); |
316 |
|
f.cancel(true); |
317 |
|
assertSame(f, f.fork()); |
318 |
< |
myself.interrupt(); |
324 |
< |
assertTrue(myself.isInterrupted()); |
318 |
> |
currentThread.interrupt(); |
319 |
|
f.quietlyJoin(); |
320 |
|
Thread.interrupted(); |
321 |
|
checkCancelled(f); |
323 |
|
f = new FibAction(8); |
324 |
|
f.completeExceptionally(new FJException()); |
325 |
|
assertSame(f, f.fork()); |
326 |
< |
myself.interrupt(); |
333 |
< |
assertTrue(myself.isInterrupted()); |
326 |
> |
currentThread.interrupt(); |
327 |
|
f.quietlyJoin(); |
328 |
|
Thread.interrupted(); |
329 |
|
checkCompletedAbnormally(f, f.getException()); |
472 |
|
FailingFibAction f = new FailingFibAction(8); |
473 |
|
assertSame(f, f.fork()); |
474 |
|
try { |
475 |
< |
f.get(5L, TimeUnit.SECONDS); |
475 |
> |
f.get(5L, SECONDS); |
476 |
|
shouldThrow(); |
477 |
|
} catch (ExecutionException success) { |
478 |
|
Throwable cause = success.getCause(); |
1478 |
|
} |
1479 |
|
|
1480 |
|
/** |
1481 |
< |
* awaitQuiescent by a worker is equivalent in effect to |
1481 |
> |
* awaitQuiescence by a worker is equivalent in effect to |
1482 |
|
* ForkJoinTask.helpQuiesce() |
1483 |
|
*/ |
1484 |
< |
public void testAwaitQuiescent1() throws Exception { |
1484 |
> |
public void testAwaitQuiescence1() throws Exception { |
1485 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1486 |
< |
try { |
1486 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1487 |
|
final long startTime = System.nanoTime(); |
1488 |
|
assertTrue(p.isQuiescent()); |
1489 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1490 |
|
protected void realCompute() { |
1491 |
|
FibAction f = new FibAction(8); |
1492 |
|
assertSame(f, f.fork()); |
1493 |
< |
boolean t = ForkJoinTask.getPool().awaitQuiescence(MEDIUM_DELAY_MS, TimeUnit.SECONDS); |
1494 |
< |
assertTrue(t); |
1493 |
> |
assertSame(p, ForkJoinTask.getPool()); |
1494 |
> |
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); |
1495 |
> |
assertTrue(quiescent); |
1496 |
> |
assertFalse(p.isQuiescent()); |
1497 |
|
while (!f.isDone()) { |
1503 |
– |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1504 |
– |
threadFail("timed out"); |
1498 |
|
assertFalse(p.getAsyncMode()); |
1499 |
|
assertFalse(p.isShutdown()); |
1500 |
|
assertFalse(p.isTerminating()); |
1501 |
|
assertFalse(p.isTerminated()); |
1502 |
|
Thread.yield(); |
1503 |
|
} |
1504 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1505 |
|
assertFalse(p.isQuiescent()); |
1506 |
|
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1507 |
< |
try { |
1514 |
< |
assertEquals(21, f.result); |
1515 |
< |
} catch (Throwable fail) { |
1516 |
< |
threadFail(fail.getMessage()); |
1517 |
< |
} |
1507 |
> |
assertEquals(21, f.result); |
1508 |
|
}}; |
1509 |
|
p.execute(a); |
1510 |
|
while (!a.isDone() || !p.isQuiescent()) { |
1521 |
– |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1522 |
– |
throw new AssertionFailedError("timed out"); |
1511 |
|
assertFalse(p.getAsyncMode()); |
1512 |
|
assertFalse(p.isShutdown()); |
1513 |
|
assertFalse(p.isTerminating()); |
1516 |
|
} |
1517 |
|
assertEquals(0, p.getQueuedTaskCount()); |
1518 |
|
assertFalse(p.getAsyncMode()); |
1531 |
– |
assertEquals(0, p.getActiveThreadCount()); |
1532 |
– |
assertEquals(0, p.getQueuedTaskCount()); |
1519 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
1520 |
|
assertFalse(p.hasQueuedSubmissions()); |
1521 |
+ |
while (p.getActiveThreadCount() != 0 |
1522 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1523 |
+ |
Thread.yield(); |
1524 |
|
assertFalse(p.isShutdown()); |
1525 |
|
assertFalse(p.isTerminating()); |
1526 |
|
assertFalse(p.isTerminated()); |
1527 |
< |
} finally { |
1539 |
< |
joinPool(p); |
1527 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1528 |
|
} |
1529 |
|
} |
1530 |
|
|
1531 |
|
/** |
1532 |
< |
* awaitQuiescent returns when pool isQuiescent() or the indicated |
1533 |
< |
* timeout elapse |
1532 |
> |
* awaitQuiescence returns when pool isQuiescent() or the indicated |
1533 |
> |
* timeout elapsed |
1534 |
|
*/ |
1535 |
< |
public void testAwaitQuiescent2() throws Exception { |
1535 |
> |
public void testAwaitQuiescence2() throws Exception { |
1536 |
> |
/** |
1537 |
> |
* """It is possible to disable or limit the use of threads in the |
1538 |
> |
* common pool by setting the parallelism property to zero. However |
1539 |
> |
* doing so may cause unjoined tasks to never be executed.""" |
1540 |
> |
*/ |
1541 |
> |
if ("0".equals(System.getProperty( |
1542 |
> |
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1543 |
> |
return; |
1544 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1545 |
< |
try { |
1545 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1546 |
|
assertTrue(p.isQuiescent()); |
1547 |
< |
for (;;) { |
1548 |
< |
final long startTime = System.nanoTime(); |
1549 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1550 |
< |
protected void realCompute() { |
1551 |
< |
FibAction f = new FibAction(8); |
1552 |
< |
assertSame(f, f.fork()); |
1553 |
< |
ForkJoinTask.helpQuiesce(); |
1554 |
< |
while (!f.isDone()) { |
1555 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1556 |
< |
threadFail("timed out"); |
1557 |
< |
assertFalse(p.getAsyncMode()); |
1558 |
< |
assertFalse(p.isShutdown()); |
1559 |
< |
assertFalse(p.isTerminating()); |
1560 |
< |
assertFalse(p.isTerminated()); |
1561 |
< |
Thread.yield(); |
1562 |
< |
} |
1563 |
< |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1564 |
< |
try { |
1565 |
< |
assertEquals(21, f.result); |
1566 |
< |
} catch (Throwable fail) { System.out.println("fail " + fail.getMessage()); } |
1567 |
< |
} |
1568 |
< |
}; |
1569 |
< |
p.execute(a); |
1570 |
< |
if (a.isDone() || p.isQuiescent()) |
1571 |
< |
continue; // Already done so cannot test; retry |
1572 |
< |
while (!p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) { |
1573 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1574 |
< |
threadFail("timed out"); |
1575 |
< |
assertFalse(p.getAsyncMode()); |
1576 |
< |
assertFalse(p.isShutdown()); |
1577 |
< |
assertFalse(p.isTerminating()); |
1578 |
< |
assertFalse(p.isTerminated()); |
1583 |
< |
Thread.yield(); |
1584 |
< |
} |
1585 |
< |
assertTrue(p.isQuiescent()); |
1586 |
< |
assertTrue(a.isDone()); |
1587 |
< |
assertEquals(0, p.getQueuedTaskCount()); |
1588 |
< |
assertFalse(p.getAsyncMode()); |
1589 |
< |
assertEquals(0, p.getActiveThreadCount()); |
1590 |
< |
assertEquals(0, p.getQueuedTaskCount()); |
1591 |
< |
assertEquals(0, p.getQueuedSubmissionCount()); |
1592 |
< |
assertFalse(p.hasQueuedSubmissions()); |
1593 |
< |
assertFalse(p.isShutdown()); |
1594 |
< |
assertFalse(p.isTerminating()); |
1595 |
< |
assertFalse(p.isTerminated()); |
1596 |
< |
break; |
1597 |
< |
} |
1598 |
< |
} finally { |
1599 |
< |
joinPool(p); |
1547 |
> |
final long startTime = System.nanoTime(); |
1548 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1549 |
> |
protected void realCompute() { |
1550 |
> |
FibAction f = new FibAction(8); |
1551 |
> |
assertSame(f, f.fork()); |
1552 |
> |
while (!f.isDone() |
1553 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) { |
1554 |
> |
assertFalse(p.getAsyncMode()); |
1555 |
> |
assertFalse(p.isShutdown()); |
1556 |
> |
assertFalse(p.isTerminating()); |
1557 |
> |
assertFalse(p.isTerminated()); |
1558 |
> |
Thread.yield(); |
1559 |
> |
} |
1560 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1561 |
> |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1562 |
> |
assertEquals(21, f.result); |
1563 |
> |
}}; |
1564 |
> |
p.execute(a); |
1565 |
> |
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1566 |
> |
assertTrue(p.isQuiescent()); |
1567 |
> |
assertTrue(a.isDone()); |
1568 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
1569 |
> |
assertFalse(p.getAsyncMode()); |
1570 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
1571 |
> |
assertFalse(p.hasQueuedSubmissions()); |
1572 |
> |
while (p.getActiveThreadCount() != 0 |
1573 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1574 |
> |
Thread.yield(); |
1575 |
> |
assertFalse(p.isShutdown()); |
1576 |
> |
assertFalse(p.isTerminating()); |
1577 |
> |
assertFalse(p.isTerminated()); |
1578 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1579 |
|
} |
1580 |
|
} |
1581 |
|
|