--- jsr166/src/test/tck/RecursiveActionTest.java 2011/05/31 10:28:06 1.32 +++ jsr166/src/test/tck/RecursiveActionTest.java 2018/01/07 22:59:18 1.53 @@ -4,23 +4,27 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import java.util.Arrays; +import java.util.HashSet; import java.util.concurrent.CancellationException; -import java.util.concurrent.SynchronousQueue; 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.TimeUnit; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.SECONDS; -import java.util.HashSet; + +import junit.framework.Test; +import junit.framework.TestSuite; public class RecursiveActionTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -42,14 +46,12 @@ public class RecursiveActionTest extends } private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) { - try { + try (PoolCleaner cleaner = cleaner(pool)) { checkNotDone(a); assertNull(pool.invoke(a)); checkCompletedNormally(a); - } finally { - joinPool(pool); } } @@ -71,14 +73,14 @@ public class RecursiveActionTest extends Thread.currentThread().interrupt(); try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } } try { - a.get(0L, SECONDS); + a.get(randomExpiredTimeout(), randomTimeUnit()); shouldThrow(); } catch (TimeoutException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -96,9 +98,7 @@ public class RecursiveActionTest extends assertFalse(a.cancel(true)); try { assertNull(a.get()); - } catch (Throwable fail) { threadUnexpectedException(fail); } - try { - assertNull(a.get(5L, SECONDS)); + assertNull(a.get(randomTimeout(), randomTimeUnit())); } catch (Throwable fail) { threadUnexpectedException(fail); } } @@ -123,7 +123,7 @@ public class RecursiveActionTest extends } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } @@ -154,7 +154,7 @@ public class RecursiveActionTest extends } catch (Throwable fail) { threadUnexpectedException(fail); } try { - a.get(5L, SECONDS); + a.get(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (ExecutionException success) { assertSame(t.getClass(), success.getCause().getClass()); @@ -166,12 +166,12 @@ public class RecursiveActionTest extends public FJException(Throwable cause) { super(cause); } } - // A simple recursive action for testing + /** A simple recursive action for testing. */ final class FibAction extends CheckedRecursiveAction { final int number; int result; FibAction(int n) { number = n; } - public void realCompute() { + protected void realCompute() { int n = number; if (n <= 1) result = n; @@ -184,7 +184,7 @@ public class RecursiveActionTest extends } } - // A recursive action failing in base case + /** A recursive action failing in base case. */ static final class FailingFibAction extends RecursiveAction { final int number; int result; @@ -209,7 +209,7 @@ public class RecursiveActionTest extends */ 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); @@ -225,7 +225,7 @@ public class RecursiveActionTest extends */ public void testQuietlyInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.quietlyInvoke(); assertEquals(21, f.result); @@ -239,7 +239,7 @@ public class RecursiveActionTest extends */ 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()); @@ -254,14 +254,13 @@ public class RecursiveActionTest extends */ 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(); assertNull(f.join()); Thread.interrupted(); assertEquals(21, f.result); @@ -270,8 +269,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -283,8 +281,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -296,8 +293,7 @@ public class RecursiveActionTest extends // test quietlyJoin() f = new FibAction(8); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); assertEquals(21, f.result); @@ -306,8 +302,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.cancel(true); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); checkCancelled(f); @@ -315,8 +310,7 @@ public class RecursiveActionTest extends f = new FibAction(8); f.completeExceptionally(new FJException()); assertSame(f, f.fork()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); Thread.interrupted(); checkCompletedAbnormally(f, f.getException()); @@ -331,10 +325,9 @@ public class RecursiveActionTest extends * succeeds in the presence of interrupts */ public void testJoinIgnoresInterruptsOutsideForkJoinPool() { - final SynchronousQueue sq = - new SynchronousQueue(); + final SynchronousQueue sq = new SynchronousQueue<>(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() throws InterruptedException { + protected void realCompute() throws InterruptedException { FibAction[] fibActions = new FibAction[6]; for (int i = 0; i < fibActions.length; i++) fibActions[i] = new FibAction(8); @@ -356,22 +349,20 @@ public class RecursiveActionTest extends public void realRun() throws InterruptedException { FibAction[] fibActions = sq.take(); FibAction f; - final Thread myself = Thread.currentThread(); + final Thread currentThread = Thread.currentThread(); // test join() ------------ f = fibActions[0]; assertFalse(ForkJoinTask.inForkJoinPool()); - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); assertNull(f.join()); assertTrue(Thread.interrupted()); assertEquals(21, f.result); checkCompletedNormally(f); f = fibActions[1]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -381,8 +372,7 @@ public class RecursiveActionTest extends } f = fibActions[2]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); try { f.join(); shouldThrow(); @@ -394,23 +384,20 @@ public class RecursiveActionTest extends // test quietlyJoin() --------- f = fibActions[3]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); assertTrue(Thread.interrupted()); assertEquals(21, f.result); checkCompletedNormally(f); f = fibActions[4]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); assertTrue(Thread.interrupted()); checkCancelled(f); f = fibActions[5]; - myself.interrupt(); - assertTrue(myself.isInterrupted()); + currentThread.interrupt(); f.quietlyJoin(); assertTrue(Thread.interrupted()); assertTrue(f.getException() instanceof FJException); @@ -421,12 +408,12 @@ public class RecursiveActionTest extends t = newStartedThread(r); testInvokeOnPool(mainPool(), a); - awaitTermination(t, LONG_DELAY_MS); + awaitTermination(t); a.reinitialize(); t = newStartedThread(r); testInvokeOnPool(singletonPool(), a); - awaitTermination(t, LONG_DELAY_MS); + awaitTermination(t); } /** @@ -434,7 +421,7 @@ public class RecursiveActionTest extends */ 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()); @@ -449,10 +436,10 @@ public class RecursiveActionTest extends */ 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)); + assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); assertEquals(21, f.result); checkCompletedNormally(f); }}; @@ -464,11 +451,11 @@ public class RecursiveActionTest extends */ 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 { - f.get(5L, null); + f.get(randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} }}; @@ -480,7 +467,7 @@ public class RecursiveActionTest extends */ public void testForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -496,10 +483,12 @@ public class RecursiveActionTest extends */ public void testForkHelpQuiesce() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertSame(f, f.fork()); helpQuiesce(); + while (!f.isDone()) // wait out race + ; assertEquals(21, f.result); assertEquals(0, getQueuedTaskCount()); checkCompletedNormally(f); @@ -512,7 +501,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); try { f.invoke(); @@ -529,7 +518,7 @@ public class RecursiveActionTest extends */ 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); @@ -543,7 +532,7 @@ public class RecursiveActionTest extends */ public void testAbnormalForkJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); try { @@ -561,7 +550,7 @@ public class RecursiveActionTest extends */ 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 { @@ -581,11 +570,11 @@ public class RecursiveActionTest extends */ 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(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { Throwable cause = success.getCause(); @@ -601,7 +590,7 @@ public class RecursiveActionTest extends */ public void testAbnormalForkQuietlyJoin() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction f = new FailingFibAction(8); assertSame(f, f.fork()); f.quietlyJoin(); @@ -616,7 +605,7 @@ public class RecursiveActionTest extends */ public void testCancelledInvoke() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); assertTrue(f.cancel(true)); try { @@ -634,7 +623,7 @@ public class RecursiveActionTest extends */ 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()); @@ -653,7 +642,7 @@ public class RecursiveActionTest extends */ 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()); @@ -672,12 +661,12 @@ public class RecursiveActionTest extends */ 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()); try { - f.get(5L, SECONDS); + f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { checkCancelled(f); @@ -691,7 +680,7 @@ public class RecursiveActionTest extends */ 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()); @@ -707,7 +696,7 @@ public class RecursiveActionTest extends public void testGetPool() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertSame(mainPool, getPool()); }}; testInvokeOnPool(mainPool, a); @@ -718,7 +707,7 @@ public class RecursiveActionTest extends */ public void testGetPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertNull(getPool()); }}; assertNull(a.invoke()); @@ -729,7 +718,7 @@ public class RecursiveActionTest extends */ public void testInForkJoinPool() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertTrue(inForkJoinPool()); }}; testInvokeOnPool(mainPool(), a); @@ -740,7 +729,7 @@ public class RecursiveActionTest extends */ public void testInForkJoinPool2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { assertFalse(inForkJoinPool()); }}; assertNull(a.invoke()); @@ -752,7 +741,7 @@ public class RecursiveActionTest extends public void testWorkerGetPool() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); assertSame(mainPool, w.getPool()); @@ -766,7 +755,7 @@ public class RecursiveActionTest extends public void testWorkerGetPoolIndex() { final ForkJoinPool mainPool = mainPool(); RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread(); assertTrue(w.getPoolIndex() >= 0); @@ -781,7 +770,7 @@ public class RecursiveActionTest extends */ public void testSetRawResult() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { setRawResult(null); assertNull(getRawResult()); }}; @@ -789,11 +778,11 @@ public class RecursiveActionTest extends } /** - * A reinitialized task may be re-invoked + * A reinitialized normally completed task may be re-invoked */ public void testReinitialize() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); checkNotDone(f); @@ -809,11 +798,34 @@ public class RecursiveActionTest extends } /** + * A reinitialized abnormally completed task may be re-invoked + */ + public void testReinitializeAbnormal() { + RecursiveAction a = new CheckedRecursiveAction() { + protected void realCompute() { + FailingFibAction f = new FailingFibAction(8); + checkNotDone(f); + + for (int i = 0; i < 3; i++) { + try { + f.invoke(); + shouldThrow(); + } catch (FJException success) { + checkCompletedAbnormally(f, success); + } + f.reinitialize(); + checkNotDone(f); + } + }}; + testInvokeOnPool(mainPool(), a); + } + + /** * invoke task throws exception after invoking completeExceptionally */ public void testCompleteExceptionally() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.completeExceptionally(new FJException()); try { @@ -831,7 +843,7 @@ public class RecursiveActionTest extends */ public void testComplete() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); f.complete(null); assertNull(f.invoke()); @@ -846,7 +858,7 @@ public class RecursiveActionTest extends */ 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); @@ -863,7 +875,7 @@ public class RecursiveActionTest extends */ public void testInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); invokeAll(f); checkCompletedNormally(f); @@ -877,7 +889,7 @@ public class RecursiveActionTest extends */ 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); @@ -900,7 +912,7 @@ public class RecursiveActionTest extends */ 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); @@ -927,7 +939,7 @@ public class RecursiveActionTest extends */ 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; @@ -944,7 +956,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll2() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction f = new FibAction(8); FailingFibAction g = new FailingFibAction(9); try { @@ -962,7 +974,7 @@ public class RecursiveActionTest extends */ public void testAbnormalInvokeAll1() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FailingFibAction g = new FailingFibAction(9); try { invokeAll(g); @@ -979,7 +991,7 @@ public class RecursiveActionTest extends */ 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); @@ -998,7 +1010,7 @@ public class RecursiveActionTest extends */ 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); @@ -1022,7 +1034,7 @@ public class RecursiveActionTest extends */ public void testTryUnfork() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1041,7 +1053,7 @@ public class RecursiveActionTest extends */ public void testGetSurplusQueuedTaskCount() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction h = new FibAction(7); assertSame(h, h.fork()); FibAction g = new FibAction(9); @@ -1063,7 +1075,7 @@ public class RecursiveActionTest extends */ public void testPeekNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1084,7 +1096,7 @@ public class RecursiveActionTest extends */ public void testPollNextLocalTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1102,7 +1114,7 @@ public class RecursiveActionTest extends */ public void testPollTask() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1120,7 +1132,7 @@ public class RecursiveActionTest extends */ public void testPeekNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1140,7 +1152,7 @@ public class RecursiveActionTest extends */ public void testPollNextLocalTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1159,7 +1171,7 @@ public class RecursiveActionTest extends */ public void testPollTaskAsync() { RecursiveAction a = new CheckedRecursiveAction() { - public void realCompute() { + protected void realCompute() { FibAction g = new FibAction(9); assertSame(g, g.fork()); FibAction f = new FibAction(8); @@ -1172,4 +1184,47 @@ public class RecursiveActionTest extends testInvokeOnPool(asyncSingletonPool(), a); } + /** Demo from RecursiveAction javadoc */ + static class SortTask extends RecursiveAction { + final long[] array; final int lo, hi; + SortTask(long[] array, int lo, int hi) { + this.array = array; this.lo = lo; this.hi = hi; + } + SortTask(long[] array) { this(array, 0, array.length); } + protected void compute() { + if (hi - lo < THRESHOLD) + sortSequentially(lo, hi); + else { + int mid = (lo + hi) >>> 1; + invokeAll(new SortTask(array, lo, mid), + new SortTask(array, mid, hi)); + merge(lo, mid, hi); + } + } + // implementation details follow: + static final int THRESHOLD = 100; + void sortSequentially(int lo, int hi) { + Arrays.sort(array, lo, hi); + } + void merge(int lo, int mid, int hi) { + long[] buf = Arrays.copyOfRange(array, lo, mid); + for (int i = 0, j = lo, k = mid; i < buf.length; j++) + array[j] = (k == hi || buf[i] < array[k]) ? + buf[i++] : array[k++]; + } + } + + /** + * SortTask demo works as advertised + */ + public void testSortTaskDemo() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + long[] array = new long[1007]; + for (int i = 0; i < array.length; i++) + array[i] = rnd.nextLong(); + long[] arrayClone = array.clone(); + testInvokeOnPool(mainPool(), new SortTask(array)); + Arrays.sort(arrayClone); + assertTrue(Arrays.equals(array, arrayClone)); + } }