4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
> |
import java.util.HashSet; |
10 |
|
import java.util.concurrent.CancellationException; |
11 |
+ |
import java.util.concurrent.CountedCompleter; |
12 |
|
import java.util.concurrent.ExecutionException; |
13 |
|
import java.util.concurrent.ForkJoinPool; |
14 |
|
import java.util.concurrent.ForkJoinTask; |
12 |
– |
import java.util.concurrent.ForkJoinWorkerThread; |
15 |
|
import java.util.concurrent.RecursiveAction; |
14 |
– |
import java.util.concurrent.CountedCompleter; |
15 |
– |
import java.util.concurrent.ThreadLocalRandom; |
16 |
– |
import java.util.concurrent.TimeUnit; |
16 |
|
import java.util.concurrent.TimeoutException; |
17 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
18 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
19 |
< |
import java.util.Arrays; |
21 |
< |
import java.util.HashSet; |
17 |
> |
|
18 |
> |
import junit.framework.Test; |
19 |
> |
import junit.framework.TestSuite; |
20 |
|
|
21 |
|
public class ForkJoinPool8Test extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
|
26 |
|
public static Test suite() { |
83 |
|
|
84 |
|
Thread.currentThread().interrupt(); |
85 |
|
try { |
86 |
< |
a.get(5L, SECONDS); |
86 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
87 |
|
shouldThrow(); |
88 |
|
} catch (InterruptedException success) { |
89 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
90 |
|
} |
91 |
|
|
92 |
|
try { |
93 |
< |
a.get(0L, SECONDS); |
93 |
> |
a.get(randomExpiredTimeout(), randomTimeUnit()); |
94 |
|
shouldThrow(); |
95 |
|
} catch (TimeoutException success) { |
96 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
108 |
|
assertFalse(a.cancel(true)); |
109 |
|
try { |
110 |
|
assertNull(a.get()); |
111 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
114 |
< |
try { |
115 |
< |
assertNull(a.get(5L, SECONDS)); |
111 |
> |
assertNull(a.get(randomTimeout(), randomTimeUnit())); |
112 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
113 |
|
} |
114 |
|
|
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
|
|
135 |
|
try { |
136 |
< |
a.get(5L, SECONDS); |
136 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
137 |
|
shouldThrow(); |
138 |
|
} catch (CancellationException success) { |
139 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
165 |
|
|
166 |
|
try { |
167 |
< |
a.get(5L, SECONDS); |
167 |
> |
a.get(randomTimeout(), randomTimeUnit()); |
168 |
|
shouldThrow(); |
169 |
|
} catch (ExecutionException success) { |
170 |
|
assertSame(t.getClass(), success.getCause().getClass()); |
176 |
|
public FJException(Throwable cause) { super(cause); } |
177 |
|
} |
178 |
|
|
179 |
< |
// A simple recursive action for testing |
179 |
> |
/** A simple recursive action for testing. */ |
180 |
|
final class FibAction extends CheckedRecursiveAction { |
181 |
|
final int number; |
182 |
|
int result; |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
< |
// A recursive action failing in base case |
197 |
> |
/** A recursive action failing in base case. */ |
198 |
|
static final class FailingFibAction extends RecursiveAction { |
199 |
|
final int number; |
200 |
|
int result; |
266 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
267 |
|
protected void realCompute() { |
268 |
|
FibAction f = new FibAction(8); |
269 |
< |
final Thread myself = Thread.currentThread(); |
269 |
> |
final Thread currentThread = Thread.currentThread(); |
270 |
|
|
271 |
|
// test join() |
272 |
|
assertSame(f, f.fork()); |
273 |
< |
myself.interrupt(); |
278 |
< |
assertTrue(myself.isInterrupted()); |
273 |
> |
currentThread.interrupt(); |
274 |
|
assertNull(f.join()); |
275 |
|
Thread.interrupted(); |
276 |
|
assertEquals(21, f.result); |
279 |
|
f = new FibAction(8); |
280 |
|
f.cancel(true); |
281 |
|
assertSame(f, f.fork()); |
282 |
< |
myself.interrupt(); |
288 |
< |
assertTrue(myself.isInterrupted()); |
282 |
> |
currentThread.interrupt(); |
283 |
|
try { |
284 |
|
f.join(); |
285 |
|
shouldThrow(); |
291 |
|
f = new FibAction(8); |
292 |
|
f.completeExceptionally(new FJException()); |
293 |
|
assertSame(f, f.fork()); |
294 |
< |
myself.interrupt(); |
301 |
< |
assertTrue(myself.isInterrupted()); |
294 |
> |
currentThread.interrupt(); |
295 |
|
try { |
296 |
|
f.join(); |
297 |
|
shouldThrow(); |
303 |
|
// test quietlyJoin() |
304 |
|
f = new FibAction(8); |
305 |
|
assertSame(f, f.fork()); |
306 |
< |
myself.interrupt(); |
314 |
< |
assertTrue(myself.isInterrupted()); |
306 |
> |
currentThread.interrupt(); |
307 |
|
f.quietlyJoin(); |
308 |
|
Thread.interrupted(); |
309 |
|
assertEquals(21, f.result); |
312 |
|
f = new FibAction(8); |
313 |
|
f.cancel(true); |
314 |
|
assertSame(f, f.fork()); |
315 |
< |
myself.interrupt(); |
324 |
< |
assertTrue(myself.isInterrupted()); |
315 |
> |
currentThread.interrupt(); |
316 |
|
f.quietlyJoin(); |
317 |
|
Thread.interrupted(); |
318 |
|
checkCancelled(f); |
320 |
|
f = new FibAction(8); |
321 |
|
f.completeExceptionally(new FJException()); |
322 |
|
assertSame(f, f.fork()); |
323 |
< |
myself.interrupt(); |
333 |
< |
assertTrue(myself.isInterrupted()); |
323 |
> |
currentThread.interrupt(); |
324 |
|
f.quietlyJoin(); |
325 |
|
Thread.interrupted(); |
326 |
|
checkCompletedAbnormally(f, f.getException()); |
353 |
|
protected void realCompute() throws Exception { |
354 |
|
FibAction f = new FibAction(8); |
355 |
|
assertSame(f, f.fork()); |
356 |
< |
assertNull(f.get(5L, SECONDS)); |
356 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
357 |
|
assertEquals(21, f.result); |
358 |
|
checkCompletedNormally(f); |
359 |
|
}}; |
369 |
|
FibAction f = new FibAction(8); |
370 |
|
assertSame(f, f.fork()); |
371 |
|
try { |
372 |
< |
f.get(5L, null); |
372 |
> |
f.get(randomTimeout(), null); |
373 |
|
shouldThrow(); |
374 |
|
} catch (NullPointerException success) {} |
375 |
|
}}; |
469 |
|
FailingFibAction f = new FailingFibAction(8); |
470 |
|
assertSame(f, f.fork()); |
471 |
|
try { |
472 |
< |
f.get(5L, TimeUnit.SECONDS); |
472 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
473 |
|
shouldThrow(); |
474 |
|
} catch (ExecutionException success) { |
475 |
|
Throwable cause = success.getCause(); |
561 |
|
assertTrue(f.cancel(true)); |
562 |
|
assertSame(f, f.fork()); |
563 |
|
try { |
564 |
< |
f.get(5L, SECONDS); |
564 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
565 |
|
shouldThrow(); |
566 |
|
} catch (CancellationException success) { |
567 |
|
checkCancelled(f); |
902 |
|
} |
903 |
|
} |
904 |
|
|
905 |
< |
// Version of CCF with forced failure in left completions |
905 |
> |
/** Version of CCF with forced failure in left completions. */ |
906 |
|
abstract static class FailingCCF extends CountedCompleter { |
907 |
|
int number; |
908 |
|
int rnumber; |
1037 |
|
CCF f = new LCCF(null, 8); |
1038 |
|
assertSame(f, f.fork()); |
1039 |
|
try { |
1040 |
< |
f.get(5L, null); |
1040 |
> |
f.get(randomTimeout(), null); |
1041 |
|
shouldThrow(); |
1042 |
|
} catch (NullPointerException success) {} |
1043 |
|
}}; |
1480 |
|
*/ |
1481 |
|
public void testAwaitQuiescence1() throws Exception { |
1482 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1483 |
< |
try { |
1483 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1484 |
|
final long startTime = System.nanoTime(); |
1485 |
|
assertTrue(p.isQuiescent()); |
1486 |
|
ForkJoinTask a = new CheckedRecursiveAction() { |
1487 |
|
protected void realCompute() { |
1488 |
|
FibAction f = new FibAction(8); |
1489 |
|
assertSame(f, f.fork()); |
1490 |
< |
boolean t = ForkJoinTask.getPool().awaitQuiescence(MEDIUM_DELAY_MS, TimeUnit.SECONDS); |
1491 |
< |
assertTrue(t); |
1490 |
> |
assertSame(p, ForkJoinTask.getPool()); |
1491 |
> |
boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); |
1492 |
> |
assertTrue(quiescent); |
1493 |
> |
assertFalse(p.isQuiescent()); |
1494 |
|
while (!f.isDone()) { |
1503 |
– |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1504 |
– |
threadFail("timed out"); |
1495 |
|
assertFalse(p.getAsyncMode()); |
1496 |
|
assertFalse(p.isShutdown()); |
1497 |
|
assertFalse(p.isTerminating()); |
1498 |
|
assertFalse(p.isTerminated()); |
1499 |
|
Thread.yield(); |
1500 |
|
} |
1501 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1502 |
|
assertFalse(p.isQuiescent()); |
1503 |
|
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1504 |
< |
try { |
1514 |
< |
assertEquals(21, f.result); |
1515 |
< |
} catch (Throwable fail) { |
1516 |
< |
threadFail(fail.getMessage()); |
1517 |
< |
} |
1504 |
> |
assertEquals(21, f.result); |
1505 |
|
}}; |
1506 |
|
p.execute(a); |
1507 |
|
while (!a.isDone() || !p.isQuiescent()) { |
1521 |
– |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1522 |
– |
throw new AssertionFailedError("timed out"); |
1508 |
|
assertFalse(p.getAsyncMode()); |
1509 |
|
assertFalse(p.isShutdown()); |
1510 |
|
assertFalse(p.isTerminating()); |
1513 |
|
} |
1514 |
|
assertEquals(0, p.getQueuedTaskCount()); |
1515 |
|
assertFalse(p.getAsyncMode()); |
1531 |
– |
assertEquals(0, p.getActiveThreadCount()); |
1532 |
– |
assertEquals(0, p.getQueuedTaskCount()); |
1516 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
1517 |
|
assertFalse(p.hasQueuedSubmissions()); |
1518 |
+ |
while (p.getActiveThreadCount() != 0 |
1519 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1520 |
+ |
Thread.yield(); |
1521 |
|
assertFalse(p.isShutdown()); |
1522 |
|
assertFalse(p.isTerminating()); |
1523 |
|
assertFalse(p.isTerminated()); |
1524 |
< |
} finally { |
1539 |
< |
joinPool(p); |
1524 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1525 |
|
} |
1526 |
|
} |
1527 |
|
|
1530 |
|
* timeout elapsed |
1531 |
|
*/ |
1532 |
|
public void testAwaitQuiescence2() throws Exception { |
1533 |
+ |
/* |
1534 |
+ |
* """It is possible to disable or limit the use of threads in the |
1535 |
+ |
* common pool by setting the parallelism property to zero. However |
1536 |
+ |
* doing so may cause unjoined tasks to never be executed.""" |
1537 |
+ |
*/ |
1538 |
+ |
if ("0".equals(System.getProperty( |
1539 |
+ |
"java.util.concurrent.ForkJoinPool.common.parallelism"))) |
1540 |
+ |
return; |
1541 |
|
final ForkJoinPool p = new ForkJoinPool(); |
1542 |
< |
try { |
1542 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1543 |
|
assertTrue(p.isQuiescent()); |
1544 |
< |
for (;;) { |
1545 |
< |
final long startTime = System.nanoTime(); |
1546 |
< |
ForkJoinTask a = new CheckedRecursiveAction() { |
1547 |
< |
protected void realCompute() { |
1548 |
< |
FibAction f = new FibAction(8); |
1549 |
< |
assertSame(f, f.fork()); |
1550 |
< |
ForkJoinTask.helpQuiesce(); |
1551 |
< |
while (!f.isDone()) { |
1552 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1553 |
< |
threadFail("timed out"); |
1554 |
< |
assertFalse(p.getAsyncMode()); |
1555 |
< |
assertFalse(p.isShutdown()); |
1556 |
< |
assertFalse(p.isTerminating()); |
1557 |
< |
assertFalse(p.isTerminated()); |
1558 |
< |
Thread.yield(); |
1559 |
< |
} |
1560 |
< |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1561 |
< |
try { |
1562 |
< |
assertEquals(21, f.result); |
1563 |
< |
} catch (Throwable fail) { System.out.println("fail " + fail.getMessage()); } |
1564 |
< |
}}; |
1565 |
< |
p.execute(a); |
1566 |
< |
if (a.isDone() || p.isQuiescent()) |
1567 |
< |
continue; // Already done so cannot test; retry |
1568 |
< |
while (!p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) { |
1569 |
< |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1570 |
< |
threadFail("timed out"); |
1571 |
< |
assertFalse(p.getAsyncMode()); |
1572 |
< |
assertFalse(p.isShutdown()); |
1573 |
< |
assertFalse(p.isTerminating()); |
1574 |
< |
assertFalse(p.isTerminated()); |
1575 |
< |
Thread.yield(); |
1583 |
< |
} |
1584 |
< |
assertTrue(p.isQuiescent()); |
1585 |
< |
assertTrue(a.isDone()); |
1586 |
< |
assertEquals(0, p.getQueuedTaskCount()); |
1587 |
< |
assertFalse(p.getAsyncMode()); |
1588 |
< |
assertEquals(0, p.getActiveThreadCount()); |
1589 |
< |
assertEquals(0, p.getQueuedTaskCount()); |
1590 |
< |
assertEquals(0, p.getQueuedSubmissionCount()); |
1591 |
< |
assertFalse(p.hasQueuedSubmissions()); |
1592 |
< |
assertFalse(p.isShutdown()); |
1593 |
< |
assertFalse(p.isTerminating()); |
1594 |
< |
assertFalse(p.isTerminated()); |
1595 |
< |
break; |
1596 |
< |
} |
1597 |
< |
} finally { |
1598 |
< |
joinPool(p); |
1544 |
> |
final long startTime = System.nanoTime(); |
1545 |
> |
ForkJoinTask a = new CheckedRecursiveAction() { |
1546 |
> |
protected void realCompute() { |
1547 |
> |
FibAction f = new FibAction(8); |
1548 |
> |
assertSame(f, f.fork()); |
1549 |
> |
while (!f.isDone() |
1550 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) { |
1551 |
> |
assertFalse(p.getAsyncMode()); |
1552 |
> |
assertFalse(p.isShutdown()); |
1553 |
> |
assertFalse(p.isTerminating()); |
1554 |
> |
assertFalse(p.isTerminated()); |
1555 |
> |
Thread.yield(); |
1556 |
> |
} |
1557 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1558 |
> |
assertEquals(0, ForkJoinTask.getQueuedTaskCount()); |
1559 |
> |
assertEquals(21, f.result); |
1560 |
> |
}}; |
1561 |
> |
p.execute(a); |
1562 |
> |
assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); |
1563 |
> |
assertTrue(p.isQuiescent()); |
1564 |
> |
assertTrue(a.isDone()); |
1565 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
1566 |
> |
assertFalse(p.getAsyncMode()); |
1567 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
1568 |
> |
assertFalse(p.hasQueuedSubmissions()); |
1569 |
> |
while (p.getActiveThreadCount() != 0 |
1570 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1571 |
> |
Thread.yield(); |
1572 |
> |
assertFalse(p.isShutdown()); |
1573 |
> |
assertFalse(p.isTerminating()); |
1574 |
> |
assertFalse(p.isTerminated()); |
1575 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1576 |
|
} |
1577 |
|
} |
1578 |
|
|