Class DefaultPromise<V>
- Direct Known Subclasses:
DefaultChannelGroupFuture
,DefaultChannelPromise
,DefaultProgressivePromise
,ImmediateEventExecutor.ImmediatePromise
,PromiseTask
,ProxyHandler.LazyChannelPromise
,SslHandler.LazyChannelPromise
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
private static final class
Nested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final DefaultPromise.CauseHolder
private static final StackTraceElement[]
private final EventExecutor
private GenericFutureListener
<? extends Future<?>> One or more listeners.private DefaultFutureListeners
private static final InternalLogger
private static final int
private boolean
Threading - synchronized(this).static final String
System property with integer type value, that determine the max reentrancy/recursion level for when listener notifications prompt other listeners to be notified.private static final InternalLogger
private Object
private static final AtomicReferenceFieldUpdater
<DefaultPromise, Object> private static final Object
private static final Object
private short
Threading - synchronized(this). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Seeexecutor()
for expectations of the executor.DefaultPromise
(EventExecutor executor) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionaddListener
(GenericFutureListener<? extends Future<? super V>> listener) Adds the specified listener to this future.private void
addListener0
(GenericFutureListener<? extends Future<? super V>> listener) addListeners
(GenericFutureListener<? extends Future<? super V>>... listeners) Adds the specified listeners to this future.await()
Waits for this future to be completed.boolean
await
(long timeoutMillis) Waits for this future to be completed within the specified time limit.boolean
Waits for this future to be completed within the specified time limit.private boolean
await0
(long timeoutNanos, boolean interruptable) Waits for this future to be completed without interruption.boolean
awaitUninterruptibly
(long timeoutMillis) Waits for this future to be completed within the specified time limit without interruption.boolean
awaitUninterruptibly
(long timeout, TimeUnit unit) Waits for this future to be completed within the specified time limit without interruption.boolean
cancel
(boolean mayInterruptIfRunning) If the cancellation was successful it will fail the future with aCancellationException
.cause()
Returns the cause of the failed I/O operation if the I/O operation has failed.private Throwable
protected void
private boolean
Check if there are any waiters and if so notify these.private void
protected EventExecutor
executor()
Get the executor used to notify listeners when this promise is complete.get()
getNow()
Return the result without blocking.private void
boolean
returnstrue
if and only if the operation can be cancelled viaFuture.cancel(boolean)
.boolean
private static boolean
isCancelled0
(Object result) boolean
isDone()
private static boolean
boolean
Returnstrue
if and only if the I/O operation was completed successfully.protected static void
notifyListener
(EventExecutor eventExecutor, Future<?> future, GenericFutureListener<?> listener) Notify a listener that a future has completed.private static void
notifyListener0
(Future future, GenericFutureListener l) private void
private void
notifyListeners0
(DefaultFutureListeners listeners) private void
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 ofDefaultPromise
since the listener(s) may be changed and is protected by a synchronized operation.private static void
notifyProgressiveListener0
(ProgressiveFuture future, GenericProgressiveFutureListener l, long progress, long total) (package private) void
notifyProgressiveListeners
(long progress, long total) Notify all progressive listeners.private static void
notifyProgressiveListeners0
(ProgressiveFuture<?> future, GenericProgressiveFutureListener<?>[] listeners, long progress, long total) private Object
removeListener
(GenericFutureListener<? extends Future<? super V>> listener) Removes the first occurrence of the specified listener from this future.private void
removeListener0
(GenericFutureListener<? extends Future<? super V>> toRemove) removeListeners
(GenericFutureListener<? extends Future<? super V>>... listeners) Removes the first occurrence for each of the listeners from this future.private void
private static void
safeExecute
(EventExecutor executor, Runnable task) setFailure
(Throwable cause) Marks this future as a failure and notifies all listeners.private boolean
setFailure0
(Throwable cause) setSuccess
(V result) Marks this future as a success and notifies all listeners.private boolean
setSuccess0
(V result) boolean
Make this future impossible to cancel.private boolean
sync()
Waits for this future until it is done, and rethrows the cause of the failure if this future failed.Waits for this future until it is done, and rethrows the cause of the failure if this future failed.toString()
protected StringBuilder
boolean
tryFailure
(Throwable cause) Marks this future as a failure and notifies all listeners.boolean
trySuccess
(V result) Marks this future as a success and notifies all listeners.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, state
-
Field Details
-
PROPERTY_MAX_LISTENER_STACK_DEPTH
System property with integer type value, that determine the max reentrancy/recursion level for when listener notifications prompt other listeners to be notified.When the reentrancy/recursion level becomes greater than this number, a new task will instead be scheduled on the event loop, to finish notifying any subsequent listners.
The default value is
8
.- See Also:
-
logger
-
rejectedExecutionLogger
-
MAX_LISTENER_STACK_DEPTH
private static final int MAX_LISTENER_STACK_DEPTH -
RESULT_UPDATER
-
SUCCESS
-
UNCANCELLABLE
-
CANCELLATION_CAUSE_HOLDER
-
CANCELLATION_STACK
-
result
-
executor
-
listener
One or more listeners. Can be aGenericFutureListener
or 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.
-
listeners
-
waiters
private short waitersThreading - synchronized(this). We are required to hold the monitor to use Java's underlying wait()/notifyAll(). -
notifyingListeners
private boolean notifyingListenersThreading - synchronized(this). We must prevent concurrent notification and FIFO listener notification if the executor changes.
-
-
Constructor Details
-
DefaultPromise
Creates a new instance.It is preferable to use
EventExecutor.newPromise()
to create a new promise- Parameters:
executor
- theEventExecutor
which is used to notify the promise once it is complete. It is assumed this executor will protect againstStackOverflowError
exceptions. The executor may be used to avoidStackOverflowError
by executing aRunnable
if the stack depth exceeds a threshold.
-
DefaultPromise
protected DefaultPromise()Seeexecutor()
for expectations of the executor.
-
-
Method Details
-
setSuccess
Description copied from interface:Promise
Marks this future as a success and notifies all listeners. If it is success or failed already it will throw anIllegalStateException
.- Specified by:
setSuccess
in interfacePromise<V>
-
trySuccess
Description copied from interface:Promise
Marks this future as a success and notifies all listeners.- Specified by:
trySuccess
in interfacePromise<V>
- Returns:
true
if and only if successfully marked this future as a success. Otherwisefalse
because this future is already marked as either a success or a failure.
-
setFailure
Description copied from interface:Promise
Marks this future as a failure and notifies all listeners. If it is success or failed already it will throw anIllegalStateException
.- Specified by:
setFailure
in interfacePromise<V>
-
tryFailure
Description copied from interface:Promise
Marks this future as a failure and notifies all listeners.- Specified by:
tryFailure
in interfacePromise<V>
- Returns:
true
if and only if successfully marked this future as a failure. Otherwisefalse
because this future is already marked as either a success or a failure.
-
setUncancellable
public boolean setUncancellable()Description copied from interface:Promise
Make this future impossible to cancel.- Specified by:
setUncancellable
in interfacePromise<V>
- Returns:
true
if and only if successfully marked this future as uncancellable or it is already done without being cancelled.false
if this future has been cancelled already.
-
isSuccess
public boolean isSuccess()Description copied from interface:Future
Returnstrue
if and only if the I/O operation was completed successfully. -
isCancellable
public boolean isCancellable()Description copied from interface:Future
returnstrue
if and only if the operation can be cancelled viaFuture.cancel(boolean)
.- Specified by:
isCancellable
in interfaceFuture<V>
-
cause
Description copied from interface:Future
Returns the cause of the failed I/O operation if the I/O operation has failed. -
cause0
-
addListener
Description copied from interface:Future
Adds 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:
addListener
in interfaceFuture<V>
- Specified by:
addListener
in interfacePromise<V>
-
addListeners
Description copied from interface:Future
Adds 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:
addListeners
in interfaceFuture<V>
- Specified by:
addListeners
in interfacePromise<V>
-
removeListener
Description copied from interface:Future
Removes 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:
removeListener
in interfaceFuture<V>
- Specified by:
removeListener
in interfacePromise<V>
-
removeListeners
Description copied from interface:Future
Removes 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:
removeListeners
in interfaceFuture<V>
- Specified by:
removeListeners
in interfacePromise<V>
-
await
Description copied from interface:Future
Waits for this future to be completed. -
awaitUninterruptibly
Description copied from interface:Future
Waits for this future to be completed without interruption. This method catches anInterruptedException
and discards it silently.- Specified by:
awaitUninterruptibly
in interfaceFuture<V>
- Specified by:
awaitUninterruptibly
in interfacePromise<V>
-
await
Description copied from interface:Future
Waits for this future to be completed within the specified time limit.- Specified by:
await
in interfaceFuture<V>
- Returns:
true
if and only if the future was completed within the specified time limit- Throws:
InterruptedException
- if the current thread was interrupted
-
await
Description copied from interface:Future
Waits for this future to be completed within the specified time limit.- Specified by:
await
in interfaceFuture<V>
- Returns:
true
if and only if the future was completed within the specified time limit- Throws:
InterruptedException
- if the current thread was interrupted
-
awaitUninterruptibly
Description copied from interface:Future
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedException
and discards it silently.- Specified by:
awaitUninterruptibly
in interfaceFuture<V>
- Returns:
true
if and only if the future was completed within the specified time limit
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeoutMillis) Description copied from interface:Future
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedException
and discards it silently.- Specified by:
awaitUninterruptibly
in interfaceFuture<V>
- Returns:
true
if and only if the future was completed within the specified time limit
-
getNow
Description copied from interface:Future
Return the result without blocking. If the future is not done yet this will returnnull
.As it is possible that a
null
value 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 returnednull
value. -
get
- Specified by:
get
in interfaceFuture<V>
- Overrides:
get
in classAbstractFuture<V>
- Throws:
InterruptedException
ExecutionException
-
get
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
get
in interfaceFuture<V>
- Overrides:
get
in classAbstractFuture<V>
- Throws:
InterruptedException
ExecutionException
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:
isCancelled
in interfaceFuture<V>
-
isDone
public boolean isDone() -
sync
Description copied from interface:Future
Waits for this future until it is done, and rethrows the cause of the failure if this future failed. -
syncUninterruptibly
Description copied from interface:Future
Waits for this future until it is done, and rethrows the cause of the failure if this future failed.- Specified by:
syncUninterruptibly
in interfaceFuture<V>
- Specified by:
syncUninterruptibly
in interfacePromise<V>
-
toString
-
toStringBuilder
-
executor
Get the executor used to notify listeners when this promise is complete.It is assumed this executor will protect against
StackOverflowError
exceptions. The executor may be used to avoidStackOverflowError
by executing aRunnable
if 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_DEPTH
that will limit recursion to preventStackOverflowError
and 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 ofDefaultPromise
since the listener(s) may be changed and is protected by a synchronized operation. -
notifyListenersNow
private void notifyListenersNow() -
notifyListeners0
-
notifyListener0
-
addListener0
-
removeListener0
-
setSuccess0
-
setFailure0
-
setValue0
-
checkNotifyWaiters
private boolean checkNotifyWaiters()Check if there are any waiters and if so notify these.- Returns:
true
if there are any listeners attached to the promise,false
otherwise.
-
incWaiters
private void incWaiters() -
decWaiters
private void decWaiters() -
rethrowIfFailed
private void rethrowIfFailed() -
await0
- Throws:
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
GenericProgressiveFutureListener
s.- Parameters:
progress
- the new progress.total
- the total progress.
-
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
-
isDone0
-
safeExecute
-