T
- The result object type returned by this futurepublic class ExecuteOnGetFutureTask<T> extends ListenableFutureTask<T>
get()
IF it has not already started. It is still expected that this future will be
provided to an executor, but if that executor has been unable to start execution by the time
get()
is requested, execution will start on the get()
thread.
This can make sense if for example you need to have the result of a given future before you can
go forward with additional work. That way if the thread pool is too busy with other things, we
can try to get the result for this work unit as fast as possible (once we are ready for it).
If execution starts on the get()
call, once the task is started in the thread pool, it
will return immediately (WITHOUT invoking the task contained in the future).
This does come with some details to be aware of. Execution may only occur on the
get()
request. Requesting FutureTask.get(long, java.util.concurrent.TimeUnit)
will NOT
cause execution to start (as we can not ensure a timeout will be respected). In addition this
can not work with recurring tasks.
ListenableFuture.ListenerOptimizationStrategy
Constructor and Description |
---|
ExecuteOnGetFutureTask(java.util.concurrent.Callable<T> task)
Constructs a runnable future with a callable work unit.
|
ExecuteOnGetFutureTask(java.lang.Runnable task)
Constructs a runnable future with a runnable work unit.
|
ExecuteOnGetFutureTask(java.lang.Runnable task,
T result)
Constructs a runnable future with a runnable work unit.
|
Modifier and Type | Method and Description |
---|---|
T |
get() |
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 . |
void |
run() |
getContainedCallable, getFailure, getRunningStackTrace, isCompletedExceptionally, listener
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 ExecuteOnGetFutureTask(java.lang.Runnable task)
task
- runnable to be runpublic ExecuteOnGetFutureTask(java.lang.Runnable task, T result)
task
- runnable to be runresult
- result to be provide after run has completedpublic ExecuteOnGetFutureTask(java.util.concurrent.Callable<T> task)
task
- callable to be runpublic void run()
run
in interface java.lang.Runnable
run
in interface java.util.concurrent.RunnableFuture<T>
run
in class ListenableFutureTask<T>
public T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
public 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>
getFailure
in class ListenableFutureTask<T>
null
if completed normallyjava.lang.InterruptedException
- If the current thread was interrupted while blocking