public class CallbackFuture<T> extends Object implements 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() |
public void setCompletionListener(Mapping<CallbackFuture<T>,T> listener)
public void complete(T result)
public T getResult()
public boolean cancel(boolean mayInterruptIfRunning)
public T get() throws InterruptedException, ExecutionException
get
in interface Future<T>
InterruptedException
ExecutionException
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get
in interface Future<T>
InterruptedException
ExecutionException
TimeoutException
public boolean isCancelled()
isCancelled
in interface Future<T>
Copyright © 2015. All rights reserved.