/** * Performs the given task, returning its result upon completion. * If the computation encounters an unchecked Exception or Error, * it is rethrown as the outcome of this invocation. Rethrown * exceptions behave in the same way as regular exceptions, but, * when possible, contain stack traces (as displayed for example * using {@code ex.printStackTrace()}) of both the current thread * as well as the thread actually encountering the exception; * minimally only the latter. * * @param task the task * @return the task's result * @throws NullPointerException if the task is null * @throws RejectedExecutionException if the task cannot be * scheduled for execution */ public <T> T invoke(ForkJoinTask<T> task) { if (task == null) throw new NullPointerException(); externalPush(task); return task.join(); }
rootTask.join();
private void runAll() { for (ForkJoinTask<?> task: tasks) { task.fork(); } Throwable failure = null; for (ForkJoinTask<?> task: tasks) { try { task.join(); } catch (Throwable th) { logger.error("job " + task + " failed with exception", th); if (failure == null) { failure = th; } else { failure.addSuppressed(th); } } } if (failure != null) { throw new UncheckedExecutionException("Error running a subjob", failure); } } }
default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){ return fj.submit(() -> foldParallel(fn)).join(); }
<T> T terminate(Supplier<T> terminalOperation) { return fjp.submit(terminalOperation::get).join(); }
<T, U> T terminate(U value, Function<U, T> terminalOperation) { return fjp.submit(() -> terminalOperation.apply(value)).join(); }
/** * Performs the given task, returning its result upon completion. * If the computation encounters an unchecked Exception or Error, * it is rethrown as the outcome of this invocation. Rethrown * exceptions behave in the same way as regular exceptions, but, * when possible, contain stack traces (as displayed for example * using {@code ex.printStackTrace()}) of both the current thread * as well as the thread actually encountering the exception; * minimally only the latter. * * @param task the task * @return the task's result * @throws NullPointerException if the task is null * @throws RejectedExecutionException if the task cannot be * scheduled for execution */ public <T> T invoke(ForkJoinTask<T> task) { if (task == null) throw new NullPointerException(); externalPush(task); return task.join(); }
/** * Performs the given task, returning its result upon completion. * If the computation encounters an unchecked Exception or Error, * it is rethrown as the outcome of this invocation. Rethrown * exceptions behave in the same way as regular exceptions, but, * when possible, contain stack traces (as displayed for example * using {@code ex.printStackTrace()}) of both the current thread * as well as the thread actually encountering the exception; * minimally only the latter. * * @param task the task * @return the task's result * @throws NullPointerException if the task is null * @throws RejectedExecutionException if the task cannot be * scheduled for execution */ public <T> T invoke(ForkJoinTask<T> task) { if (task == null) throw new NullPointerException(); externalPush(task); return task.join(); }
protected void initializeIndexes(SearchIntegrator si, int indexSize) { ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); List<ForkJoinTask<?>> tasks = new ArrayList<>(); for ( Dataset<? extends AbstractBookEntity> dataset : datasets ) { ForkJoinTask<?> task = forkJoinPool.submit( () -> SearchIntegratorHelper.preindexEntities( si, dataset, IntStream.range( 0, indexSize ) ) ); tasks.add( task ); } for ( ForkJoinTask<?> task : tasks ) { task.join(); } }
public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) { t2.fork(); t1.invoke(); t2.join(); }
default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){ return fj.submit(() -> foldParallel(fn)).join(); }
default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){ return fj.submit(() -> foldParallel(fn)).join(); }
/** * Use the fork-join framework to recursively split the input list * and return a list of lists of SearchResults that contain all * matching phrases in the input list. */ private List<List<SearchResults>> splitInputList(int splitPos) { // Create and fork a new SearchWithForkJoinTask that // concurrently handles the "left hand" part of the input, // while "this" handles the "right hand" part of the input. ForkJoinTask<List<List<SearchResults>>> leftTask = forkLeftTask(splitPos, mMinSplitSize); List<List<SearchResults>> rightResult = computeRightTask(splitPos, mMinSplitSize); // Wait and join the results from the left task., List<List<SearchResults>> leftResult = leftTask.join(); // sConcatenate the left result with the right result. leftResult.addAll(rightResult); // Return the result. return leftResult; }
@Override public <T> CompletableFuture<T> runAsyncInIsolatedTransaction(Function<? super Transaction, ? extends CompletableFuture<T>> retryable) { ForkJoinTask<? extends CompletableFuture<T>> task = ForkJoinPool.commonPool().submit(() -> { try (MemoryTransaction tx = createTransaction(false)) { return retryable.apply(tx); } }); return task.join(); }
@Override protected void compute() { parents.forEach(evId -> { try { IEvent ev = global.getEvMgr().get(evId); if (ev.getDepth() >= minDepth && sink.offer(ev) && !childLimit.contains(ev.getId())) { List<String> parents = ev.getParents().stream().map(IEventIdReference::getEventId).collect(Collectors.toList()); new GetChildEventsRecursiveAction(parents, childLimit, minDepth, sink).fork().join(); } } catch (IllegalArgumentException e) { log.debug("Event {} is not known to use, skipping", evId); } }); } }
@Override public Task.Wait submit(Task task) { ForkJoinTask<?> wrapped = ForkJoinTask.adapt(() -> task.execute(this)).fork(); return () -> wrapped.join(); } },
class Fibonacci extends RecursiveTask<Integer> { final int n; Fibonacci(int n) { this.n = n; } Integer compute() { if (n <= 1) return n; Fibonacci f1 = new Fibonacci(n - 1); f1.fork(); Fibonacci f2 = new Fibonacci(n - 2); return f2.compute() + f1.join(); } }
class Fibonacci extends RecursiveTask<Long> { private final long n; Fibonacci(long n) { this.n = n; } public Long compute() { if (n <= 1) { return n; } Fibonacci f1 = new Fibonacci(n - 1); f1.fork(); Fibonacci f2 = new Fibonacci(n - 2); return f2.compute() + f1.join(); } }
@Test(expected = CPUReactiveAuditException.class) public void join() throws InterruptedException, ExecutionException { TestTools.strict.commit(); e.join(); }
class FibonacciBigSubtasks extends RecursiveTask<Long> { private final long n; FibonacciBigSubtasks(long n) { this.n = n; } public Long compute() { return fib(n); } private long fib(long n) { if (n <= 1) { return 1; } if (n > 10 && getSurplusQueuedTaskCount() < 2) { final FibonacciBigSubtasks f1 = new FibonacciBigSubtasks(n - 1); final FibonacciBigSubtasks f2 = new FibonacciBigSubtasks(n - 2); f1.fork(); return f2.compute() + f1.join(); } else { return fib(n - 1) + fib(n - 2); } } }