@Override public void andThen(ExecutionCallback<V> callback) { andThen(callback, defaultExecutor); }
@Override protected void onDestroy() { for (ClientTrackableJob trackableJob : trackableJobs.values()) { trackableJob.completableFuture.cancel(false); } }
@Override public final boolean cancel(boolean mayInterruptIfRunning) { Boolean shouldCancel = null; for (; ; ) { Object currentState = this.state; if (isDoneState(currentState)) { return false; } if (shouldCancel == null) { shouldCancel = shouldCancel(mayInterruptIfRunning); } if (!shouldCancel) { return false; } if (STATE.compareAndSet(this, currentState, CANCELLED_STATE)) { cancelled(mayInterruptIfRunning); notifyThreadsWaitingOnGet(); return true; } } }
/** * Sets the result. If {@code result} is an instance of Throwable, this * future will be completed exceptionally. That is, {@link #get} will throw * the exception rather than return it. * * @return true, if this call made this future to complete */ protected boolean setResult(Object result) { for (; ; ) { Object currentState = this.state; if (isDoneState(currentState)) { return false; } if (STATE.compareAndSet(this, currentState, result)) { done(); notifyThreadsWaitingOnGet(); runAsynchronous((ExecutionCallbackNode) currentState, result); break; } } return true; }
@Override public void andThen(ExecutionCallback<V> callback, Executor executor) { isNotNull(callback, "callback"); isNotNull(executor, "executor"); for (; ; ) { Object currentState = this.state; if (isCancelledState(currentState)) { return; } if (isDoneState(currentState)) { runAsynchronous(callback, executor, currentState); return; } ExecutionCallbackNode newState = new ExecutionCallbackNode<V>(callback, executor, (ExecutionCallbackNode) currentState); if (STATE.compareAndSet(this, currentState, newState)) { // we have successfully scheduled the callback. return; } // we failed to update the state. This can mean 2 things: // either a result was set, which we'll see when retrying this loop // or a different thread also called andThen, which we'll deal with when we retry the loop. } }
Object currentState = this.state; if (isCancelledState(currentState)) { throw new CancellationException(); if (isDoneState(currentState)) { return getResult(currentState); if (!isDoneState(this.state)) { wait(millisToWait);
private static <V> V getResult(Object state) { if (isCancelledState(state)) { return null; } if (!isDoneState(state)) { return null; } if (state instanceof Throwable) { sneakyThrow((Throwable) state); } return (V) state; }
@Override public boolean isCancelled() { if (delegate.isCancelled()) { cancel(true); return true; } else { return super.isCancelled(); } }
@Override public final V get() throws InterruptedException, ExecutionException { for (; ; ) { try { return get(Long.MAX_VALUE, TimeUnit.MILLISECONDS); } catch (TimeoutException ignored) { // A timeout here can only be a spurious artifact. // It should never happen and even if it does, we must retry. ignore(ignored); } } }
/** * Returns: * <ul> * <li>null - if cancelled or not done</li> * <li>result - if done and result is NOT Throwable</li> * <li>sneaky throws an exception - if done and result is Throwable</li> * </ul> */ protected V getResult() { return getResult(this.state); }
@Override public boolean isDone() { return isDoneState(state); }
@Override public boolean isCancelled() { return isCancelledState(state); }
@Override public boolean isDone() { if (delegate.isDone()) { try { ensureResultSet(Long.MAX_VALUE, TimeUnit.DAYS); } catch (ExecutionException ignored) { ignore(ignored); } catch (CancellationException ignored) { ignore(ignored); } return true; } else { return super.isDone(); } }
/** * Sets the result. If {@code result} is an instance of Throwable, this * future will be completed exceptionally. That is, {@link #get} will throw * the exception rather than return it. * * @return true, if this call made this future to complete */ protected boolean setResult(Object result) { for (; ; ) { Object currentState = this.state; if (isDoneState(currentState)) { return false; } if (STATE.compareAndSet(this, currentState, result)) { done(); notifyThreadsWaitingOnGet(); runAsynchronous((ExecutionCallbackNode) currentState, result); break; } } return true; }
@Override public void andThen(ExecutionCallback<V> callback, Executor executor) { isNotNull(callback, "callback"); isNotNull(executor, "executor"); for (; ; ) { Object currentState = this.state; if (isCancelledState(currentState)) { return; } if (isDoneState(currentState)) { runAsynchronous(callback, executor, currentState); return; } ExecutionCallbackNode newState = new ExecutionCallbackNode<V>(callback, executor, (ExecutionCallbackNode) currentState); if (STATE.compareAndSet(this, currentState, newState)) { // we have successfully scheduled the callback. return; } // we failed to update the state. This can mean 2 things: // either a result was set, which we'll see when retrying this loop // or a different thread also called andThen, which we'll deal with when we retry the loop. } }
Object currentState = this.state; if (isCancelledState(currentState)) { throw new CancellationException(); if (isDoneState(currentState)) { return getResult(currentState); if (!isDoneState(this.state)) { wait(millisToWait);
private static <V> V getResult(Object state) { if (isCancelledState(state)) { return null; } if (!isDoneState(state)) { return null; } if (state instanceof Throwable) { sneakyThrow((Throwable) state); } return (V) state; }
@Override public boolean isCancelled() { if (delegate.isCancelled()) { cancel(true); return true; } else { return super.isCancelled(); } }
@Override public final V get() throws InterruptedException, ExecutionException { for (; ; ) { try { return get(Long.MAX_VALUE, TimeUnit.MILLISECONDS); } catch (TimeoutException ignored) { // A timeout here can only be a spurious artifact. // It should never happen and even if it does, we must retry. ignore(ignored); } } }
/** * Returns: * <ul> * <li>null - if cancelled or not done</li> * <li>result - if done and result is NOT Throwable</li> * <li>sneaky throws an exception - if done and result is Throwable</li> * </ul> */ protected V getResult() { return getResult(this.state); }