@Test public void testTrySetError() { TaskCompletionSource<Void> tcs = new TaskCompletionSource<>(); Task<Void> task = tcs.getTask(); assertFalse(task.isCompleted()); Exception exception = new RuntimeException("DISQUALIFIED"); boolean success = tcs.trySetError(exception); assertTrue(success); assertTrue(task.isCompleted()); assertEquals(exception, task.getError()); }
boolean trySetError(Exception error) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.error = error; errorHasBeenObserved = false; lock.notifyAll(); runContinuations(); if (!errorHasBeenObserved && getUnobservedExceptionHandler() != null) unobservedErrorNotifier = new UnobservedErrorNotifier(this); return true; } }
Task { boolean trySetError(Exception error) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.error = error; errorHasBeenObserved = false; lock.notifyAll(); runContinuations(); if (!errorHasBeenObserved && getUnobservedExceptionHandler() != null) unobservedErrorNotifier = new UnobservedErrorNotifier(this); return true; } } }
Task { boolean trySetError(Exception error) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.error = error; errorHasBeenObserved = false; lock.notifyAll(); runContinuations(); if (!errorHasBeenObserved && getUnobservedExceptionHandler() != null) unobservedErrorNotifier = new UnobservedErrorNotifier(this); return true; } } Task(); private  Task(TResult result); private  Task(boolean cancelled); }
Task { boolean trySetError(Exception error) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.error = error; errorHasBeenObserved = false; lock.notifyAll(); runContinuations(); if (!errorHasBeenObserved && getUnobservedExceptionHandler() != null) unobservedErrorNotifier = new UnobservedErrorNotifier(this); return true; } } Task(); private  Task(TResult result); private  Task(boolean cancelled); static UnobservedExceptionHandler getUnobservedExceptionHandler(); static void setUnobservedExceptionHandler(UnobservedExceptionHandler eh); static Task<TResult>.TaskCompletionSource create(); boolean isCompleted(); boolean isCancelled(); boolean isFaulted(); TResult getResult(); Exception getError(); void waitForCompletion(); boolean waitForCompletion(long duration, TimeUnit timeUnit); @SuppressWarnings("unchecked") static Task<TResult> forResult(TResult value); static Task<TResult> forError(Exception error); @SuppressWarnings("unchecked") static Task<TResult> cancelled(); static Task<Void> delay(long delay); static Task<Void> delay(long delay, CancellationToken cancellationToken); Task<TOut> cast(); Task<Void> makeVoid(); static Task<TResult> callInBackground(Callable<TResult> callable); static Task<TResult> callInBackground(Callable<TResult> callable, CancellationToken ct); static Task<TResult> call(final Callable<TResult> callable, Executor executor); static Task<TResult> call(final Callable<TResult> callable, Executor executor,
      final CancellationToken ct); static Task<TResult> call(final Callable<TResult> callable); static Task<TResult> call(final Callable<TResult> callable, CancellationToken ct); static Task<Task<TResult>> whenAnyResult(Collection<? extends Task<TResult>> tasks); @SuppressWarnings("unchecked") static Task<Task<?>> whenAny(Collection<? extends Task<?>> tasks); static Task<List<TResult>> whenAllResult(final Collection<? extends Task<TResult>> tasks); static Task<Void> whenAll(Collection<? extends Task<?>> tasks); Task<Void> continueWhile(Callable<Boolean> predicate,
      Continuation<Void, Task<Void>> continuation); Task<Void> continueWhile(Callable<Boolean> predicate,
      Continuation<Void, Task<Void>> continuation, CancellationToken ct); Task<Void> continueWhile(final Callable<Boolean> predicate,
      final Continuation<Void, Task<Void>> continuation, final Executor executor); Task<Void> continueWhile(final Callable<Boolean> predicate,
      final Continuation<Void, Task<Void>> continuation, final Executor executor,
      final CancellationToken ct); Task<TContinuationResult> continueWith(
      final Continuation<TResult, TContinuationResult> continuation, final Executor executor); Task<TContinuationResult> continueWith(
      final Continuation<TResult, TContinuationResult> continuation, final Executor executor,
      final CancellationToken ct); Task<TContinuationResult> continueWith(
      Continuation<TResult, TContinuationResult> continuation); Task<TContinuationResult> continueWith(
      Continuation<TResult, TContinuationResult> continuation, CancellationToken ct); Task<TContinuationResult> continueWithTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, final Executor executor); Task<TContinuationResult> continueWithTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, final Executor executor,
      final CancellationToken ct); Task<TContinuationResult> continueWithTask(
      Continuation<TResult, Task<TContinuationResult>> continuation); Task<TContinuationResult> continueWithTask(
      Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation, Executor executor); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation, Executor executor,
      final CancellationToken ct); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation, CancellationToken ct); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor,
      final CancellationToken ct); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation,
      CancellationToken ct); }
