T
- The result object type returned by this futurepublic class SettableListenableFuture<T> extends java.lang.Object implements ListenableFuture<T>, FutureCallback<T>
ListenableFuture.ListenerOptimizationStrategy
Constructor and Description |
---|
SettableListenableFuture()
Constructs a new
SettableListenableFuture . |
SettableListenableFuture(boolean throwIfAlreadyComplete)
Constructs a new
SettableListenableFuture . |
Modifier and Type | Method and Description |
---|---|
void |
addListener(java.lang.Runnable listener,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a listener to be called once the future has completed.
|
boolean |
cancel(boolean interruptThread) |
void |
clearResult()
Clears the stored result from this set future.
|
T |
get() |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
void |
handleFailure(java.lang.Throwable t)
This call defers to
setFailure(Throwable) . |
void |
handleResult(T result)
This call defers to
setResult(Object) . |
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
setFailure(java.lang.Throwable failure)
Call to indicate this future is done, and provide the occurred failure.
|
boolean |
setResult(T result)
Call to indicate this future is done, and provide the given result.
|
void |
setRunningThread(java.lang.Thread thread)
Optional call to set the thread internally that will be generating the result for this
future.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addCallback, addCallback, addCallback, addListener, addListener, flatMap, flatMap, flatMap, map, map, map, throwMap, throwMap, throwMap
public SettableListenableFuture()
SettableListenableFuture
. You can return this immediately and
provide a result to the object later when it is ready.
This defaults in the behavior since version 1.2.0 where if the future has completed (either
by cancel(boolean)
, setResult(Object)
, or setFailure(Throwable)
),
any additional attempts to setResult(Object)
or setFailure(Throwable)
will
result in a IllegalStateException
being thrown.
public SettableListenableFuture(boolean throwIfAlreadyComplete)
SettableListenableFuture
. You can return this immediately and
provide a result to the object later when it is ready.
This constructor allows you to control the behavior when results are attempt to be set after
the future has already completed (either by
cancel(boolean)
, setResult(Object)
, or setFailure(Throwable)
).
If true
, any additional attempts to setResult(Object)
or
setFailure(Throwable)
will result in a IllegalStateException
being thrown.
If false
, additional attempts to set a result will just be silently ignored.
throwIfAlreadyComplete
- Defines the behavior when result or failure is set on a completed futurepublic void addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor, ListenableFuture.ListenerOptimizationStrategy optimize)
ListenableFuture
If the provided Executor
is null, the listener will execute on the thread which
computed the original future (once it is done). If the future has already completed, the
listener will execute immediately on the thread which is adding the listener.
Caution should be used when choosing to optimize the listener execution. If the listener is
complex, or wanting to be run concurrent, this optimization could prevent that. In addition
it will prevent other listeners from potentially being invoked until it completes. However
if the listener is small / fast, this can provide significant performance gains. It should
also be known that not all ListenableFuture
implementations may be able to do such an
optimization. Please see ListenableFuture.ListenerOptimizationStrategy
javadocs for more specific
details of what optimizations are available.
addListener
in interface ListenableFuture<T>
listener
- the listener to run when the computation is completeexecutor
- Executor
the listener should be ran on, or null
optimize
- true
to avoid listener queuing for execution if already on the desired poolpublic void handleResult(T result)
setResult(Object)
. It is implemented so that you can construct
this, return it immediately, but only later provide this as a callback to another
ListenableFuture
implementation.
This should never be invoked by the implementor, this should only be invoked by other
ListenableFuture
's.
If this is being used to chain together ListenableFuture
's,
setResult(Object)
/setFailure(Throwable)
should never be called manually (or
an exception will occur).
handleResult
in interface FutureCallback<T>
result
- Result object to provide to the future to be returned from get()
callpublic void handleFailure(java.lang.Throwable t)
setFailure(Throwable)
. It is implemented so that you can
construct this, return it immediately, but only later provide this as a callback to another
ListenableFuture
implementation.
This should never be invoked by the implementor, this should only be invoked by other
ListenableFuture
's.
If this is being used to chain together ListenableFuture
's,
setResult(Object)
/setFailure(Throwable)
should never be called manually (or
an exception will occur).
handleFailure
in interface FutureCallback<T>
t
- Throwable to be provided as the cause from the ExecutionException thrown from get()
callpublic boolean setResult(T result)
setFailure(Throwable)
are called.
If future has already completed and constructed with SettableListenableFuture()
or
true
provided to SettableListenableFuture(boolean)
this will throw an
IllegalStateException
. If complete but constructed with a false
this result
will be ignored.
result
- result to provide for get()
call, can be null
true
if the result was set (ie future did not complete in failure or cancel}public boolean setFailure(java.lang.Throwable failure)
setResult(Object)
are called, and only called once. If the provided
failure is null
, a new Exception
will be created so that something is always
provided in the ExecutionException
on calls to get()
.
If future has already completed and constructed with SettableListenableFuture()
or
true
provided to SettableListenableFuture(boolean)
this will throw an
IllegalStateException
. If complete but constructed with a false
this failure
result will be ignored.
failure
- Throwable that caused failure during computation.true
if the failure was set (ie future did not complete with result or cancel}public void setRunningThread(java.lang.Thread thread)
cancel(boolean)
call is invoked
with true
, we can send an interrupt to this thread.
The reference to this thread will be cleared after this future has completed (thus allowing it to be garbage collected).
thread
- Thread that is generating the result for this futurepublic boolean cancel(boolean interruptThread)
cancel
in interface java.util.concurrent.Future<T>
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>
public void clearResult()
get()
will throw an
IllegalStateException
. So it is critical that this is only called after you are sure
no future calls to get the result on this future will be attempted.
The design of this is so that if you want to chain ListenableFuture
's together, you
can clear the results of old ones after their result has been consumed. This is really only
useful in very specific instances.
public boolean isDone()
isDone
in interface java.util.concurrent.Future<T>
public T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
get
in interface java.util.concurrent.Future<T>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
public T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
get
in interface java.util.concurrent.Future<T>
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException