Class DefaultPromise<V>

java.lang.Object
io.netty.util.concurrent.AbstractFuture<V>
io.netty.util.concurrent.DefaultPromise<V>
All Implemented Interfaces:
Future<V>, Promise<V>, Future<V>
Direct Known Subclasses:
DefaultChannelGroupFuture, DefaultChannelPromise, DefaultProgressivePromise, ImmediateEventExecutor.ImmediatePromise, PromiseTask, ProxyHandler.LazyChannelPromise, SslHandler.LazyChannelPromise

public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V>
  • Field Details

    • 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 AtomicReferenceFieldUpdater<DefaultPromise,Object> RESULT_UPDATER
    • SUCCESS

      private static final Object SUCCESS
    • UNCANCELLABLE

      private static final Object UNCANCELLABLE
    • CANCELLATION_CAUSE_HOLDER

      private static final DefaultPromise.CauseHolder CANCELLATION_CAUSE_HOLDER
    • CANCELLATION_STACK

      private static final StackTraceElement[] CANCELLATION_STACK
    • result

      private volatile Object result
    • executor

      private final EventExecutor executor
    • listener

      private GenericFutureListener<? extends Future<?>> listener
      One or more listeners. Can be a GenericFutureListener or a DefaultFutureListeners. If null, 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

      private DefaultFutureListeners listeners
    • 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 Details

    • DefaultPromise

      public DefaultPromise(EventExecutor executor)
      Creates a new instance. It is preferable to use EventExecutor.newPromise() to create a new promise
      Parameters:
      executor - the EventExecutor which is used to notify the promise once it is complete. It is assumed this executor will protect against StackOverflowError exceptions. The executor may be used to avoid StackOverflowError by executing a Runnable if the stack depth exceeds a threshold.
    • DefaultPromise

      protected DefaultPromise()
      See executor() for expectations of the executor.
  • Method Details

    • setSuccess

      public Promise<V> setSuccess(V result)
      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 an IllegalStateException.
      Specified by:
      setSuccess in interface Promise<V>
    • trySuccess

      public boolean trySuccess(V result)
      Description copied from interface: Promise
      Marks this future as a success and notifies all listeners.
      Specified by:
      trySuccess in interface Promise<V>
      Returns:
      true if and only if successfully marked this future as a success. Otherwise false because this future is already marked as either a success or a failure.
    • setFailure

      public Promise<V> setFailure(Throwable cause)
      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 an IllegalStateException.
      Specified by:
      setFailure in interface Promise<V>
    • tryFailure

      public boolean tryFailure(Throwable cause)
      Description copied from interface: Promise
      Marks this future as a failure and notifies all listeners.
      Specified by:
      tryFailure in interface Promise<V>
      Returns:
      true if and only if successfully marked this future as a failure. Otherwise false 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 interface Promise<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
      Returns true if and only if the I/O operation was completed successfully.
      Specified by:
      isSuccess in interface Future<V>
    • isCancellable

      public boolean isCancellable()
      Description copied from interface: Future
      returns true if and only if the operation can be cancelled via Future.cancel(boolean).
      Specified by:
      isCancellable in interface Future<V>
    • cause

      public Throwable cause()
      Description copied from interface: Future
      Returns the cause of the failed I/O operation if the I/O operation has failed.
      Specified by:
      cause in interface Future<V>
      Returns:
      the cause of the failure. null if succeeded or this future is not completed yet.
    • cause0

      private Throwable cause0(Object result)
    • addListener

      public Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener)
      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 interface Future<V>
      Specified by:
      addListener in interface Promise<V>
    • addListeners

      public Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
      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 interface Future<V>
      Specified by:
      addListeners in interface Promise<V>
    • removeListener

      public Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener)
      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 interface Future<V>
      Specified by:
      removeListener in interface Promise<V>
    • removeListeners

      public Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners)
      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 interface Future<V>
      Specified by:
      removeListeners in interface Promise<V>
    • await

      public Promise<V> await() throws InterruptedException
      Description copied from interface: Future
      Waits for this future to be completed.
      Specified by:
      await in interface Future<V>
      Specified by:
      await in interface Promise<V>
      Throws:
      InterruptedException - if the current thread was interrupted
    • awaitUninterruptibly

      public Promise<V> awaitUninterruptibly()
      Description copied from interface: Future
      Waits for this future to be completed without interruption. This method catches an InterruptedException and discards it silently.
      Specified by:
      awaitUninterruptibly in interface Future<V>
      Specified by:
      awaitUninterruptibly in interface Promise<V>
    • await

      public boolean await(long timeout, TimeUnit unit) throws InterruptedException
      Description copied from interface: Future
      Waits for this future to be completed within the specified time limit.
      Specified by:
      await in interface Future<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

      public boolean await(long timeoutMillis) throws InterruptedException
      Description copied from interface: Future
      Waits for this future to be completed within the specified time limit.
      Specified by:
      await in interface Future<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

      public boolean awaitUninterruptibly(long timeout, TimeUnit unit)
      Description copied from interface: Future
      Waits for this future to be completed within the specified time limit without interruption. This method catches an InterruptedException and discards it silently.
      Specified by:
      awaitUninterruptibly in interface Future<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 an InterruptedException and discards it silently.
      Specified by:
      awaitUninterruptibly in interface Future<V>
      Returns:
      true if and only if the future was completed within the specified time limit
    • getNow

      public V getNow()
      Description copied from interface: Future
      Return the result without blocking. If the future is not done yet this will return null. 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 with Future.isDone() and not rely on the returned null value.
      Specified by:
      getNow in interface Future<V>
    • get

      Specified by:
      get in interface Future<V>
      Overrides:
      get in class AbstractFuture<V>
      Throws:
      InterruptedException
      ExecutionException
    • get

      public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Specified by:
      get in interface Future<V>
      Overrides:
      get in class AbstractFuture<V>
      Throws:
      InterruptedException
      ExecutionException
      TimeoutException
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      If the cancellation was successful it will fail the future with a CancellationException.
      Specified by:
      cancel in interface Future<V>
      Specified by:
      cancel in interface Future<V>
      Parameters:
      mayInterruptIfRunning - this value has no effect in this implementation.
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface Future<V>
    • isDone

      public boolean isDone()
      Specified by:
      isDone in interface Future<V>
    • sync

      public Promise<V> sync() throws InterruptedException
      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:
      sync in interface Future<V>
      Specified by:
      sync in interface Promise<V>
      Throws:
      InterruptedException
    • syncUninterruptibly

      public Promise<V> 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 interface Future<V>
      Specified by:
      syncUninterruptibly in interface Promise<V>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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 StackOverflowError exceptions. The executor may be used to avoid StackOverflowError by executing a Runnable 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 prevent StackOverflowError and will stop notifying listeners added after this threshold is exceeded.

      Parameters:
      eventExecutor - the executor to use to notify the listener listener.
      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 to notifyListeners() but cannot share code because the listener(s) cannot be cached for an instance of DefaultPromise since 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>> toRemove)
    • setSuccess0

      private boolean setSuccess0(V result)
    • setFailure0

      private boolean setFailure0(Throwable cause)
    • setValue0

      private boolean setValue0(Object objResult)
    • 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

      private boolean await0(long timeoutNanos, boolean interruptable) throws InterruptedException
      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 GenericProgressiveFutureListeners.

      Parameters:
      progress - the new progress.
      total - the total progress.
    • progressiveListeners

      private 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(Object result)
    • isDone0

      private static boolean isDone0(Object result)
    • safeExecute

      private static void safeExecute(EventExecutor executor, Runnable task)