|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cz.cuni.amis.utils.future.FutureWithListeners<RESULT>
RESULT
- public abstract class FutureWithListeners<RESULT>
Abstract class that represents future result of some computation that allows you to hook
listeners on the status of the future computation (see addFutureListener(IFutureListener)
).
Whenever the computation is completed (or cancelled / exception has happened / etc.) the listeners are informed.
Field Summary | |
---|---|
protected java.util.concurrent.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 java.lang.Object |
mutex
Mutex synchronizing access to internal data structures of the future. |
Constructor Summary | |
---|---|
FutureWithListeners()
|
Method Summary | |
---|---|
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(java.lang.Exception e)
Informs the future that it can't be computed due to the exception. |
protected java.util.concurrent.CountDownLatch |
createLatch()
Factory method that should return CountDownLatch or its descendant initialized to 1. |
RESULT |
get()
|
RESULT |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Returns a result or waits for the computation till timeout. |
java.lang.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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.lang.Object mutex
protected Listeners<IFutureListener<RESULT>> listeners
addFutureListener(IFutureListener)
.
protected java.util.concurrent.CountDownLatch latch
get()
or get(long, TimeUnit)
. This
latch is instantiated whenever needed via method createLatch()
.
Constructor Detail |
---|
public FutureWithListeners()
Method Detail |
---|
public FutureStatus getStatus()
public void addFutureListener(IFutureListener<RESULT> listener)
listener
- public void removeFutureListener(IFutureListener<RESULT> listener)
listener
- public boolean isListening(IFutureListener<RESULT> listener)
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.
result
- public void computationException(java.lang.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.
e
- protected void switchStatus(FutureStatus newStatus)
newStatus
- protected java.util.concurrent.CountDownLatch createLatch()
CountDownLatch
or its descendant initialized to 1.
protected boolean cancelComputation(boolean mayInterruptIfRunning)
mayInterruptIfRunning
-
public final boolean cancel(boolean mayInterruptIfRunning)
cancel
in interface java.util.concurrent.Future<RESULT>
public RESULT get()
get
in interface java.util.concurrent.Future<RESULT>
public RESULT get(long timeout, java.util.concurrent.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).
get
in interface java.util.concurrent.Future<RESULT>
timeout
- unit
-
public boolean isCancelled()
isCancelled
in interface java.util.concurrent.Future<RESULT>
public boolean isDone()
isDone
in interface java.util.concurrent.Future<RESULT>
public java.lang.Exception getException()
getStatus()
== EXCEPTION).
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |