Search in sources :

Example 16 with ForkJoinPool

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

the class ForkJoinPoolTest method testSubmitFailedPrivilegedExceptionAction.

/**
 * A submitted failed privileged exception action reports exception
 */
public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
    final Callable<Object> callable = Executors.callable(new PrivilegedExceptionAction<Object>() {

        public Object run() {
            throw new IndexOutOfBoundsException();
        }
    });
    Runnable r = new CheckedRunnable() {

        public void realRun() throws Exception {
            ExecutorService e = new ForkJoinPool(1);
            PoolCleaner cleaner = null;
            try {
                cleaner = cleaner(e);
                Future<?> future = e.submit(callable);
                try {
                    future.get();
                    shouldThrow();
                } catch (ExecutionException success) {
                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
                }
            } finally {
                if (cleaner != null) {
                    cleaner.close();
                }
            }
        }
    };
    runWithPermissions(r, new RuntimePermission("modifyThread"));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 17 with ForkJoinPool

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

the class ForkJoinPoolTest method testTimedInvokeAll2.

/**
 * timed invokeAll(empty collection) returns empty list
 */
public void testTimedInvokeAll2() 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, randomTimeout(), randomTimeUnit());
        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 18 with ForkJoinPool

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

the class ForkJoinPoolTest method testInvokeAll3.

/**
 * invokeAll(c) throws NullPointerException if c has null elements
 */
public void testInvokeAll3() throws InterruptedException {
    ExecutorService e = new ForkJoinPool(1);
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(e);
        List<Callable<String>> l = new ArrayList<>();
        l.add(new StringTask());
        l.add(null);
        try {
            e.invokeAll(l);
            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 19 with ForkJoinPool

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

the class ForkJoinPoolTest method testSubmitPrivilegedExceptionAction.

/**
 * A submitted privileged exception action runs to completion
 */
public void testSubmitPrivilegedExceptionAction() throws Exception {
    final Callable<Object> callable = Executors.callable(new PrivilegedExceptionAction<Object>() {

        public Object run() {
            return TEST_STRING;
        }
    });
    Runnable r = new CheckedRunnable() {

        public void realRun() throws Exception {
            ExecutorService e = new ForkJoinPool(1);
            PoolCleaner cleaner = null;
            try {
                cleaner = cleaner(e);
                Future<?> future = e.submit(callable);
                assertSame(TEST_STRING, future.get());
            } finally {
                if (cleaner != null) {
                    cleaner.close();
                }
            }
        }
    };
    runWithPermissions(r, new RuntimePermission("modifyThread"));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 20 with ForkJoinPool

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

the class ForkJoinPoolTest method testSubmitPrivilegedAction.

/**
 * A submitted privileged action runs to completion
 */
public void testSubmitPrivilegedAction() throws Exception {
    final Callable<Object> callable = Executors.callable(new PrivilegedAction<Object>() {

        public Object run() {
            return TEST_STRING;
        }
    });
    Runnable r = new CheckedRunnable() {

        public void realRun() throws Exception {
            ExecutorService e = new ForkJoinPool(1);
            PoolCleaner cleaner = null;
            try {
                cleaner = cleaner(e);
                Future<?> future = e.submit(callable);
                assertSame(TEST_STRING, future.get());
            } finally {
                if (cleaner != null) {
                    cleaner.close();
                }
            }
        }
    };
    runWithPermissions(r, new RuntimePermission("modifyThread"));
}
Also used : 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