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

public Class ListenableFutureTask<V extends @Nullable Object>

extends FutureTask<V extends @Nullable Object>
implements ListenableFuture<V extends @Nullable Object>
Class Inheritance
All Implemented Interfaces
com.google.common.util.concurrent.ListenableFuture, java.util.concurrent.Future
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
java.lang.Math.min, java.util.concurrent.TimeUnit.NANOSECONDS

A FutureTask that also implements the ListenableFuture interface. Unlike FutureTask, ListenableFutureTask does not provide an overrideable done() method. For similar functionality, call addListener.

Few users should use this class. It is intended primarily for those who are implementing an ExecutorService. Most users should call ListeningExecutorService.submit on a service obtained from MoreExecutors#listeningDecorator.

Author
Sven Mawson
Since
1.0

Field Summary

Modifier and TypeField and Description
private final ExecutionList

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
ListenableFutureTask(Runnable runnable, V result)

Method Summary

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

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

Registers a listener to be run on the given executor.
public static <V extends @Nullable Object> ListenableFutureTask<V>
create(Callable<V>
the callable task
callable
)

Creates a ListenableFutureTask that will upon running, execute the given Callable.

public static <V extends @Nullable Object> ListenableFutureTask<V>
create(Runnable
the runnable task
runnable
,
V
the result to return on successful completion. If you don't need a particular result, consider using constructions of the form: ListenableFuture<?> f = ListenableFutureTask.create(runnable, null)
result
)

Creates a ListenableFutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.

protected void
done()

Overrides java.util.concurrent.FutureTask.done.

Internal implementation detail used to invoke the listeners.
public V
get(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Overrides java.util.concurrent.FutureTask.get.

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.
Inherited from java.util.concurrent.FutureTask:
cancelexceptionNowgetisCancelledisDoneresultNowrunrunAndResetsetsetExceptionstatetoString

Field Detail

executionListback to summary
private final ExecutionList executionList

Constructor Detail

ListenableFutureTaskback to summary
pack-priv ListenableFutureTask(Callable<V> callable)
ListenableFutureTaskback to summary
pack-priv ListenableFutureTask(Runnable runnable, V result)

Method Detail

addListenerback to summary
public void addListener(Runnable listener, Executor exec)

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

Doc from com.google.common.util.concurrent.ListenableFuture.addListener.

Registers a listener to be run on the given executor. The listener will run when the Future's computation is complete or, if the computation is already complete, immediately.

There is no guaranteed ordering of execution of listeners, but any listener added through this method is guaranteed to be called once the computation is complete.

Exceptions thrown by a listener will be propagated up to the executor. Any exception thrown during Executor.execute (e.g., a RejectedExecutionException or an exception thrown by direct execution) will be caught and logged.

Note

If your listener is lightweight -- and will not cause stack overflow by completing more futures or adding more directExecutor() listeners inline -- consider MoreExecutors#directExecutor. Otherwise, avoid it: See the warnings on the docs for directExecutor.

This is the most general listener interface. For common operations performed using listeners, see Futures. For a simplified but general listener interface, see addCallback().

Memory consistency effects: Actions in a thread prior to adding a listener happen-before its execution begins, perhaps in another thread.

Guava implementations of ListenableFuture promptly release references to listeners after executing them.

Parameters
listener:Runnable

the listener to run when the computation is complete

exec:Executor

the executor to run the listener in

Annotations
@Override
createback to summary
public static <V extends @Nullable Object> ListenableFutureTask<V> create(Callable<V> callable)

Creates a ListenableFutureTask that will upon running, execute the given Callable.

Parameters
callable:Callable<V>

the callable task

Since
10.0
createback to summary
public static <V extends @Nullable Object> ListenableFutureTask<V> create(Runnable runnable, V result)

Creates a ListenableFutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.

Parameters
runnable:Runnable

the runnable task

result:V

the result to return on successful completion. If you don't need a particular result, consider using constructions of the form: ListenableFuture<?> f = ListenableFutureTask.create(runnable, null)

Since
10.0
doneback to summary
protected void done()

Overrides java.util.concurrent.FutureTask.done.

Internal implementation detail used to invoke the listeners.

Annotations
@Override
getback to summary
public V get(long timeout, TimeUnit unit) throws TimeoutException, InterruptedException, ExecutionException

Overrides java.util.concurrent.FutureTask.get.

Implements java.util.concurrent.Future.get.

Doc from 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.

Parameters
timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Returns:V

the computed result

Annotations
@CanIgnoreReturnValue
@Override
@ParametricNullness
Exceptions
TimeoutException:
if the wait timed out
InterruptedException:
if the current thread was interrupted while waiting
ExecutionException:
if the computation threw an exception