Search in sources :

Example 11 with ForkJoinTask

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

the class ForkJoinPoolTest method testDrainTasksTo.

/**
 * drainTasksTo transfers unexecuted submitted tasks, if present
 */
public void testDrainTasksTo() {
    final CountDownLatch done = new CountDownLatch(1);
    SubFJP p = new SubFJP();
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p);
        ForkJoinTask<?> a = p.submit(awaiter(done));
        ForkJoinTask<?> b = p.submit(awaiter(done));
        ForkJoinTask<?> c = p.submit(awaiter(done));
        ArrayList<ForkJoinTask<?>> al = new ArrayList();
        p.drainTasksTo(al);
        assertTrue(al.size() > 0);
        for (ForkJoinTask<?> r : al) {
            assertTrue(r == a || r == b || r == c);
            assertFalse(r.isDone());
        }
        done.countDown();
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ForkJoinTask(java8.util.concurrent.ForkJoinTask) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with ForkJoinTask

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

the class ForkJoinPoolTest method testSubmitAfterShutdown.

/**
 * A task submitted after shutdown is rejected
 */
public void testSubmitAfterShutdown() {
    ForkJoinPool p = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p);
        p.shutdown();
        assertTrue(p.isShutdown());
        try {
            @SuppressWarnings("unused") ForkJoinTask<Integer> f = p.submit(new FibTask(8));
            shouldThrow();
        } catch (RejectedExecutionException success) {
        }
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 13 with ForkJoinTask

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

the class ForkJoinTask8Test method testAbnormalInvokeAll2.

private void testAbnormalInvokeAll2(ForkJoinPool pool) {
    @SuppressWarnings("serial") 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);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinTask(java8.util.concurrent.ForkJoinTask)

Example 14 with ForkJoinTask

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

the class ForkJoinTask8Test method testPollSubmission.

// jdk9
/**
 * pollSubmission returns unexecuted submitted task, if present
 */
public void testPollSubmission() {
    final CountDownLatch done = new CountDownLatch(1);
    final ForkJoinTask<?> a = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinTask<?> b = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinTask<?> c = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinPool p = singletonPool();
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p, done);
        Thread external = new Thread(new CheckedRunnable() {

            public void realRun() {
                p.execute(a);
                p.execute(b);
                p.execute(c);
            }
        });
        @SuppressWarnings("serial") RecursiveAction s = new CheckedRecursiveAction() {

            protected void realCompute() {
                external.start();
                try {
                    external.join();
                } catch (Exception ex) {
                    threadUnexpectedException(ex);
                }
                assertTrue(p.hasQueuedSubmissions());
                assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
                ForkJoinTask<?> r = ForkJoinTask.pollSubmission();
                assertTrue(r == a || r == b || r == c);
                assertFalse(r.isDone());
            }
        };
        p.invoke(s);
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) RecursiveAction(java8.util.concurrent.RecursiveAction) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ForkJoinPool(java8.util.concurrent.ForkJoinPool) ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread)

Example 15 with ForkJoinTask

use of java8.util.concurrent.ForkJoinTask 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)

Aggregations

ForkJoinTask (java8.util.concurrent.ForkJoinTask)10 RecursiveAction (java8.util.concurrent.RecursiveAction)10 ForkJoinPool (java8.util.concurrent.ForkJoinPool)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ForkJoinWorkerThread (java8.util.concurrent.ForkJoinWorkerThread)1