--- jsr166/src/test/tck/ForkJoinPool8Test.java 2013/06/03 16:46:12 1.8 +++ jsr166/src/test/tck/ForkJoinPool8Test.java 2016/08/16 23:02:57 1.33 @@ -4,25 +4,24 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.HashSet; import java.util.concurrent.CancellationException; +import java.util.concurrent.CountedCompleter; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; -import java.util.concurrent.ForkJoinWorkerThread; import java.util.concurrent.RecursiveAction; -import java.util.concurrent.CountedCompleter; -import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.SECONDS; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import java.util.Arrays; -import java.util.HashSet; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ForkJoinPool8Test extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -185,7 +184,7 @@ public class ForkJoinPool8Test extends J final int number; int result; FibAction(int n) { number = n; } - public void realCompute() { + protected void realCompute() { int n = number; if (n <= 1) result = n; @@ -223,7 +222,7 @@ public class ForkJoinPool8Test extends J */ public void testInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertNull(f.invoke()); assertEquals(21, f.result); @@ -239,7 +238,7 @@ public class ForkJoinPool8Test extends J */ public void testQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.quietlyInvoke(); assertEquals(21, f.result); @@ -253,7 +252,7 @@ public class ForkJoinPool8Test extends J */ public void testForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); assertNull(f.join()); @@ -268,14 +267,14 @@ public class ForkJoinPool8Test extends J */ public void testJoinIgnoresInterrupts() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); - final Thread myself = Thread.currentThread(); + final Thread currentThread = Thread.currentThread(); // test join() assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); + assertTrue(currentThread.isInterrupted()); assertNull(f.join()); Thread.interrupted(); assertEquals(21, f.result); @@ -284,8 +283,8 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); + assertTrue(currentThread.isInterrupted()); try { f.join(); shouldThrow(); @@ -297,8 +296,8 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); + assertTrue(currentThread.isInterrupted()); try { f.join(); shouldThrow(); @@ -310,8 +309,8 @@ public class ForkJoinPool8Test extends J // test quietlyJoin() f = new FibAction(8); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); + assertTrue(currentThread.isInterrupted()); f.quietlyJoin(); Thread.interrupted(); assertEquals(21, f.result); @@ -320,8 +319,8 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); + assertTrue(currentThread.isInterrupted()); f.quietlyJoin(); Thread.interrupted(); checkCancelled(f); @@ -329,8 +328,8 @@ public class ForkJoinPool8Test extends J f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); + assertTrue(currentThread.isInterrupted()); f.quietlyJoin(); Thread.interrupted(); checkCompletedAbnormally(f, f.getException()); @@ -340,13 +339,12 @@ public class ForkJoinPool8Test extends J checkInvoke(a); } - /** * get of a forked task returns when task completes */ public void testForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); assertNull(f.get()); @@ -361,7 +359,7 @@ public class ForkJoinPool8Test extends J */ public void testForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); assertNull(f.get(5L, SECONDS)); @@ -376,7 +374,7 @@ public class ForkJoinPool8Test extends J */ public void testForkTimedGetNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertSame(f, f.fork()); try { @@ -392,7 +390,7 @@ public class ForkJoinPool8Test extends J */ public void testForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -407,7 +405,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); try { f.invoke(); @@ -424,7 +422,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -438,7 +436,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { @@ -456,7 +454,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { @@ -476,11 +474,11 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { - f.get(5L, TimeUnit.SECONDS); + f.get(5L, SECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); @@ -496,7 +494,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -511,7 +509,7 @@ public class ForkJoinPool8Test extends J */ public void testCancelledInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); try { @@ -529,7 +527,7 @@ public class ForkJoinPool8Test extends J */ public void testCancelledForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -548,7 +546,7 @@ public class ForkJoinPool8Test extends J */ public void testCancelledForkGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -567,7 +565,7 @@ public class ForkJoinPool8Test extends J */ public void testCancelledForkTimedGet() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws Exception { + protected void realCompute() throws Exception { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -586,7 +584,7 @@ public class ForkJoinPool8Test extends J */ public void testCancelledForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -601,7 +599,7 @@ public class ForkJoinPool8Test extends J */ public void testInForkJoinPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); @@ -612,7 +610,7 @@ public class ForkJoinPool8Test extends J */ public void testReinitialize() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); checkNotDone(f); @@ -632,7 +630,7 @@ public class ForkJoinPool8Test extends J */ public void testReinitializeAbnormal() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); checkNotDone(f); @@ -655,7 +653,7 @@ public class ForkJoinPool8Test extends J */ public void testCompleteExceptionally() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.completeExceptionally(new FJException()); try { @@ -673,7 +671,7 @@ public class ForkJoinPool8Test extends J */ public void testComplete() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.complete(null); assertNull(f.invoke()); @@ -688,7 +686,7 @@ public class ForkJoinPool8Test extends J */ public void testInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); invokeAll(f, g); @@ -705,7 +703,7 @@ public class ForkJoinPool8Test extends J */ public void testInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); invokeAll(f); checkCompletedNormally(f); @@ -719,7 +717,7 @@ public class ForkJoinPool8Test extends J */ public void testInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -742,7 +740,7 @@ public class ForkJoinPool8Test extends J */ public void testInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -769,7 +767,7 @@ public class ForkJoinPool8Test extends J */ public void testInvokeAllNPE() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FibAction g = new FibAction(9); FibAction h = null; @@ -786,7 +784,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FailingFibAction g = new FailingFibAction(9); try { @@ -804,7 +802,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction g = new FailingFibAction(9); try { invokeAll(g); @@ -821,7 +819,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalInvokeAll3() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FailingFibAction g = new FailingFibAction(9); FibAction h = new FibAction(7); @@ -840,7 +838,7 @@ public class ForkJoinPool8Test extends J */ public void testAbnormalInvokeAllCollection() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); FibAction g = new FibAction(9); FibAction h = new FibAction(7); @@ -860,18 +858,6 @@ public class ForkJoinPool8Test extends J // CountedCompleter versions - public abstract class CheckedFJTask extends RecursiveAction { - protected abstract void realCompute() throws Throwable; - - public final void compute() { - try { - realCompute(); - } catch (Throwable t) { - threadUnexpectedException(t); - } - } - } - abstract static class CCF extends CountedCompleter { int number; int rnumber; @@ -980,8 +966,8 @@ public class ForkJoinPool8Test extends J * completed tasks; getRawResult returns null. */ public void testInvokeCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); assertNull(f.invoke()); assertEquals(21, f.number); @@ -996,8 +982,8 @@ public class ForkJoinPool8Test extends J * completed tasks */ public void testQuietlyInvokeCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); f.quietlyInvoke(); assertEquals(21, f.number); @@ -1010,8 +996,8 @@ public class ForkJoinPool8Test extends J * join of a forked task returns when task completes */ public void testForkJoinCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); assertSame(f, f.fork()); assertNull(f.join()); @@ -1025,8 +1011,8 @@ public class ForkJoinPool8Test extends J * get of a forked task returns when task completes */ public void testForkGetCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { CCF f = new LCCF(null, 8); assertSame(f, f.fork()); assertNull(f.get()); @@ -1040,8 +1026,8 @@ public class ForkJoinPool8Test extends J * timed get of a forked task returns when task completes */ public void testForkTimedGetCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { CCF f = new LCCF(null, 8); assertSame(f, f.fork()); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); @@ -1055,8 +1041,8 @@ public class ForkJoinPool8Test extends J * timed get with null time unit throws NPE */ public void testForkTimedGetNPECC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { CCF f = new LCCF(null, 8); assertSame(f, f.fork()); try { @@ -1071,8 +1057,8 @@ public class ForkJoinPool8Test extends J * quietlyJoin of a forked task returns when task completes */ public void testForkQuietlyJoinCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -1086,8 +1072,8 @@ public class ForkJoinPool8Test extends J * invoke task throws exception when task completes abnormally */ public void testAbnormalInvokeCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { FailingCCF f = new LFCCF(null, 8); try { f.invoke(); @@ -1103,8 +1089,8 @@ public class ForkJoinPool8Test extends J * quietlyInvoke task returns when task completes abnormally */ public void testAbnormalQuietlyInvokeCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { FailingCCF f = new LFCCF(null, 8); f.quietlyInvoke(); assertTrue(f.getException() instanceof FJException); @@ -1117,8 +1103,8 @@ public class ForkJoinPool8Test extends J * join of a forked task throws exception when task completes abnormally */ public void testAbnormalForkJoinCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { FailingCCF f = new LFCCF(null, 8); assertSame(f, f.fork()); try { @@ -1135,8 +1121,8 @@ public class ForkJoinPool8Test extends J * get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkGetCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { FailingCCF f = new LFCCF(null, 8); assertSame(f, f.fork()); try { @@ -1155,8 +1141,8 @@ public class ForkJoinPool8Test extends J * timed get of a forked task throws exception when task completes abnormally */ public void testAbnormalForkTimedGetCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { FailingCCF f = new LFCCF(null, 8); assertSame(f, f.fork()); try { @@ -1175,8 +1161,8 @@ public class ForkJoinPool8Test extends J * quietlyJoin of a forked task returns when task completes abnormally */ public void testAbnormalForkQuietlyJoinCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { FailingCCF f = new LFCCF(null, 8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -1190,8 +1176,8 @@ public class ForkJoinPool8Test extends J * invoke task throws exception when task cancelled */ public void testCancelledInvokeCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); assertTrue(f.cancel(true)); try { @@ -1208,8 +1194,8 @@ public class ForkJoinPool8Test extends J * join of a forked task throws exception when task cancelled */ public void testCancelledForkJoinCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1227,8 +1213,8 @@ public class ForkJoinPool8Test extends J * get of a forked task throws exception when task cancelled */ public void testCancelledForkGetCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { CCF f = new LCCF(null, 8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1246,8 +1232,8 @@ public class ForkJoinPool8Test extends J * timed get of a forked task throws exception when task cancelled */ public void testCancelledForkTimedGetCC() throws Exception { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() throws Exception { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() throws Exception { CCF f = new LCCF(null, 8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1265,8 +1251,8 @@ public class ForkJoinPool8Test extends J * quietlyJoin of a forked task returns when task cancelled */ public void testCancelledForkQuietlyJoinCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); assertTrue(f.cancel(true)); assertSame(f, f.fork()); @@ -1280,8 +1266,8 @@ public class ForkJoinPool8Test extends J * getPool of non-FJ task returns null */ public void testGetPool2CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { assertNull(getPool()); }}; assertNull(a.invoke()); @@ -1291,8 +1277,8 @@ public class ForkJoinPool8Test extends J * inForkJoinPool of non-FJ task returns false */ public void testInForkJoinPool2CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); @@ -1302,8 +1288,8 @@ public class ForkJoinPool8Test extends J * setRawResult(null) succeeds */ public void testSetRawResultCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { setRawResult(null); assertNull(getRawResult()); }}; @@ -1314,8 +1300,8 @@ public class ForkJoinPool8Test extends J * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally2CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); f.completeExceptionally(new FJException()); try { @@ -1332,8 +1318,8 @@ public class ForkJoinPool8Test extends J * invokeAll(t1, t2) invokes all task arguments */ public void testInvokeAll2CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); CCF g = new LCCF(null, 9); invokeAll(f, g); @@ -1349,8 +1335,8 @@ public class ForkJoinPool8Test extends J * invokeAll(tasks) with 1 argument invokes task */ public void testInvokeAll1CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); invokeAll(f); checkCompletedNormally(f); @@ -1363,8 +1349,8 @@ public class ForkJoinPool8Test extends J * invokeAll(tasks) with > 2 argument invokes tasks */ public void testInvokeAll3CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); CCF g = new LCCF(null, 9); CCF h = new LCCF(null, 7); @@ -1383,8 +1369,8 @@ public class ForkJoinPool8Test extends J * invokeAll(collection) invokes all tasks in the collection */ public void testInvokeAllCollectionCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); CCF g = new LCCF(null, 9); CCF h = new LCCF(null, 7); @@ -1407,8 +1393,8 @@ public class ForkJoinPool8Test extends J * invokeAll(tasks) with any null task throws NPE */ public void testInvokeAllNPECC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); CCF g = new LCCF(null, 9); CCF h = null; @@ -1424,8 +1410,8 @@ public class ForkJoinPool8Test extends J * invokeAll(t1, t2) throw exception if any task does */ public void testAbnormalInvokeAll2CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); FailingCCF g = new LFCCF(null, 9); try { @@ -1442,8 +1428,8 @@ public class ForkJoinPool8Test extends J * invokeAll(tasks) with 1 argument throws exception if task does */ public void testAbnormalInvokeAll1CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { FailingCCF g = new LFCCF(null, 9); try { invokeAll(g); @@ -1459,8 +1445,8 @@ public class ForkJoinPool8Test extends J * invokeAll(tasks) with > 2 argument throws exception if any task does */ public void testAbnormalInvokeAll3CC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { CCF f = new LCCF(null, 8); FailingCCF g = new LFCCF(null, 9); CCF h = new LCCF(null, 7); @@ -1475,11 +1461,11 @@ public class ForkJoinPool8Test extends J } /** - * invokeAll(collection) throws exception if any task does + * invokeAll(collection) throws exception if any task does */ public void testAbnormalInvokeAllCollectionCC() { - ForkJoinTask a = new CheckedFJTask() { - public void realCompute() { + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { FailingCCF f = new LFCCF(null, 8); CCF g = new LCCF(null, 9); CCF h = new LCCF(null, 7); @@ -1497,4 +1483,106 @@ public class ForkJoinPool8Test extends J checkInvoke(a); } + /** + * awaitQuiescence by a worker is equivalent in effect to + * ForkJoinTask.helpQuiesce() + */ + public void testAwaitQuiescence1() throws Exception { + final ForkJoinPool p = new ForkJoinPool(); + try (PoolCleaner cleaner = cleaner(p)) { + final long startTime = System.nanoTime(); + assertTrue(p.isQuiescent()); + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); + assertSame(p, ForkJoinTask.getPool()); + boolean quiescent = p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS); + assertTrue(quiescent); + assertFalse(p.isQuiescent()); + while (!f.isDone()) { + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertFalse(p.isQuiescent()); + assertEquals(0, ForkJoinTask.getQueuedTaskCount()); + assertEquals(21, f.result); + }}; + p.execute(a); + while (!a.isDone() || !p.isQuiescent()) { + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertEquals(0, p.getQueuedTaskCount()); + assertFalse(p.getAsyncMode()); + assertEquals(0, p.getQueuedSubmissionCount()); + assertFalse(p.hasQueuedSubmissions()); + while (p.getActiveThreadCount() != 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + } + + /** + * awaitQuiescence returns when pool isQuiescent() or the indicated + * timeout elapsed + */ + public void testAwaitQuiescence2() throws Exception { + /** + * """It is possible to disable or limit the use of threads in the + * common pool by setting the parallelism property to zero. However + * doing so may cause unjoined tasks to never be executed.""" + */ + if ("0".equals(System.getProperty( + "java.util.concurrent.ForkJoinPool.common.parallelism"))) + return; + final ForkJoinPool p = new ForkJoinPool(); + try (PoolCleaner cleaner = cleaner(p)) { + assertTrue(p.isQuiescent()); + final long startTime = System.nanoTime(); + ForkJoinTask a = new CheckedRecursiveAction() { + protected void realCompute() { + FibAction f = new FibAction(8); + assertSame(f, f.fork()); + while (!f.isDone() + && millisElapsedSince(startTime) < LONG_DELAY_MS) { + assertFalse(p.getAsyncMode()); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + Thread.yield(); + } + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + assertEquals(0, ForkJoinTask.getQueuedTaskCount()); + assertEquals(21, f.result); + }}; + p.execute(a); + assertTrue(p.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)); + assertTrue(p.isQuiescent()); + assertTrue(a.isDone()); + assertEquals(0, p.getQueuedTaskCount()); + assertFalse(p.getAsyncMode()); + assertEquals(0, p.getQueuedSubmissionCount()); + assertFalse(p.hasQueuedSubmissions()); + while (p.getActiveThreadCount() != 0 + && millisElapsedSince(startTime) < LONG_DELAY_MS) + Thread.yield(); + assertFalse(p.isShutdown()); + assertFalse(p.isTerminating()); + assertFalse(p.isTerminated()); + assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); + } + } + }