Search in sources :

Example 21 with ForkJoinPool

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

the class ForkJoinPoolTest method testInvokeAny6.

/**
 * invokeAny(c) returns result of some task in c if at least one completes
 */
public void testInvokeAny6() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(new StringTask());
        String result = e.invokeAny(l);
        assertSame(TEST_STRING, result);
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 22 with ForkJoinPool

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

the class ForkJoinPoolTest method testSubmitNullCallable.

/**
 * submit(null callable) throws NullPointerException
 */
public void testSubmitNullCallable() {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        try {
            @SuppressWarnings("unused") Future<String> future = e.submit((Callable<String>) null);
            shouldThrow();
        } catch (NullPointerException success) {
        }
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 23 with ForkJoinPool

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

the class ForkJoinPoolTest method testInvokeAny2.

/**
 * invokeAny(empty collection) throws IllegalArgumentException
 */
public void testInvokeAny2() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        try {
            e.invokeAny(new ArrayList<Callable<String>>());
            shouldThrow();
        } catch (IllegalArgumentException success) {
        }
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Callable(java.util.concurrent.Callable) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 24 with ForkJoinPool

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

the class ForkJoinPoolTest method testSubmitRunnable2.

/**
 * Completed submit(runnable, result) returns result
 */
public void testSubmitRunnable2() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
        assertSame(TEST_STRING, future.get());
        assertTrue(future.isDone());
        assertFalse(future.isCancelled());
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 25 with ForkJoinPool

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

the class ForkJoinTask8Test method testAbnormalForkQuietlyJoin.

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

        protected void realCompute() {
            FailingAsyncFib f = new FailingAsyncFib(8);
            assertSame(f, f.fork());
            f.quietlyJoin();
            assertTrue(f.getException() instanceof FJException);
            checkCompletedAbnormally(f, f.getException());
        }
    };
    testInvokeOnPool(pool, a);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction)

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