Package cn.sliew.milky.concurrent
Class DefaultPromise<V>
- java.lang.Object
-
- cn.sliew.milky.concurrent.AbstractFuture<V>
-
- cn.sliew.milky.concurrent.DefaultPromise<V>
-
- Direct Known Subclasses:
DefaultProgressivePromise
public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V>
-
-
Field Summary
Fields Modifier and Type Field Description protected List<FutureListener<? extends Future<? super V>>>listenersOne or more listeners.
-
Constructor Summary
Constructors Constructor Description DefaultPromise()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Promise<V>addListener(FutureListener<? extends Future<? super V>> listener)Adds the specified listener to this future.Promise<V>await()Waits for this future to be completed.booleanawait(long timeout, TimeUnit unit)Waits for this future to be completed within the specified time limit.booleancancel(boolean mayInterruptIfRunning)Throwablecause()Returns the cause of the failed operation if the operation has failed.VgetNow()Return the result without blocking.booleanisCancelled()booleanisDone()booleanisSuccess()Returnstrueif and only if the operation was completed successfully.Promise<V>removeListener(FutureListener<? extends Future<? super V>> listener)Removes the first occurrence of the specified listener from this future.Promise<V>setFailure(Throwable cause)Marks this future as a failure and notifies all listeners.Promise<V>setSuccess(V result)Marks this future as a success and notifies all listeners.Promise<V>sync()Waits for this future until it is done, and rethrows the cause of the failure if this future failed.booleantryFailure(Throwable cause)Marks this future as a failure and notifies all listeners.booleantrySuccess(V result)Marks this future as a success and notifies all listeners.-
Methods inherited from class cn.sliew.milky.concurrent.AbstractFuture
get, get
-
-
-
-
Field Detail
-
listeners
protected List<FutureListener<? extends Future<? super V>>> listeners
One or more listeners.
-
-
Method Detail
-
setSuccess
public Promise<V> setSuccess(V result)
Description copied from interface:PromiseMarks this future as a success and notifies all listeners.If it is success or failed already it will throw an
IllegalStateException.- Specified by:
setSuccessin interfacePromise<V>
-
trySuccess
public boolean trySuccess(V result)
Description copied from interface:PromiseMarks this future as a success and notifies all listeners.- Specified by:
trySuccessin interfacePromise<V>- Returns:
trueif and only if successfully marked this future as a success. Otherwisefalsebecause this future is already marked as either a success or a failure.
-
setFailure
public Promise<V> setFailure(Throwable cause)
Description copied from interface:PromiseMarks this future as a failure and notifies all listeners.If it is success or failed already it will throw an
IllegalStateException.- Specified by:
setFailurein interfacePromise<V>
-
tryFailure
public boolean tryFailure(Throwable cause)
Description copied from interface:PromiseMarks this future as a failure and notifies all listeners.- Specified by:
tryFailurein interfacePromise<V>- Returns:
trueif and only if successfully marked this future as a failure. Otherwisefalsebecause this future is already marked as either a success or a failure.
-
addListener
public Promise<V> addListener(FutureListener<? extends Future<? super V>> listener)
Description copied from interface:FutureAdds the specified listener to this future. The specified listener is notified when this future is done. If this future is already completed, the specified listener is notified immediately.- Specified by:
addListenerin interfaceFuture<V>- Specified by:
addListenerin interfacePromise<V>
-
removeListener
public Promise<V> removeListener(FutureListener<? extends Future<? super V>> listener)
Description copied from interface:FutureRemoves the first occurrence of the specified listener from this future. The specified listener is no longer notified when this future is done. If the specified listener is not associated with this future, this method does nothing and returns silently.- Specified by:
removeListenerin interfaceFuture<V>- Specified by:
removeListenerin interfacePromise<V>
-
sync
public Promise<V> sync() throws InterruptedException
Description copied from interface:FutureWaits for this future until it is done, and rethrows the cause of the failure if this future failed.
-
await
public Promise<V> await() throws InterruptedException
Description copied from interface:FutureWaits for this future to be completed.
-
await
public boolean await(long timeout, TimeUnit unit) throws InterruptedExceptionDescription copied from interface:FutureWaits for this future to be completed within the specified time limit.- Specified by:
awaitin interfaceFuture<V>- Returns:
trueif and only if the future was completed within the specified time limit- Throws:
InterruptedException- if the current thread was interrupted
-
isSuccess
public boolean isSuccess()
Description copied from interface:FutureReturnstrueif and only if the operation was completed successfully.
-
cause
public Throwable cause()
Description copied from interface:FutureReturns the cause of the failed operation if the operation has failed.
-
getNow
public V getNow()
Description copied from interface:FutureReturn the result without blocking. If the future is not done yet this will returnnull.As it is possible that a
nullvalue is used to mark the future as successful you also need to check if the future is really done withFuture.isDone()and not relay on the returnednullvalue.
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfaceFuture<V>
-
-