Task { boolean trySetError(Exception error) { synchronized (lock) { if (complete) { return false; } complete = true; Task.this.error = error; errorHasBeenObserved = false; lock.notifyAll(); runContinuations(); if (!errorHasBeenObserved && getUnobservedExceptionHandler() != null) unobservedErrorNotifier = new UnobservedErrorNotifier(this); return true; } } Task(); private  Task(TResult result); private  Task(boolean cancelled); static UnobservedExceptionHandler getUnobservedExceptionHandler(); static void setUnobservedExceptionHandler(UnobservedExceptionHandler eh); static Task<TResult>.TaskCompletionSource create(); boolean isCompleted(); boolean isCancelled(); boolean isFaulted(); TResult getResult(); Exception getError(); void waitForCompletion(); boolean waitForCompletion(long duration, TimeUnit timeUnit); @SuppressWarnings("unchecked") static Task<TResult> forResult(TResult value); static Task<TResult> forError(Exception error); @SuppressWarnings("unchecked") static Task<TResult> cancelled(); static Task<Void> delay(long delay); static Task<Void> delay(long delay, CancellationToken cancellationToken); Task<TOut> cast(); Task<Void> makeVoid(); static Task<TResult> callInBackground(Callable<TResult> callable); static Task<TResult> callInBackground(Callable<TResult> callable, CancellationToken ct); static Task<TResult> call(final Callable<TResult> callable, Executor executor); static Task<TResult> call(final Callable<TResult> callable, Executor executor,
      final CancellationToken ct); static Task<TResult> call(final Callable<TResult> callable); static Task<TResult> call(final Callable<TResult> callable, CancellationToken ct); static Task<Task<TResult>> whenAnyResult(Collection<? extends Task<TResult>> tasks); @SuppressWarnings("unchecked") static Task<Task<?>> whenAny(Collection<? extends Task<?>> tasks); static Task<List<TResult>> whenAllResult(final Collection<? extends Task<TResult>> tasks); static Task<Void> whenAll(Collection<? extends Task<?>> tasks); Task<Void> continueWhile(Callable<Boolean> predicate,
      Continuation<Void, Task<Void>> continuation); Task<Void> continueWhile(Callable<Boolean> predicate,
      Continuation<Void, Task<Void>> continuation, CancellationToken ct); Task<Void> continueWhile(final Callable<Boolean> predicate,
      final Continuation<Void, Task<Void>> continuation, final Executor executor); Task<Void> continueWhile(final Callable<Boolean> predicate,
      final Continuation<Void, Task<Void>> continuation, final Executor executor,
      final CancellationToken ct); Task<TContinuationResult> continueWith(
      final Continuation<TResult, TContinuationResult> continuation, final Executor executor); Task<TContinuationResult> continueWith(
      final Continuation<TResult, TContinuationResult> continuation, final Executor executor,
      final CancellationToken ct); Task<TContinuationResult> continueWith(
      Continuation<TResult, TContinuationResult> continuation); Task<TContinuationResult> continueWith(
      Continuation<TResult, TContinuationResult> continuation, CancellationToken ct); Task<TContinuationResult> continueWithTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, final Executor executor); Task<TContinuationResult> continueWithTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, final Executor executor,
      final CancellationToken ct); Task<TContinuationResult> continueWithTask(
      Continuation<TResult, Task<TContinuationResult>> continuation); Task<TContinuationResult> continueWithTask(
      Continuation<TResult, Task<TContinuationResult>> continuation, CancellationToken ct); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation, Executor executor); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation, Executor executor,
      final CancellationToken ct); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation); Task<TContinuationResult> onSuccess(
      final Continuation<TResult, TContinuationResult> continuation, CancellationToken ct); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation, Executor executor,
      final CancellationToken ct); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation); Task<TContinuationResult> onSuccessTask(
      final Continuation<TResult, Task<TContinuationResult>> continuation,
      CancellationToken ct); static final ExecutorService BACKGROUND_EXECUTOR; static final Executor UI_THREAD_EXECUTOR; }