java.util.concurrent.RecursiveAction Java Examples

The following examples show how to use java.util.concurrent.RecursiveAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTask8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
    try (PoolCleaner cleaner = cleaner(pool)) {
        assertFalse(a.isDone());
        assertFalse(a.isCompletedNormally());
        assertFalse(a.isCompletedAbnormally());
        assertFalse(a.isCancelled());
        assertNull(a.getException());
        assertNull(a.getRawResult());

        assertNull(pool.invoke(a));

        assertTrue(a.isDone());
        assertTrue(a.isCompletedNormally());
        assertFalse(a.isCompletedAbnormally());
        assertFalse(a.isCancelled());
        assertNull(a.getException());
        assertNull(a.getRawResult());
    }
}
 
Example #2
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument invokes tasks
 */
public void testInvokeAll3() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction f = new FibAction(8);
            FibAction g = new FibAction(9);
            FibAction h = new FibAction(7);
            invokeAll(f, g, h);
            assertTrue(f.isDone());
            assertTrue(g.isDone());
            assertTrue(h.isDone());
            checkCompletedNormally(f);
            assertEquals(21, f.result);
            checkCompletedNormally(g);
            assertEquals(34, g.result);
            checkCompletedNormally(g);
            assertEquals(13, h.result);
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #3
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * A reinitialized normally completed task may be re-invoked
 */
public void testReinitialize() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction f = new FibAction(8);
            checkNotDone(f);

            for (int i = 0; i < 3; i++) {
                assertNull(f.invoke());
                assertEquals(21, f.result);
                checkCompletedNormally(f);
                f.reinitialize();
                checkNotDone(f);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #4
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTask8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testAbnormalForkGet(ForkJoinPool pool) {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FailingAsyncFib f = new FailingAsyncFib(8);
            assertSame(f, f.fork());
            try {
                f.get();
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
        }};
    testInvokeOnPool(pool, a);
}
 
Example #5
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTask8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testAbnormalForkTimedGet(ForkJoinPool pool) {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FailingAsyncFib f = new FailingAsyncFib(8);
            assertSame(f, f.fork());
            try {
                f.get(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
        }};
    testInvokeOnPool(pool, a);
}
 
Example #6
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument throws exception if any task does
 */
public void testAbnormalInvokeAll3() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            FailingAsyncFib g = new FailingAsyncFib(9);
            AsyncFib h = new AsyncFib(7);
            ForkJoinTask[] tasks = { f, g, h };
            shuffle(tasks);
            try {
                invokeAll(tasks);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #7
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * peekNextLocalTask returns least recent unexecuted task in async mode
 */
public void testPeekNextLocalTaskAsync() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib g = new AsyncFib(9);
            assertSame(g, g.fork());
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            assertSame(g, peekNextLocalTask());
            assertNull(f.join());
            helpQuiesce();
            checkCompletedNormally(f);
            assertEquals(34, g.number);
            checkCompletedNormally(g);
        }};
    testInvokeOnPool(asyncSingletonPool(), a);
}
 
Example #8
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTask8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testAbnormalInvokeAll2(ForkJoinPool pool) {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            FailingAsyncFib g = new FailingAsyncFib(9);
            ForkJoinTask[] tasks = { f, g };
            shuffle(tasks);
            try {
                invokeAll(tasks[0], tasks[1]);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
        }};
    testInvokeOnPool(pool, a);
}
 
Example #9
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * get of a forked task throws exception when task cancelled
 */
