Search in sources :

Example 76 with ForkJoinPool

use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.

the class ForkJoinTask8Test method testAbnormalInvokeAll3.

private void testAbnormalInvokeAll3(ForkJoinPool pool) {
    @SuppressWarnings("serial") 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[0], tasks[1], tasks[2]);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
        }
    };
    testInvokeOnPool(pool, a);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinTask(java8.util.concurrent.ForkJoinTask)

Example 77 with ForkJoinPool

use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.

the class ForkJoinTask8Test method testForkTimedGetNullTimeUnit.

private void testForkTimedGetNullTimeUnit(ForkJoinPool pool) {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() throws Exception {
            AsyncFib f = new AsyncFib(8);
            assertSame(f, f.fork());
            try {
                f.get(randomTimeout(), null);
                shouldThrow();
            } catch (NullPointerException success) {
            }
        }
    };
    testInvokeOnPool(pool, a);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 78 with ForkJoinPool

use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.

the class RecursiveActionTest method testGetPool.

/**
 * getPool of executing task returns its pool
 */
public void testGetPool() {
    final ForkJoinPool mainPool = mainPool();
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            assertSame(mainPool, getPool());
        }
    };
    testInvokeOnPool(mainPool, a);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 79 with ForkJoinPool

use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.

the class RecursiveActionTest method testJoinIgnoresInterruptsOutsideForkJoinPool.

/**
 * join/quietlyJoin of a forked task when not in ForkJoinPool
 * succeeds in the presence of interrupts
 */
public void testJoinIgnoresInterruptsOutsideForkJoinPool() {
    final SynchronousQueue<FibAction[]> sq = new SynchronousQueue<>();
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() throws InterruptedException {
            FibAction[] fibActions = new FibAction[6];
            for (int i = 0; i < fibActions.length; i++) fibActions[i] = new FibAction(8);
            fibActions[1].cancel(false);
            fibActions[2].completeExceptionally(new FJException());
            fibActions[4].cancel(true);
            fibActions[5].completeExceptionally(new FJException());
            for (int i = 0; i < fibActions.length; i++) fibActions[i].fork();
            sq.put(fibActions);
            helpQuiesce();
        }
    };
    Runnable r = new CheckedRunnable() {

        public void realRun() throws InterruptedException {
            FibAction[] fibActions = sq.take();
            FibAction f;
            final Thread myself = Thread.currentThread();
            // test join() ------------
            f = fibActions[0];
            assertFalse(ForkJoinTask.inForkJoinPool());
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            assertNull(f.join());
            assertTrue(Thread.interrupted());
            assertEquals(21, f.result);
            checkCompletedNormally(f);
            f = fibActions[1];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            try {
                f.join();
                shouldThrow();
            } catch (CancellationException success) {
                assertTrue(Thread.interrupted());
                checkCancelled(f);
            }
            f = fibActions[2];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            try {
                f.join();
                shouldThrow();
            } catch (FJException success) {
                assertTrue(Thread.interrupted());
                checkCompletedAbnormally(f, success);
            }
            // test quietlyJoin() ---------
            f = fibActions[3];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            f.quietlyJoin();
            assertTrue(Thread.interrupted());
            assertEquals(21, f.result);
            checkCompletedNormally(f);
            f = fibActions[4];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            f.quietlyJoin();
            assertTrue(Thread.interrupted());
            checkCancelled(f);
            f = fibActions[5];
            myself.interrupt();
            assertTrue(myself.isInterrupted());
            f.quietlyJoin();
            assertTrue(Thread.interrupted());
            assertTrue(f.getException() instanceof FJException);
            checkCompletedAbnormally(f, f.getException());
        }
    };
    Thread t;
    t = newStartedThread(r);
    testInvokeOnPool(mainPool(), a);
    awaitTermination(t);
    a.reinitialize();
    t = newStartedThread(r);
    testInvokeOnPool(singletonPool(), a);
    awaitTermination(t);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) CancellationException(java.util.concurrent.CancellationException) SynchronousQueue(java.util.concurrent.SynchronousQueue)

Example 80 with ForkJoinPool

use of java8.util.concurrent.ForkJoinPool in project streamsupport by stefan-zobel.

the class RecursiveActionTest method testWorkerGetPoolIndex.

/**
 * getPoolIndex of current thread in pool returns 0 <= value < poolSize
 */
public void testWorkerGetPoolIndex() {
    final ForkJoinPool mainPool = mainPool();
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread();
            assertTrue(w.getPoolIndex() >= 0);
        // pool size can shrink after assigning index, so cannot check
        // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
        }
    };
    testInvokeOnPool(mainPool, a);
}
Also used : ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Aggregations

ForkJoinPool (java8.util.concurrent.ForkJoinPool)55 ExecutorService (java.util.concurrent.ExecutorService)32 RecursiveAction (java8.util.concurrent.RecursiveAction)32 Callable (java.util.concurrent.Callable)20 ArrayList (java.util.ArrayList)15 ExecutionException (java.util.concurrent.ExecutionException)10 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)9 Future (java.util.concurrent.Future)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ForkJoinWorkerThread (java8.util.concurrent.ForkJoinWorkerThread)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ForkJoinTask (java8.util.concurrent.ForkJoinTask)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CancellationException (java.util.concurrent.CancellationException)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1 TimeoutException (java.util.concurrent.TimeoutException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Predicate (java8.util.function.Predicate)1