RESULT - public abstract class FutureWithListeners<RESULT> extends Object implements IFutureWithListeners<RESULT>
addFutureListener(IFutureListener)).
Whenever the computation is completed (or cancelled / exception has happened / etc.) the listeners are informed.| Modifier and Type | Field and Description |
|---|---|
protected CountDownLatch |
latch
Latch where threads are waiting when using
get() or get(long, TimeUnit). |
protected Listeners<IFutureListener<RESULT>> |
listeners
Future listeners, here we store listeners registred in
addFutureListener(IFutureListener). |
protected Object |
mutex
Mutex synchronizing access to internal data structures of the future.
|
| Constructor and Description |
|---|
FutureWithListeners() |
| Modifier and Type | Method and Description |
|---|---|
void |
addFutureListener(IFutureListener<RESULT> listener)
Adds a listener on a future status (using strong reference).
|
boolean |
cancel(boolean mayInterruptIfRunning) |
protected boolean |
cancelComputation(boolean mayInterruptIfRunning)
This should cancel the computation of the future.
|
void |
computationException(Exception e)
Informs the future that it can't be computed due to the exception.
|
protected CountDownLatch |
createLatch()
Factory method that should return
CountDownLatch or its descendant initialized to 1. |
RESULT |
get() |
RESULT |
get(long timeout,
TimeUnit unit)
Returns a result or waits for the computation till timeout.
|
Exception |
getException()
Contains an exception that has happened during the computation in the case of (
getStatus() == EXCEPTION). |
FutureStatus |
getStatus()
Current status of the future computation.
|
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
isListening(IFutureListener<RESULT> listener)
Whether some listener is listening on the future.
|
void |
removeFutureListener(IFutureListener<RESULT> listener)
Removes a listener from the future.
|
void |
setResult(RESULT result)
Sets the result of the future computation.
|
protected void |
switchStatus(FutureStatus newStatus)
Changes the status of the future (if it is different than current one) and notifies the listeners
about this change.
|
protected Object mutex
protected Listeners<IFutureListener<RESULT>> listeners
addFutureListener(IFutureListener).protected CountDownLatch latch
get() or get(long, TimeUnit). This
latch is instantiated whenever needed via method createLatch().public FutureStatus getStatus()
getStatus in interface IFutureWithListeners<RESULT>public void addFutureListener(IFutureListener<RESULT> listener)
addFutureListener in interface IFutureWithListeners<RESULT>listener - public void removeFutureListener(IFutureListener<RESULT> listener)
removeFutureListener in interface IFutureWithListeners<RESULT>listener - public boolean isListening(IFutureListener<RESULT> listener)
isListening in interface IFutureWithListeners<RESULT>listener - public void setResult(RESULT result)
Switches the status to FUTURE_IS_READY (notifying listeners along the way).
The result can be set only iff NOT isDone(), i.e., status is FutureStatus:FUTURE_IS_BEING_COMPUTED.
setResult in interface IFutureWithListeners<RESULT>result - public void computationException(Exception e)
Switches the status to EXCEPTION (notifying listeners along the way).
The result can be set only iff NOT isDone(), i.e., status is FutureStatus:FUTURE_IS_BEING_COMPUTED.
computationException in interface IFutureWithListeners<RESULT>e - protected void switchStatus(FutureStatus newStatus)
newStatus - protected CountDownLatch createLatch()
CountDownLatch or its descendant initialized to 1.protected boolean cancelComputation(boolean mayInterruptIfRunning)
mayInterruptIfRunning - public final boolean cancel(boolean mayInterruptIfRunning)
public RESULT get()
public RESULT get(long timeout, TimeUnit unit)
Does not throw TimeoutException! It returns null instead - always examine status of the future
via getStatus() if the null is returned to tell whether the 'null' is the
result of the computation (if the status is FUTURE_IS_READY than the 'null' is truly the result).
public boolean isCancelled()
isCancelled in interface IFutureWithListeners<RESULT>isCancelled in interface Future<RESULT>public boolean isDone()
public Exception getException()
getStatus() == EXCEPTION).getException in interface IFutureWithListeners<RESULT>Copyright © 2019 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All rights reserved.