T
- The result object type returned by this futurepublic class ListenableFutureTask<T> extends java.util.concurrent.FutureTask<T> implements ListenableRunnableFuture<T>, CallableContainer<T>
Executor
, and then return this future.ListenableFuture.ListenerOptimizationStrategy
Constructor and Description |
---|
ListenableFutureTask(boolean recurring,
java.util.concurrent.Callable<T> task)
Constructs a runnable future with a callable work unit.
|
ListenableFutureTask(boolean recurring,
java.util.concurrent.Callable<T> task,
java.util.concurrent.Executor executingExecutor)
Constructs a runnable future with a callable work unit.
|
ListenableFutureTask(boolean recurring,
java.lang.Runnable task)
Constructs a runnable future with a runnable work unit.
|
ListenableFutureTask(boolean recurring,
java.lang.Runnable task,
T result)
Constructs a runnable future with a runnable work unit.
|
ListenableFutureTask(boolean recurring,
java.lang.Runnable task,
T result,
java.util.concurrent.Executor executingExecutor)
Constructs a runnable future with a runnable work unit.
|
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.Callable<T> |
getContainedCallable()
Call to get the contained callable held within the wrapper.
|
java.lang.Throwable |
getFailure()
Similar to
Future.get() except instead of providing a result, this will provide a thrown
exception if ListenableFuture.isCompletedExceptionally() returns true . |
java.lang.Throwable |
getFailure(long timeout,
java.util.concurrent.TimeUnit unit)
Similar to
Future.get(long, TimeUnit) except instead of providing a result, this will
provide a thrown exception if ListenableFuture.isCompletedExceptionally() returns true . |
java.lang.StackTraceElement[] |
getRunningStackTrace()
A best effort to return the stack trace for for the executing thread of either this future,
or a future which this depends on through the use of
ListenableFuture.map(Function) or similar
functions. |
boolean |
isCompletedExceptionally()
Returns
true if the future is both done and has completed with an error or was
canceled. |
ListenableFuture<T> |
listener(java.lang.Runnable listener,
java.util.concurrent.Executor executor,
ListenableFuture.ListenerOptimizationStrategy optimize)
Add a listener to be called once the future has completed.
|
void |
run() |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
callback, callback, callback, failureCallback, failureCallback, failureCallback, flatMap, flatMap, flatMap, flatMap, flatMapFailure, flatMapFailure, flatMapFailure, listener, listener, map, map, map, mapFailure, mapFailure, mapFailure, resultCallback, resultCallback, resultCallback, throwMap, throwMap, throwMap
public ListenableFutureTask(boolean recurring, java.lang.Runnable task)
recurring
- boolean to indicate if this task can run multiple times, and thus must be reset after each runtask
- runnable to be runpublic ListenableFutureTask(boolean recurring, java.lang.Runnable task, T result)
recurring
- boolean to indicate if this task can run multiple times, and thus must be reset after each runtask
- runnable to be runresult
- result to be provide after run has completedpublic ListenableFutureTask(boolean recurring, java.util.concurrent.Callable<T> task)
recurring
- boolean to indicate if this task can run multiple times, and thus must be reset after each runtask
- callable to be runpublic ListenableFutureTask(boolean recurring, java.lang.Runnable task, T result, java.util.concurrent.Executor executingExecutor)
recurring
- boolean to indicate if this task can run multiple times, and thus must be reset after each runtask
- runnable to be runresult
- result to be provide after run has completedexecutingExecutor
- Executor task will be run on for possible listener optimization, or null
public ListenableFutureTask(boolean recurring, java.util.concurrent.Callable<T> task, java.util.concurrent.Executor executingExecutor)
recurring
- boolean to indicate if this task can run multiple times, and thus must be reset after each runtask
- callable to be runexecutingExecutor
- Executor task will be run on for possible listener optimization, or null
public void run()
public ListenableFuture<T> listener(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.
listener
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 poolthis
instance to add more listeners or other functional operationspublic java.util.concurrent.Callable<T> getContainedCallable()
CallableContainer
getContainedCallable
in interface CallableContainer<T>
null
if none is containedpublic java.lang.StackTraceElement[] getRunningStackTrace()
ListenableFuture
ListenableFuture.map(Function)
or similar
functions. If there is no thread executing the future yet, or the future has already
completed, then this will return null
.
This is done without locking (though generating a stack trace still requires a JVM safe point), so the resulting stack trace is NOT guaranteed to be accurate. In most cases (particularly when blocking) this should be accurate though.
getRunningStackTrace
in interface ListenableFuture<T>
null
if unavailablepublic boolean isCompletedExceptionally()
ListenableFuture
true
if the future is both done and has completed with an error or was
canceled. If this returns true
the Throwable
responsible for the error can
be retrieved using ListenableFuture.getFailure()
;isCompletedExceptionally
in interface ListenableFuture<T>
true
if this ListenableFuture completed by a thrown Exception or was canceledpublic java.lang.Throwable getFailure() throws java.lang.InterruptedException
ListenableFuture
Future.get()
except instead of providing a result, this will provide a thrown
exception if ListenableFuture.isCompletedExceptionally()
returns true
. If the future has not
completed yet this function will block until completion. If the future completed normally,
this will return null
.getFailure
in interface ListenableFuture<T>
null
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blockingpublic java.lang.Throwable getFailure(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
ListenableFuture
Future.get(long, TimeUnit)
except instead of providing a result, this will
provide a thrown exception if ListenableFuture.isCompletedExceptionally()
returns true
. If
the future has not completed yet this function will block until completion. If the future
completed normally, this will return null
.getFailure
in interface ListenableFuture<T>
timeout
- The maximum time to waitunit
- The time unit of the timeout argumentnull
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blockingjava.util.concurrent.TimeoutException
- If the timeout was reached before the future completed