public void testCancelledForkGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FibAction f = new FibAction(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.get();
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #10
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTask8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * getSurplusQueuedTaskCount returns > 0 when
 * there are more tasks than threads
 */
public void testGetSurplusQueuedTaskCount() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib h = new AsyncFib(7);
            assertSame(h, h.fork());
            AsyncFib g = new AsyncFib(9);
            assertSame(g, g.fork());
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            assertTrue(getSurplusQueuedTaskCount() > 0);
            helpQuiesce();
            assertEquals(0, getSurplusQueuedTaskCount());
            f.checkCompletedNormally();
            g.checkCompletedNormally();
            h.checkCompletedNormally();
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #11
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTask8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * peekNextLocalTask returns most recent unexecuted task.
 */
public void testPeekNextLocalTask() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib g = new AsyncFib(9);
            assertSame(g, g.fork());
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            assertSame(f, peekNextLocalTask());
            assertNull(f.join());
            f.checkCompletedNormally();
            helpQuiesce();
            g.checkCompletedNormally();
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #12
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(t1, t2) throw exception if any task does
 */
public void testAbnormalInvokeAll2() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            FailingAsyncFib g = new FailingAsyncFib(9);
            ForkJoinTask[] tasks = { f, g };
            shuffle(tasks);
            try {
                invokeAll(tasks);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #13
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(collection) invokes all tasks in the collection
 */
public void testInvokeAllCollectionSingleton() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            AsyncFib g = new AsyncFib(9);
            AsyncFib h = new AsyncFib(7);
            HashSet set = new HashSet();
            set.add(f);
            set.add(g);
            set.add(h);
            invokeAll(set);
            assertEquals(21, f.number);
            assertEquals(34, g.number);
            assertEquals(13, h.number);
            checkCompletedNormally(f);
            checkCompletedNormally(g);
            checkCompletedNormally(h);
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #14
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * pollNextLocalTask returns most recent unexecuted task without
 * executing it
 */
public void testPollNextLocalTask() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib g = new AsyncFib(9);
            assertSame(g, g.fork());
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            assertSame(f, pollNextLocalTask());
            helpQuiesce();
            checkNotDone(f);
            assertEquals(34, g.number);
            checkCompletedNormally(g);
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #15
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument throws exception if any task does
 */
public void testAbnormalInvokeAll3Singleton() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            FailingAsyncFib g = new FailingAsyncFib(9);
            AsyncFib h = new AsyncFib(7);
            ForkJoinTask[] tasks = { f, g, h };
            shuffle(tasks);
            try {
                invokeAll(tasks);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed get of a forked task throws exception when task completes abnormally
 */
public void testAbnormalForkTimedGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FailingFibAction f = new FailingFibAction(8);
            assertSame(f, f.fork());
            try {
                f.get(5L, SECONDS);
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #17
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinPool8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * get of a forked task throws exception when task completes abnormally
 */
public void testAbnormalForkGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FailingFibAction f = new FailingFibAction(8);
            assertSame(f, f.fork());
            try {
                f.get();
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
        }};
    checkInvoke(a);
}
 
Example #18
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * peekNextLocalTask returns most recent unexecuted task.
 */
public void testPeekNextLocalTask() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib g = new AsyncFib(9);
            assertSame(g, g.fork());
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            assertSame(f, peekNextLocalTask());
            assertNull(f.join());
            checkCompletedNormally(f);
            helpQuiesce();
            checkCompletedNormally(g);
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #19
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinPool8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * join of a forked task throws exception when task cancelled
 */
public void testCancelledForkJoin() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction f = new FibAction(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.join();
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }};
    checkInvoke(a);
}
 
Example #20
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * pollNextLocalTask returns least recent unexecuted task without
 * executing it, in async mode
 */
public void testPollNextLocalTaskAsync() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib g = new AsyncFib(9);
            assertSame(g, g.fork());
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            assertSame(g, pollNextLocalTask());
            helpQuiesce();
            assertEquals(21, f.number);
            checkCompletedNormally(f);
            checkNotDone(g);
        }};
    testInvokeOnPool(asyncSingletonPool(), a);
}
 
Example #21
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinPool8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed get of a forked task throws exception when task cancelled
 */
public void testCancelledForkTimedGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FibAction f = new FibAction(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.get(5L, SECONDS);
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }};
    checkInvoke(a);
}
 
Example #22
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * get of a forked task throws exception when task completes abnormally
 */
