Java Code Examples for java.util.concurrent.ForkJoinTask#invokeAll()

The following examples show how to use java.util.concurrent.ForkJoinTask#invokeAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MiGzInputStream.java    From migz with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void close() throws IOException {
  _eosCompressed = true; // will (eventually) result in all tasks stopping

  _decompressedBufferPool.offer(EMPTY_BYTE_ARRAY); { } // there might be a thread waiting on a buffer

  // try to cancel/interrupt all tasks, then wait for them to finish
  DecompressTask[] activeTasks = _activeTasks.keySet().toArray(new DecompressTask[0]);
  Arrays.stream(activeTasks).forEach(task -> task.cancel(true));

  try {
    ForkJoinTask.invokeAll(activeTasks); // wait until our tasks have completed; may rethrow worker exceptions
  } catch (Exception e) {
    // do nothing--exceptions are expected here
  } finally {
    _inputStream.close(); // now safe to close underlying stream
    _taskStatePopulation.forEach(TaskState::close); // release task state resources

    try {
      if (_ownsThreadPool) {
        _threadPool.shutdown();
      }
    } catch (Exception ignored) { } // ignore exceptions while shutting down
  }
}
 
Example 2
Source File: ForkJoinFibonacci.java    From java-concurrency with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void compute() {

    long n = number;
    if (n <= threshold) {
        number = fib(n);
    } else {
        ForkJoinFibonacci f1 = new ForkJoinFibonacci(n - 1);
        ForkJoinFibonacci f2 = new ForkJoinFibonacci(n - 2);
        ForkJoinTask.invokeAll(f1, f2);
        number = f1.number + f2.number;
    }
}
 
Example 3
Source File: ConcurrencyTool.java    From minperf with Apache License 2.0 5 votes vote down vote up
public void invokeAll(ForkJoinTask<?>... tasks) {
    if (pool != null) {
        ForkJoinTask.invokeAll(tasks);
        return;
    }
    for (ForkJoinTask<?> t : tasks) {
        t.invoke();
    }
}
 
Example 4
Source File: DirSize.java    From java-fork-join-example with Apache License 2.0 5 votes vote down vote up
@Override
protected void compute() {
  DirSize.LOGGER.debug("Computing size of: {}", file);

  if (file.isFile()) {
    sizeAccumulator.addAndGet(file.length());
  } else {
    final File[] children = file.listFiles();
    if (children != null) {
      for (final File child : children) {
        ForkJoinTask.invokeAll(new SizeOfFileAction(child, sizeAccumulator));
      }
    }
  }
}
 
Example 5
Source File: WakeSharedPool.java    From reef with Apache License 2.0 5 votes vote down vote up
public void submit(final ForkJoinTask<?> t) {
  if (ForkJoinTask.inForkJoinPool()) {
    ForkJoinTask.invokeAll(t);
    // alternatively just pool().pool.execute(t), which simply forces it to be this pool
    // (right now we expect only one anyway)
  } else {
    pool.submit(t);
  }
}
 
Example 6
Source File: PrimeNumbers.java    From tutorials with MIT License 5 votes vote down vote up
@Override
protected void compute() {
    if (((upperBound + 1) - lowerBound) > granularity) {
        ForkJoinTask.invokeAll(subTasks());
    } else {
        findPrimeNumbers();
    }
}
 
Example 7
Source File: CustomRecursiveAction.java    From tutorials with MIT License 5 votes vote down vote up
@Override
protected void compute() {

    if (workLoad.length() > THRESHOLD) {
        ForkJoinTask.invokeAll(createSubtasks());
    } else {
        processing(workLoad);
    }
}