Java Code Examples for com.google.common.util.concurrent.Futures#submitAsync()

The following examples show how to use com.google.common.util.concurrent.Futures#submitAsync() . 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: GrpcClient.java    From etcd-java with Apache License 2.0 6 votes vote down vote up
protected <ReqT,R> ListenableFuture<R> fuCall(MethodDescriptor<ReqT,R> method, ReqT request,
        CallOptions callOptions, long timeoutMs) {
    if (timeoutMs <= 0L) {
        timeoutMs = defaultTimeoutMs;
    }
    if (timeoutMs > 0L) {
        Deadline deadline = callOptions.getDeadline();
        Deadline timeoutDeadline = Deadline.after(timeoutMs, MILLISECONDS);
        if (deadline == null || timeoutDeadline.isBefore(deadline)) {
            callOptions = callOptions.withDeadline(timeoutDeadline);
        } else if (deadline.isExpired()) {
            return Futures.immediateFailedFuture(
                    Status.DEADLINE_EXCEEDED.asRuntimeException());
        }
    }
    final CallOptions finalCallOpts = callOptions;
    return sendViaEventLoop && !isEventThread.satisfied()
            ? Futures.submitAsync(() -> fuCall(method, request, finalCallOpts), ses)
                    : fuCall(method, request, finalCallOpts);
}
 
Example 2
Source File: JobLimiter.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a new task to the queue. JobLimiter uses the provided service only for invoking the
 * callable itself.
 */
public <V> ListenableFuture<V> schedule(
    ListeningExecutorService service, ThrowingSupplier<ListenableFuture<V>, Exception> callable) {
  // To help prevent stack overflows either this callable needs to be forced to be async, or the
  // release() call when it's finished needs to be. It's easier to do it here.
  ThrowingSupplier<ListenableFuture<V>, Exception> safeCallable =
      () -> Futures.submitAsync(callable::get, service);

  if (semaphore.tryAcquire()) {
    return send(safeCallable);
  }
  ListenableFuture<V> enqueued = enqueue(safeCallable);

  // It's possible that all running jobs finished after we failed to acquire the semaphore and
  // before we enqueued the callable. To not get stuck in the queue forever, try again.
  if (semaphore.tryAcquire()) {
    release();
  }
  return enqueued;
}
 
Example 3
Source File: SkyQueryEnvironment.java    From bazel with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <R> ListenableFuture<R> safeSubmitAsync(QueryTaskAsyncCallable<R> callable) {
  try {
    return Futures.submitAsync(() -> (ListenableFuture<R>) callable.call(), executor);
  } catch (RejectedExecutionException e) {
    return Futures.immediateCancelledFuture();
  }
}
 
Example 4
Source File: JavaAsync.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Finds factorial of a number using Guava's Futures.submitAsync()
 * @param number
 * @return
 */
@Loggable
public static ListenableFuture<Long> factorialUsingGuavaFutures(int number) {
    ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
    AsyncCallable<Long> asyncCallable = Callables.asAsyncCallable(new Callable<Long>() {
        public Long call() {
            return factorial(number);
        }
    }, service);
    return Futures.submitAsync(asyncCallable, service);
}
 
Example 5
Source File: LocalFallbackStrategy.java    From buck with Apache License 2.0 5 votes vote down vote up
private void fallbackBuildToLocalStrategy() {
  if (hasCancellationBeenRequested) {
    completeCombinedFutureWithException(
        new RemoteActionCancelledException(
            "Unable to fall back to Local Strategy, execution has been cancelled"),
        Result.CANCELLED,
        Result.NOT_RUN);
    return;
  }
  ListenableFuture<Optional<BuildResult>> future =
      Futures.submitAsync(
          strategyContext::runWithDefaultBehavior, strategyContext.getExecutorService());
  localStrategyBuildResult = Optional.of(future);
  future.addListener(() -> onLocalBuildFinished(future), MoreExecutors.directExecutor());
}