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:
DefaultChannelGroupFuture,DefaultChannelPromise,DefaultProgressivePromise,ImmediateEventExecutor.ImmediatePromise,PromiseTask,ProxyHandler.LazyChannelPromise,SslHandler.LazyChannelPromise
public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classDefaultPromise.CauseHolderprivate static classDefaultPromise.LeanCancellationException
-
Field Summary
Fields Modifier and Type Field Description private static DefaultPromise.CauseHolderCANCELLATION_CAUSE_HOLDERprivate static java.lang.StackTraceElement[]CANCELLATION_STACKprivate EventExecutorexecutorprivate java.lang.ObjectlistenersOne or more listeners.private static InternalLoggerloggerprivate static intMAX_LISTENER_STACK_DEPTHprivate booleannotifyingListenersThreading - synchronized(this).private static InternalLoggerrejectedExecutionLoggerprivate java.lang.Objectresultprivate static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<DefaultPromise,java.lang.Object>RESULT_UPDATERprivate static java.lang.ObjectSUCCESSprivate static java.lang.ObjectUNCANCELLABLEprivate shortwaitersThreading - synchronized(this).
-
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.private voidaddListener0(GenericFutureListener<? extends Future<? super V>> listener)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, java.util.concurrent.TimeUnit unit)Waits for this future to be completed within the specified time limit.private booleanawait0(long timeoutNanos, boolean interruptable)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, java.util.concurrent.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.java.lang.Throwablecause()Returns the cause of the failed I/O operation if the I/O operation has failed.private java.lang.Throwablecause0(java.lang.Object result)protected voidcheckDeadLock()private booleancheckNotifyWaiters()Check if there are any waiters and if so notify these.private voiddecWaiters()protected EventExecutorexecutor()Get the executor used to notify listeners when this promise is complete.Vget()Vget(long timeout, java.util.concurrent.TimeUnit unit)VgetNow()Return the result without blocking.private voidincWaiters()booleanisCancellable()returnstrueif and only if the operation can be cancelled viaFuture.cancel(boolean).booleanisCancelled()private static booleanisCancelled0(java.lang.Object result)booleanisDone()private static booleanisDone0(java.lang.Object result)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.private static voidnotifyListener0(Future future, GenericFutureListener l)private voidnotifyListeners()private voidnotifyListeners0(DefaultFutureListeners listeners)private voidnotifyListenersNow()private static voidnotifyListenerWithStackOverFlowProtection(EventExecutor executor, Future<?> future, GenericFutureListener<?> listener)The logic in this method should be identical tonotifyListeners()but cannot share code because the listener(s) cannot be cached for an instance ofDefaultPromisesince the listener(s) may be changed and is protected by a synchronized operation.private static voidnotifyProgressiveListener0(ProgressiveFuture future, GenericProgressiveFutureListener l, long progress, long total)(package private) voidnotifyProgressiveListeners(long progress, long total)Notify all progressive listeners.private static voidnotifyProgressiveListeners0(ProgressiveFuture<?> future, GenericProgressiveFutureListener<?>[] listeners, long progress, long total)private java.lang.ObjectprogressiveListeners()Promise<V>removeListener(GenericFutureListener<? extends Future<? super V>> listener)Removes the first occurrence of the specified listener from this future.private voidremoveListener0(GenericFutureListener<? extends Future<? super V>> listener)Promise<V>removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners)Removes the first occurrence for each of the listeners from this future.private voidrethrowIfFailed()private static voidsafeExecute(EventExecutor executor, java.lang.Runnable task)Promise<V>setFailure(java.lang.Throwable cause)Marks this future as a failure and notifies all listeners.private booleansetFailure0(java.lang.Throwable cause)Promise<V>setSuccess(V result)Marks this future as a success and notifies all listeners.private booleansetSuccess0(V result)booleansetUncancellable()Make this future impossible to cancel.private booleansetValue0(java.lang.Object objResult)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.java.lang.StringtoString()protected java.lang.StringBuildertoStringBuilder()booleantryFailure(java.lang.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.
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
rejectedExecutionLogger
private static final InternalLogger rejectedExecutionLogger
-
MAX_LISTENER_STACK_DEPTH
private static final int MAX_LISTENER_STACK_DEPTH
-
RESULT_UPDATER
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<DefaultPromise,java.lang.Object> RESULT_UPDATER
-
SUCCESS
private static final java.lang.Object SUCCESS
-
UNCANCELLABLE
private static final java.lang.Object UNCANCELLABLE
-
CANCELLATION_CAUSE_HOLDER
private static final DefaultPromise.CauseHolder CANCELLATION_CAUSE_HOLDER
-
CANCELLATION_STACK
private static final java.lang.StackTraceElement[] CANCELLATION_STACK
-
result
private volatile java.lang.Object result
-
executor
private final EventExecutor executor
-
listeners
private java.lang.Object listeners
One or more listeners. Can be aGenericFutureListeneror aDefaultFutureListeners. Ifnull, it means either 1) no listeners were added yet or 2) all listeners were notified. Threading - synchronized(this). We must support adding listeners when there is no EventExecutor.
-
waiters
private short waiters
Threading - synchronized(this). We are required to hold the monitor to use Java's underlying wait()/notifyAll().
-
notifyingListeners
private boolean notifyingListeners
Threading - synchronized(this). We must prevent concurrent notification and FIFO listener notification if the executor changes.
-
-
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(java.lang.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(java.lang.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 java.lang.Throwable cause()
Description copied from interface:FutureReturns the cause of the failed I/O operation if the I/O operation has failed.
-
cause0
private java.lang.Throwable cause0(java.lang.Object result)
-
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 java.lang.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, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionDescription copied from interface:FutureWaits for this future to be completed within the specified time limit.
-
await
public boolean await(long timeoutMillis) throws java.lang.InterruptedExceptionDescription copied from interface:FutureWaits for this future to be completed within the specified time limit.
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeout, java.util.concurrent.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 java.lang.InterruptedException, java.util.concurrent.ExecutionException
- Specified by:
getin interfacejava.util.concurrent.Future<V>- Overrides:
getin classAbstractFuture<V>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionException
-
get
public V get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
- Specified by:
getin interfacejava.util.concurrent.Future<V>- Overrides:
getin classAbstractFuture<V>- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
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 interfacejava.util.concurrent.Future<V>
-
isDone
public boolean isDone()
- Specified by:
isDonein interfacejava.util.concurrent.Future<V>
-
sync
public Promise<V> sync() throws java.lang.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>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
toStringBuilder
protected java.lang.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.
-
notifyListeners
private void notifyListeners()
-
notifyListenerWithStackOverFlowProtection
private static void notifyListenerWithStackOverFlowProtection(EventExecutor executor, Future<?> future, GenericFutureListener<?> listener)
The logic in this method should be identical tonotifyListeners()but cannot share code because the listener(s) cannot be cached for an instance ofDefaultPromisesince the listener(s) may be changed and is protected by a synchronized operation.
-
notifyListenersNow
private void notifyListenersNow()
-
notifyListeners0
private void notifyListeners0(DefaultFutureListeners listeners)
-
notifyListener0
private static void notifyListener0(Future future, GenericFutureListener l)
-
addListener0
private void addListener0(GenericFutureListener<? extends Future<? super V>> listener)
-
removeListener0
private void removeListener0(GenericFutureListener<? extends Future<? super V>> listener)
-
setSuccess0
private boolean setSuccess0(V result)
-
setFailure0
private boolean setFailure0(java.lang.Throwable cause)
-
setValue0
private boolean setValue0(java.lang.Object objResult)
-
checkNotifyWaiters
private boolean checkNotifyWaiters()
Check if there are any waiters and if so notify these.- Returns:
trueif there are any listeners attached to the promise,falseotherwise.
-
incWaiters
private void incWaiters()
-
decWaiters
private void decWaiters()
-
rethrowIfFailed
private void rethrowIfFailed()
-
await0
private boolean await0(long timeoutNanos, boolean interruptable) throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
notifyProgressiveListeners
void notifyProgressiveListeners(long progress, long total)Notify all progressive listeners.No attempt is made to ensure notification order if multiple calls are made to this method before the original invocation completes.
This will do an iteration over all listeners to get all of type
GenericProgressiveFutureListeners.- Parameters:
progress- the new progress.total- the total progress.
-
progressiveListeners
private java.lang.Object progressiveListeners()
-
notifyProgressiveListeners0
private static void notifyProgressiveListeners0(ProgressiveFuture<?> future, GenericProgressiveFutureListener<?>[] listeners, long progress, long total)
-
notifyProgressiveListener0
private static void notifyProgressiveListener0(ProgressiveFuture future, GenericProgressiveFutureListener l, long progress, long total)
-
isCancelled0
private static boolean isCancelled0(java.lang.Object result)
-
isDone0
private static boolean isDone0(java.lang.Object result)
-
safeExecute
private static void safeExecute(EventExecutor executor, java.lang.Runnable task)
-
-