Package io.netty.util.concurrent
Class DefaultPromise<V>
- java.lang.Object
-
- io.netty.util.concurrent.AbstractFuture<V>
-
- io.netty.util.concurrent.DefaultPromise<V>
-
- Direct Known Subclasses:
DefaultProgressivePromise
public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V>
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedDefaultPromise()Seeexecutor()for expectations of the executor.DefaultPromise(EventExecutor executor)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Promise<V>addListener(GenericFutureListener<? extends Future<? super V>> listener)Adds the specified listener to this future.Promise<V>addListeners(GenericFutureListener<? extends Future<? super V>>... listeners)Adds the specified listeners to this future.Promise<V>await()Waits for this future to be completed.booleanawait(long timeoutMillis)Waits for this future to be completed within the specified time limit.booleanawait(long timeout, TimeUnit unit)Waits for this future to be completed within the specified time limit.Promise<V>awaitUninterruptibly()Waits for this future to be completed without interruption.booleanawaitUninterruptibly(long timeoutMillis)Waits for this future to be completed within the specified time limit without interruption.booleanawaitUninterruptibly(long timeout, TimeUnit unit)Waits for this future to be completed within the specified time limit without interruption.booleancancel(boolean mayInterruptIfRunning)If the cancellation was successful it will fail the future with aCancellationException.Throwablecause()Returns the cause of the failed I/O operation if the I/O operation has failed.protected voidcheckDeadLock()protected EventExecutorexecutor()Get the executor used to notify listeners when this promise is complete.Vget()Vget(long timeout, TimeUnit unit)VgetNow()Return the result without blocking.booleanisCancellable()returnstrueif and only if the operation can be cancelled viaFuture.cancel(boolean).booleanisCancelled()booleanisDone()booleanisSuccess()Returnstrueif and only if the I/O operation was completed successfully.protected static voidnotifyListener(EventExecutor eventExecutor, Future<?> future, GenericFutureListener<?> listener)Notify a listener that a future has completed.Promise<V>removeListener(GenericFutureListener<? extends Future<? super V>> listener)Removes the first occurrence of the specified listener from this future.Promise<V>removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners)Removes the first occurrence for each of the listeners 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.booleansetUncancellable()Make this future impossible to cancel.Promise<V>sync()Waits for this future until it is done, and rethrows the cause of the failure if this future failed.Promise<V>syncUninterruptibly()Waits for this future until it is done, and rethrows the cause of the failure if this future failed.StringtoString()protected StringBuildertoStringBuilder()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.
-
-
-
Constructor Detail
-
DefaultPromise
public DefaultPromise(EventExecutor executor)
Creates a new instance. It is preferable to useEventExecutor.newPromise()to create a new promise- Parameters:
executor- theEventExecutorwhich is used to notify the promise once it is complete. It is assumed this executor will protect againstStackOverflowErrorexceptions. The executor may be used to avoidStackOverflowErrorby executing aRunnableif the stack depth exceeds a threshold.
-
DefaultPromise
protected DefaultPromise()
Seeexecutor()for expectations of the executor.
-
-
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 anIllegalStateException.- 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 anIllegalStateException.- 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.
-
setUncancellable
public boolean setUncancellable()
Description copied from interface:PromiseMake this future impossible to cancel.- Specified by:
setUncancellablein interfacePromise<V>- Returns:
trueif and only if successfully marked this future as uncancellable or it is already done without being cancelled.falseif this future has been cancelled already.
-
isSuccess
public boolean isSuccess()
Description copied from interface:FutureReturnstrueif and only if the I/O operation was completed successfully.
-
isCancellable
public boolean isCancellable()
Description copied from interface:Futurereturnstrueif and only if the operation can be cancelled viaFuture.cancel(boolean).- Specified by:
isCancellablein interfaceFuture<V>
-
cause
public Throwable cause()
Description copied from interface:FutureReturns the cause of the failed I/O operation if the I/O operation has failed.
-
addListener
public Promise<V> addListener(GenericFutureListener<? 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>
-
addListeners
public Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
Description copied from interface:FutureAdds the specified listeners to this future. The specified listeners are notified when this future is done. If this future is already completed, the specified listeners are notified immediately.- Specified by:
addListenersin interfaceFuture<V>- Specified by:
addListenersin interfacePromise<V>
-
removeListener
public Promise<V> removeListener(GenericFutureListener<? 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>
-
removeListeners
public Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
Description copied from interface:FutureRemoves the first occurrence for each of the listeners from this future. The specified listeners are no longer notified when this future is done. If the specified listeners are not associated with this future, this method does nothing and returns silently.- Specified by:
removeListenersin interfaceFuture<V>- Specified by:
removeListenersin interfacePromise<V>
-
await
public Promise<V> await() throws InterruptedException
Description copied from interface:FutureWaits for this future to be completed.
-
awaitUninterruptibly
public Promise<V> awaitUninterruptibly()
Description copied from interface:FutureWaits for this future to be completed without interruption. This method catches anInterruptedExceptionand discards it silently.- Specified by:
awaitUninterruptiblyin interfaceFuture<V>- Specified by:
awaitUninterruptiblyin interfacePromise<V>
-
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
-
await
public boolean await(long timeoutMillis) 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
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeout, TimeUnit unit)Description copied from interface:FutureWaits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Specified by:
awaitUninterruptiblyin interfaceFuture<V>- Returns:
trueif and only if the future was completed within the specified time limit
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeoutMillis)
Description copied from interface:FutureWaits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- Specified by:
awaitUninterruptiblyin interfaceFuture<V>- Returns:
trueif and only if the future was completed within the specified time limit
-
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 anullvalue is used to mark the future as successful you also need to check if the future is really done withFuture.isDone()and not rely on the returnednullvalue.
-
get
public V get() throws InterruptedException, ExecutionException
- Specified by:
getin interfaceFuture<V>- Overrides:
getin classAbstractFuture<V>- Throws:
InterruptedExceptionExecutionException
-
get
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
getin interfaceFuture<V>- Overrides:
getin classAbstractFuture<V>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
If the cancellation was successful it will fail the future with aCancellationException.
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfaceFuture<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.
-
syncUninterruptibly
public Promise<V> syncUninterruptibly()
Description copied from interface:FutureWaits for this future until it is done, and rethrows the cause of the failure if this future failed.- Specified by:
syncUninterruptiblyin interfaceFuture<V>- Specified by:
syncUninterruptiblyin interfacePromise<V>
-
toStringBuilder
protected StringBuilder toStringBuilder()
-
executor
protected EventExecutor executor()
Get the executor used to notify listeners when this promise is complete.It is assumed this executor will protect against
StackOverflowErrorexceptions. The executor may be used to avoidStackOverflowErrorby executing aRunnableif the stack depth exceeds a threshold.- Returns:
- The executor used to notify listeners when this promise is complete.
-
checkDeadLock
protected void checkDeadLock()
-
notifyListener
protected static void notifyListener(EventExecutor eventExecutor, Future<?> future, GenericFutureListener<?> listener)
Notify a listener that a future has completed.This method has a fixed depth of
MAX_LISTENER_STACK_DEPTHthat will limit recursion to preventStackOverflowErrorand will stop notifying listeners added after this threshold is exceeded.- Parameters:
eventExecutor- the executor to use to notify the listenerlistener.future- the future that is complete.listener- the listener to notify.
-
-