Class DefaultPromise<V>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String 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.
    • 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.
      boolean await​(long timeoutMillis)
      Waits for this future to be completed within the specified time limit.
      boolean await​(long timeout, java.util.concurrent.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.
      boolean awaitUninterruptibly​(long timeoutMillis)
      Waits for this future to be completed within the specified time limit without interruption.
      boolean awaitUninterruptibly​(long timeout, java.util.concurrent.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 a CancellationException.
      java.lang.Throwable cause()
      Returns the cause of the failed I/O operation if the I/O operation has failed.
      protected void checkDeadLock()  
      protected EventExecutor executor()
      Get the executor used to notify listeners when this promise is complete.
      V get()  
      V get​(long timeout, java.util.concurrent.TimeUnit unit)  
      V getNow()
      Return the result without blocking.
      boolean isCancellable()
      returns true if and only if the operation can be cancelled via Future.cancel(boolean).
      boolean isCancelled()  
      boolean isDone()  
      boolean isSuccess()
      Returns true 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.
      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​(java.lang.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.
      boolean setUncancellable()
      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.
      java.lang.String toString()  
      protected java.lang.StringBuilder toStringBuilder()  
      boolean tryFailure​(java.lang.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
    • Field Detail

      • PROPERTY_MAX_LISTENER_STACK_DEPTH

        public static final java.lang.String 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:
        Constant Field Values
    • Constructor Detail

      • 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 Detail

      • 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​(java.lang.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​(java.lang.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>
      • cause

        public java.lang.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.
      • 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 java.lang.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:
        java.lang.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,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.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:
        java.lang.InterruptedException - if the current thread was interrupted
      • await

        public boolean await​(long timeoutMillis)
                      throws java.lang.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:
        java.lang.InterruptedException - if the current thread was interrupted
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeout,
                                            java.util.concurrent.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

        public V get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.util.concurrent.Future<V>
        Overrides:
        get in class AbstractFuture<V>
        Throws:
        java.lang.InterruptedException
        java.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:
        get in interface java.util.concurrent.Future<V>
        Overrides:
        get in class AbstractFuture<V>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.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 java.util.concurrent.Future<V>
        Parameters:
        mayInterruptIfRunning - this value has no effect in this implementation.
      • isCancelled

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

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

        public Promise<V> sync()
                        throws java.lang.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:
        java.lang.InterruptedException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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 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.