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

public final Class JdkFutureAdapters

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly

Utilities necessary for working with libraries that supply plain Future instances. Note that, whenever possible, it is strongly preferred to modify those libraries to return ListenableFuture directly.

For interoperability between ListenableFuture and CompletableFuture, consider Future Converter.

Author
Sven Mawson
Since
10.0 (replacing Futures.makeListenable, which existed in 1.0)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <V extends @Nullable Object> ListenableFuture<V>
listenInPoolThread(Future<V> future)

Assigns a thread to the given Future to provide ListenableFuture functionality.

public static <V extends @Nullable Object> ListenableFuture<V>
listenInPoolThread(Future<V> future, Executor executor)

Submits a blocking task for the given Future to provide ListenableFuture functionality.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

JdkFutureAdaptersback to summary
private JdkFutureAdapters()

Method Detail

listenInPoolThreadback to summary
public static <V extends @Nullable Object> ListenableFuture<V> listenInPoolThread(Future<V> future)

Assigns a thread to the given Future to provide ListenableFuture functionality.

Warning

If the input future does not already implement ListenableFuture, the returned future will emulate ListenableFuture#addListener by taking a thread from an internal, unbounded pool at the first call to addListener and holding it until the future is done.

Prefer to create ListenableFuture instances with SettableFuture, MoreExecutors#listeningDecorator(java.util.concurrent.ExecutorService), ListenableFutureTask, AbstractFuture, and other utilities over creating plain Future instances to be upgraded to ListenableFuture after the fact.

listenInPoolThreadback to summary
public static <V extends @Nullable Object> ListenableFuture<V> listenInPoolThread(Future<V> future, Executor executor)

Submits a blocking task for the given Future to provide ListenableFuture functionality.

Warning

If the input future does not already implement ListenableFuture, the returned future will emulate ListenableFuture#addListener by submitting a task to the given executor at the first call to addListener. The task must be started by the executor promptly, or else the returned ListenableFuture may fail to work. The task's execution consists of blocking until the input future is done, so each call to this method may claim and hold a thread for an arbitrary length of time. Use of bounded executors or other executors that may fail to execute a task promptly may result in deadlocks.

Prefer to create ListenableFuture instances with SettableFuture, MoreExecutors#listeningDecorator(java.util.concurrent.ExecutorService), ListenableFutureTask, AbstractFuture, and other utilities over creating plain Future instances to be upgraded to ListenableFuture after the fact.

Since
12.0
com.google.common.util.concurrent back to summary

private Class JdkFutureAdapters.ListenableFutureAdapter<V extends @Nullable Object>

extends ForwardingFuture<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

An adapter to turn a Future into a ListenableFuture. This will wait on the future to finish, and when it completes, run the listeners. This implementation will wait on the source future indefinitely, so if the source future never completes, the adapter will never complete either.

If the delegate future is interrupted or throws an unexpected unchecked exception, the listeners will not be invoked.

Field Summary

Modifier and TypeField and Description
private final Executor
private static final Executor
private final Future<V>
private final ExecutionList
private final AtomicBoolean
private static final ThreadFactory

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
ListenableFutureAdapter(Future<V> delegate, Executor adapterExecutor)

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.
protected Future<V>
delegate()

Implements abstract com.google.common.util.concurrent.ForwardingFuture.delegate.

Returns the backing delegate instance that methods are forwarded to.
Inherited from com.google.common.util.concurrent.ForwardingFuture:
cancelgetgetisCancelledisDone

Field Detail

adapterExecutorback to summary
private final Executor adapterExecutor
defaultAdapterExecutorback to summary
private static final Executor defaultAdapterExecutor
delegateback to summary
private final Future<V> delegate
executionListback to summary
private final ExecutionList executionList
hasListenersback to summary
private final AtomicBoolean hasListeners
threadFactoryback to summary
private static final ThreadFactory threadFactory

Constructor Detail

ListenableFutureAdapterback to summary
pack-priv ListenableFutureAdapter(Future<V> delegate)
ListenableFutureAdapterback to summary
pack-priv ListenableFutureAdapter(Future<V> delegate, Executor adapterExecutor)

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
delegateback to summary
protected Future<V> delegate()

Implements abstract com.google.common.util.concurrent.ForwardingFuture.delegate.

Doc from com.google.common.collect.ForwardingObject.delegate.

Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet#delegate. Concrete subclasses override this method to supply the instance being decorated.

Annotations
@Override