public class CallbackFuture<T>
extends java.lang.Object
implements java.util.concurrent.Future<T>
An CallbackFuture
offers the possibility to call back a registered
listener when it is completed. To avoid taking up a thread and waiting for
completion, this is accomplished by required the creator of the future
to invoke the completed
method when the result this
Future
represents has been computed.
Only one listener can be registered for call back and it is of type
MappingCallbackFuture
itself as an argument and must return
the result of the Future
. Typically the mapping will
just return the CallbackFuture
's own result by
calling the getResult
, but it may choose to assign a different
result. Note that blocking calls to get
will not return
until the registered listener (if any) returns, so the listener
must rely on the provided getResult
to obtain the
Future
's result value.
This class may be extended to provide semantics for cancellation. In this
case the cancel
method must be overridden and the
canceled
flag must be set to true
if cancellation
was successful.
Modifier and Type | Field and Description |
---|---|
protected boolean |
canceled |
Constructor and Description |
---|
CallbackFuture() |
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning) |
void |
complete(T result) |
T |
get() |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
T |
getResult() |
boolean |
isCancelled() |
boolean |
isDone() |
void |
setCompletionListener(Mapping<CallbackFuture<T>,T> listener) |
public void setCompletionListener(Mapping<CallbackFuture<T>,T> listener)
public void complete(T result)
public T getResult()
public boolean cancel(boolean mayInterruptIfRunning)
cancel
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
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<T>
public boolean isDone()
isDone
in interface java.util.concurrent.Future<T>