public void testAbnormalForkGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FailingAsyncFib f = new FailingAsyncFib(8);
            assertSame(f, f.fork());
            try {
                f.get();
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #23
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument invokes tasks
 */
public void testInvokeAll3Singleton() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            AsyncFib g = new AsyncFib(9);
            AsyncFib h = new AsyncFib(7);
            invokeAll(f, g, h);
            assertEquals(21, f.number);
            assertEquals(34, g.number);
            assertEquals(13, h.number);
            checkCompletedNormally(f);
            checkCompletedNormally(g);
            checkCompletedNormally(h);
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #24
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * get of a forked task throws exception when task cancelled
 */
public void testCancelledForkGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            AsyncFib f = new AsyncFib(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                f.get();
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #25
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinPool8Test.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument invokes tasks
 */
public void testInvokeAll3() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction f = new FibAction(8);
            FibAction g = new FibAction(9);
            FibAction h = new FibAction(7);
            invokeAll(f, g, h);
            assertTrue(f.isDone());
            assertTrue(g.isDone());
            assertTrue(h.isDone());
            checkCompletedNormally(f);
            assertEquals(21, f.result);
            checkCompletedNormally(g);
            assertEquals(34, g.result);
            checkCompletedNormally(g);
            assertEquals(13, h.result);
        }};
    checkInvoke(a);
}
 
Example #26
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * peekNextLocalTask returns most recent unexecuted task.
 */
public void testPeekNextLocalTask() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction g = new FibAction(9);
            assertSame(g, g.fork());
            FibAction f = new FibAction(8);
            assertSame(f, f.fork());
            assertSame(f, peekNextLocalTask());
            assertNull(f.join());
            checkCompletedNormally(f);
            helpQuiesce();
            checkCompletedNormally(f);
            checkCompletedNormally(g);
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #27
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed get of a forked task throws exception when task completes abnormally
 */
public void testAbnormalForkTimedGet() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() throws Exception {
            FailingAsyncFib f = new FailingAsyncFib(8);
            assertSame(f, f.fork());
            try {
                f.get(LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #28
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * getSurplusQueuedTaskCount returns > 0 when
 * there are more tasks than threads
 */
public void testGetSurplusQueuedTaskCount() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction h = new FibAction(7);
            assertSame(h, h.fork());
            FibAction g = new FibAction(9);
            assertSame(g, g.fork());
            FibAction f = new FibAction(8);
            assertSame(f, f.fork());
            assertTrue(getSurplusQueuedTaskCount() > 0);
            helpQuiesce();
            assertEquals(0, getSurplusQueuedTaskCount());
            checkCompletedNormally(f);
            checkCompletedNormally(g);
            checkCompletedNormally(h);
        }};
    testInvokeOnPool(singletonPool(), a);
}
 
Example #29
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ForkJoinTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(collection) throws exception if any task does
 */
public void testAbnormalInvokeAllCollection() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FailingAsyncFib f = new FailingAsyncFib(8);
            AsyncFib g = new AsyncFib(9);
            AsyncFib h = new AsyncFib(7);
            ForkJoinTask[] tasks = { f, g, h };
            shuffle(tasks);
            try {
                invokeAll(Arrays.asList(tasks));
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(f, success);
            }
        }};
    testInvokeOnPool(mainPool(), a);
}
 
Example #30
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveActionTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(collection) invokes all tasks in the collection
 */
public void testInvokeAllCollection() {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            FibAction f = new FibAction(8);
            FibAction g = new FibAction(9);
            FibAction h = new FibAction(7);
            HashSet set = new HashSet();
            set.add(f);
            set.add(g);
            set.add(h);
            invokeAll(set);
            assertTrue(f.isDone());
            assertTrue(g.isDone());
            assertTrue(h.isDone());
            checkCompletedNormally(f);
            assertEquals(21, f.result);
            checkCompletedNormally(g);
            assertEquals(34, g.result);
            checkCompletedNormally(g);
            assertEquals(13, h.result);
        }};
    testInvokeOnPool(mainPool(), a);
}