Top Description Inners Fields Constructors Methods
com.google.common.util.concurrent

public abstract Class AbstractFuture<V extends @Nullable Object>

extends InternalFutureFailureAccess
implements ListenableFuture<V extends @Nullable Object>
Class Inheritance
All Implemented Interfaces
com.google.common.util.concurrent.ListenableFuture, java.util.concurrent.Future
Known Direct Subclasses
com.google.common.util.concurrent.AbstractFuture.TrustedFuture, com.google.common.util.concurrent.Futures.InCompletionOrderFuture, com.google.common.util.concurrent.GwtFluentFutureCatchingSpecialization
Annotations
@SuppressWarnings:ShortCircuitBoolean, nullness
@GwtCompatible
emulated:true
@ReflectionSupport:FULL
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, com.google.common.util.concurrent.NullnessCasts.uncheckedNull, java.lang.Integer.toHexString, .System.identityHashCode, java.util.Objects.requireNonNull, java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater

An abstract implementation of ListenableFuture, intended for advanced users only. More common ways to create a ListenableFuture include instantiating a SettableFuture, submitting a task to a ListeningExecutorService, and deriving a Future from an existing one, typically using methods like Futures.transform and Futures.catching.

This class implements all methods in ListenableFuture. Subclasses should provide a way to set the result of the computation through the protected methods set(Object), setFuture(ListenableFuture) and setException(Throwable). Subclasses may also override afterDone(), which will be invoked automatically when the future completes. Subclasses should rarely override other methods.

Authors
Sven Mawson, Luke Sandberg
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract static class
private static class
AbstractFuture.Cancellation

A special value to represent cancellation and the 'wasInterrupted' bit.

private static class
AbstractFuture.Failure

A special value to represent failure, when setException is called successfully.

private static class
AbstractFuture.Listener

Listeners also form a stack through the listeners field.

private static class
private static class
AbstractFuture.SetFuture<V extends @Nullable Object>

A special value that encodes the 'setFuture' state.

private static class
AbstractFuture.SynchronizedHelper

AtomicHelper based on synchronized and volatile writes.

pack-priv static interface
AbstractFuture.Trusted<V extends @Nullable Object>

Tag interface marking trusted subclasses.

pack-priv abstract static class
AbstractFuture.TrustedFuture<V extends @Nullable Object>

A less abstract subclass of AbstractFuture.

private static class
private static class
AbstractFuture.Waiter

Waiter links form a Treiber stack, in the waiters field.

Field Summary

Modifier and TypeField and Description
private static final AbstractFuture.AtomicHelper
pack-priv static final boolean
private volatile AbstractFuture.Listener
listeners

All listeners.

private static final Logger
private static final Object
NULL

A special value to represent null.

private static final long
private volatile Object
value

This field encodes the current state of the future.

private volatile AbstractFuture.Waiter
waiters

All waiting threads.

Constructor Summary

AccessConstructor and Description
protected
AbstractFuture()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
private void
public void
addListener(Runnable
the listener to run when the computation is complete
listener
,
Executor
the executor to run the listener in
executor
)

Implements com.google.common.util.concurrent.ListenableFuture.addListener.

Registers a listener to be run on the given executor.
private void
protected void
afterDone()

Callback method that is called exactly once after the future is completed.

private void
appendResultObject(StringBuilder builder, Object o)

Any object can be the result of a Future, and not every object has a reasonable toString() implementation.

private void
appendUserObject(StringBuilder builder, Object o)

Helper for printing user supplied objects into our toString method.

public boolean
cancel(boolean
true if the thread executing this task should be interrupted (if the thread is known to the implementation); otherwise, in-progress tasks are allowed to complete
mayInterruptIfRunning
)

Implements java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task.
private static CancellationException
private AbstractFuture.Listener
clearListeners(AbstractFuture.Listener onto)

Clears the listeners list and prepends its contents to onto, least recently added first.

private static void
complete(AbstractFuture<?> param, boolean callInterruptTask)

Unblocks all threads and runs all listeners.

private static void
executeListener(Runnable runnable, Executor executor)

Submits the given runnable to the given Executor catching and logging all runtime exceptions thrown by the executor.

public V
get(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Implements java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
public V
get()

Implements java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result.
private V
getDoneValue(Object obj)

Unboxes obj.

private static Object
getFutureValue(ListenableFuture<?> future)

Returns a value that satisfies the contract of the value field based on the state of given future.

private static <V extends @Nullable Object> V
getUninterruptibly(Future<V> future)

An inlined private copy of Uninterruptibles#getUninterruptibly used to break an internal dependency on other /util/concurrent classes.

protected void
interruptTask()

Subclasses can override this method to implement interruption of the future's computation.

public boolean
isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally.
public boolean
isDone()

Implements java.util.concurrent.Future.isDone.

Returns true if this task completed.
pack-priv final void
maybePropagateCancellationTo(Future<?> related)

If this future has been cancelled (and possibly interrupted), cancels (and possibly interrupts) the given future (if available).

protected String

Returns:

null if an explanation cannot be provided (e.g. because the future is done).
pendingToString
()

Provide a human-readable explanation of why this future has not yet completed.

private void
releaseWaiters()

Releases all threads in the waiters list, and clears the list.

private void
removeWaiter(AbstractFuture.Waiter node)

Marks the given node as 'deleted' (null waiter) and then scans the list to unlink all deleted nodes.

protected boolean

Returns:

true if the attempt was accepted, completing the Future
set
(V
the value to be used as the result
value
)

Sets the result of this Future unless this Future has already been cancelled or set (including set asynchronously).

protected boolean

Returns:

true if the attempt was accepted, completing the Future
setException
(Throwable
the exception to be used as the failed result
throwable
)

Sets the failed result of this Future unless this Future has already been cancelled or set (including set asynchronously).

protected boolean

Returns:

true if the attempt was accepted, indicating that the Future was not previously cancelled or set.
setFuture
(ListenableFuture<? extends V>
the future to delegate to
future
)

Sets the result of this Future to match the supplied input Future once the supplied Future is done, unless this Future has already been cancelled or set (including "set asynchronously," defined below).

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
protected final Throwable
tryInternalFastPathGetFailure()

Implements abstract com.google.common.util.concurrent.internal.InternalFutureFailureAccess.tryInternalFastPathGetFailure.

Usually returns null but, if this Future has failed, may optionally return the cause of the failure.
protected final boolean
wasInterrupted()

Returns true if this future was cancelled with mayInterruptIfRunning set to true.