Search in sources :

Example 46 with ForkJoinPool

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

the class ForkJoinPoolTest method testTimedInvokeAllNullTimeUnit.

/**
 * timed invokeAll(null time unit) throws NullPointerException
 */
public void testTimedInvokeAllNullTimeUnit() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        try {
            e.invokeAll(l, randomTimeout(), null);
            shouldThrow();
        } catch (NullPointerException success) {
        }
    } 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 47 with ForkJoinPool

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

the class ForkJoinPoolTest method testBlockingForkJoinTask.

/**
 * Pool maintains parallelism when using ManagedBlocker
 */
public void testBlockingForkJoinTask() throws Throwable {
    ForkJoinPool p = new ForkJoinPool(4);
    try {
        ReentrantLock lock = new ReentrantLock();
        ManagedLocker locker = new ManagedLocker(lock);
        ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
        p.execute(f);
        assertEquals(6765, (int) f.get());
    } finally {
        // don't wait out shutdown
        p.shutdownNow();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 48 with ForkJoinPool

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

the class ForkJoinPoolTest method testTimedInvokeAll5.

/**
 * timed invokeAll(c) returns results of all completed tasks in c
 */
public void testTimedInvokeAll5() throws Throwable {
    ForkJoinPool 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());
        List<Future<String>> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
        assertEquals(2, futures.size());
        for (Future<String> future : futures) assertSame(TEST_STRING, future.get());
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 49 with ForkJoinPool

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

the class ForkJoinPoolTest method testInterruptedSubmit.

/**
 * submit(callable).get() throws InterruptedException if interrupted
 */
public void testInterruptedSubmit() throws InterruptedException {
    final CountDownLatch submitted = new CountDownLatch(1);
    final CountDownLatch quittingTime = new CountDownLatch(1);
    final Callable<Void> awaiter = new CheckedCallable<Void>() {

        public Void realCall() throws InterruptedException {
            assertTrue(quittingTime.await(2 * LONG_DELAY_MS, MILLISECONDS));
            return null;
        }
    };
    final ExecutorService p = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p, quittingTime);
        Thread t = new Thread(new CheckedInterruptedRunnable() {

            public void realRun() throws Exception {
                Future<Void> future = p.submit(awaiter);
                submitted.countDown();
                future.get();
            }
        });
        t.start();
        await(submitted);
        t.interrupt();
        awaitTermination(t);
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ForkJoinPool(java8.util.concurrent.ForkJoinPool) ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread)

Example 50 with ForkJoinPool

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

the class ForkJoinPoolTest method testExecuteRunnable.

// FJ Versions of AbstractExecutorService tests
/**
 * execute(runnable) runs it to completion
 */
public void testExecuteRunnable() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        final AtomicBoolean done = new AtomicBoolean(false);
        Future<?> future = e.submit(new CheckedRunnable() {

            public void realRun() {
                done.set(true);
            }
        });
        assertNull(future.get());
        assertNull(future.get(randomExpiredTimeout(), randomTimeUnit()));
        assertTrue(done.get());
        assertTrue(future.isDone());
        assertFalse(future.isCancelled());
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) 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