Search in sources :

Example 1 with ForkJoinPool

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

the class ForkJoinPoolTest method testInvokeAll2.

/**
 * invokeAll(empty collection) returns empty list
 */
public void testInvokeAll2() throws InterruptedException {
    ExecutorService e = new ForkJoinPool(1);
    final Collection<Callable<String>> emptyCollection = Collections.emptyList();
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Future<String>> r = e.invokeAll(emptyCollection);
        assertTrue(r.isEmpty());
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 2 with ForkJoinPool

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

the class ForkJoinPoolTest method testSetUncaughtExceptionHandler.

/**
 * setUncaughtExceptionHandler changes handler for uncaught exceptions.
 *
 * Additionally tests: Overriding ForkJoinWorkerThread.onStart
 * performs its defined action
 */
public void testSetUncaughtExceptionHandler() throws InterruptedException {
    final CountDownLatch uehInvoked = new CountDownLatch(1);
    final Thread.UncaughtExceptionHandler ueh = new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(Thread t, Throwable e) {
            threadAssertTrue(e instanceof MyError);
            threadAssertTrue(t instanceof FailingFJWSubclass);
            uehInvoked.countDown();
        }
    };
    ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), ueh, false);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p);
        assertSame(ueh, p.getUncaughtExceptionHandler());
        try {
            p.execute(new FibTask(8));
            await(uehInvoked);
        } finally {
            // failure might have prevented processing task
            p.shutdownNow();
        }
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 3 with ForkJoinPool

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

the class ForkJoinPoolTest method testSubmitForkJoinTask.

/**
 * Completed submit(ForkJoinTask) returns result
 */
public void testSubmitForkJoinTask() throws Throwable {
    ForkJoinPool p = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p);
        ForkJoinTask<Integer> f = p.submit(new FibTask(8));
        assertEquals(21, (int) f.get());
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 4 with ForkJoinPool

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

the class ForkJoinPoolTest method testTimedInvokeAll1.

/**
 * timed invokeAll(null) throws NullPointerException
 */
public void testTimedInvokeAll1() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        try {
            e.invokeAll(null, randomTimeout(), randomTimeUnit());
            shouldThrow();
        } catch (NullPointerException success) {
        }
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 5 with ForkJoinPool

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

the class ForkJoinPoolTest method testInvokeAll4.

/**
 * get of returned element of invokeAll(c) throws
 * ExecutionException on failed task
 */
public void testInvokeAll4() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Callable<String>> l = new ArrayList<>();
        l.add(new NPETask());
        List<Future<String>> futures = e.invokeAll(l);
        assertEquals(1, futures.size());
        try {
            futures.get(0).get();
            shouldThrow();
        } catch (ExecutionException success) {
            assertTrue(success.getCause() instanceof NullPointerException);
        }
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Callable(java.util.concurrent.Callable) 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