Class AsyncPromise<V>

  • Type Parameters:
    V -
    All Implemented Interfaces:
    io.netty.util.concurrent.Future<V>, io.netty.util.concurrent.Promise<V>, java.util.concurrent.Future<V>
    Direct Known Subclasses:
    AsyncChannelPromise, AsyncOneResponse

    public class AsyncPromise<V>
    extends java.lang.Object
    implements io.netty.util.concurrent.Promise<V>
    Netty's DefaultPromise uses a mutex to coordinate notifiers AND waiters between the eventLoop and the other threads. Since we register cross-thread listeners, this has the potential to block internode messaging for an unknown number of threads for an unknown period of time, if we are unlucky with the scheduler (which will certainly happen, just with some unknown but low periodicity) At the same time, we manage some other efficiencies: - We save some space when registering listeners, especially if there is only one listener, as we perform no extra allocations in this case. - We permit efficient initial state declaration, avoiding unnecessary CAS or lock acquisitions when mutating a Promise we are ourselves constructing (and can easily add more; only those we use have been added) We can also make some guarantees about our behaviour here, although we primarily mirror Netty. Specifically, we can guarantee that notifiers are always invoked in the order they are added (which may be true for netty, but was unclear and is not declared). This is useful for ensuring the correctness of some of our behaviours in OutboundConnection without having to jump through extra hoops. The implementation loosely follows that of Netty's DefaultPromise, with some slight changes; notably that we have no synchronisation on our listeners, instead using a CoW list that is cleared each time we notify listeners. We handle special values slightly differently. We do not use a special value for null, instead using a special value to indicate the result has not been set yet. This means that once isSuccess() holds, the result must be a correctly typed object (modulo generics pitfalls). All special values are also instances of FailureHolder, which simplifies a number of the logical conditions.
    • Constructor Summary

      Constructors 
      Constructor Description
      AsyncPromise​(io.netty.util.concurrent.EventExecutor executor)  
      AsyncPromise​(io.netty.util.concurrent.EventExecutor executor, io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      io.netty.util.concurrent.Promise<V> addListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
      io.netty.util.concurrent.Promise<V> addListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)  
      io.netty.util.concurrent.Promise<V> await()  
      boolean await​(long timeoutMillis)  
      boolean await​(long timeout, java.util.concurrent.TimeUnit unit)  
      io.netty.util.concurrent.Promise<V> awaitUninterruptibly()  
      boolean awaitUninterruptibly​(long timeoutMillis)  
      boolean awaitUninterruptibly​(long timeout, java.util.concurrent.TimeUnit unit)  
      boolean cancel​(boolean b)  
      java.lang.Throwable cause()  
      V get()  
      V get​(long timeout, java.util.concurrent.TimeUnit unit)  
      V getNow()
      if isSuccess(), returns the value, otherwise returns null
      boolean isCancellable()  
      boolean isCancelled()  
      boolean isDone()  
      boolean isSuccess()  
      io.netty.util.concurrent.Promise<V> removeListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
      io.netty.util.concurrent.Promise<V> removeListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)  
      io.netty.util.concurrent.Promise<V> setFailure​(java.lang.Throwable throwable)  
      io.netty.util.concurrent.Promise<V> setSuccess​(V v)  
      boolean setUncancellable()  
      io.netty.util.concurrent.Promise<V> sync()
      waits for completion; in case of failure rethrows the original exception without a new wrapping exception so may cause problems for reporting stack traces
      io.netty.util.concurrent.Promise<V> syncUninterruptibly()
      waits for completion; in case of failure rethrows the original exception without a new wrapping exception so may cause problems for reporting stack traces
      java.lang.String toString()  
      boolean tryFailure​(java.lang.Throwable throwable)  
      boolean trySuccess​(V v)  
      static <V> AsyncPromise<V> uncancellable​(io.netty.util.concurrent.EventExecutor executor)  
      static <V> AsyncPromise<V> uncancellable​(io.netty.util.concurrent.EventExecutor executor, io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AsyncPromise

        public AsyncPromise​(io.netty.util.concurrent.EventExecutor executor)
      • AsyncPromise

        public AsyncPromise​(io.netty.util.concurrent.EventExecutor executor,
                            io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
    • Method Detail

      • uncancellable

        public static <V> AsyncPromise<V> uncancellable​(io.netty.util.concurrent.EventExecutor executor)
      • uncancellable

        public static <V> AsyncPromise<V> uncancellable​(io.netty.util.concurrent.EventExecutor executor,
                                                        io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
      • setSuccess

        public io.netty.util.concurrent.Promise<V> setSuccess​(V v)
        Specified by:
        setSuccess in interface io.netty.util.concurrent.Promise<V>
      • setFailure

        public io.netty.util.concurrent.Promise<V> setFailure​(java.lang.Throwable throwable)
        Specified by:
        setFailure in interface io.netty.util.concurrent.Promise<V>
      • trySuccess

        public boolean trySuccess​(V v)
        Specified by:
        trySuccess in interface io.netty.util.concurrent.Promise<V>
      • tryFailure

        public boolean tryFailure​(java.lang.Throwable throwable)
        Specified by:
        tryFailure in interface io.netty.util.concurrent.Promise<V>
      • setUncancellable

        public boolean setUncancellable()
        Specified by:
        setUncancellable in interface io.netty.util.concurrent.Promise<V>
      • cancel

        public boolean cancel​(boolean b)
        Specified by:
        cancel in interface io.netty.util.concurrent.Future<V>
        Specified by:
        cancel in interface java.util.concurrent.Future<V>
      • isSuccess

        public boolean isSuccess()
        Specified by:
        isSuccess in interface io.netty.util.concurrent.Future<V>
      • 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>
      • isCancellable

        public boolean isCancellable()
        Specified by:
        isCancellable in interface io.netty.util.concurrent.Future<V>
      • cause

        public java.lang.Throwable cause()
        Specified by:
        cause in interface io.netty.util.concurrent.Future<V>
      • getNow

        public V getNow()
        if isSuccess(), returns the value, otherwise returns null
        Specified by:
        getNow in interface io.netty.util.concurrent.Future<V>
      • get

        public V get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.util.concurrent.Future<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>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • sync

        public io.netty.util.concurrent.Promise<V> sync()
                                                 throws java.lang.InterruptedException
        waits for completion; in case of failure rethrows the original exception without a new wrapping exception so may cause problems for reporting stack traces
        Specified by:
        sync in interface io.netty.util.concurrent.Future<V>
        Specified by:
        sync in interface io.netty.util.concurrent.Promise<V>
        Throws:
        java.lang.InterruptedException
      • syncUninterruptibly

        public io.netty.util.concurrent.Promise<V> syncUninterruptibly()
        waits for completion; in case of failure rethrows the original exception without a new wrapping exception so may cause problems for reporting stack traces
        Specified by:
        syncUninterruptibly in interface io.netty.util.concurrent.Future<V>
        Specified by:
        syncUninterruptibly in interface io.netty.util.concurrent.Promise<V>
      • addListener

        public io.netty.util.concurrent.Promise<V> addListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
        Specified by:
        addListener in interface io.netty.util.concurrent.Future<V>
        Specified by:
        addListener in interface io.netty.util.concurrent.Promise<V>
      • addListeners

        public io.netty.util.concurrent.Promise<V> addListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
        Specified by:
        addListeners in interface io.netty.util.concurrent.Future<V>
        Specified by:
        addListeners in interface io.netty.util.concurrent.Promise<V>
      • removeListener

        public io.netty.util.concurrent.Promise<V> removeListener​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>> listener)
        Specified by:
        removeListener in interface io.netty.util.concurrent.Future<V>
        Specified by:
        removeListener in interface io.netty.util.concurrent.Promise<V>
      • removeListeners

        public io.netty.util.concurrent.Promise<V> removeListeners​(io.netty.util.concurrent.GenericFutureListener<? extends io.netty.util.concurrent.Future<? super V>>... listeners)
        Specified by:
        removeListeners in interface io.netty.util.concurrent.Future<V>
        Specified by:
        removeListeners in interface io.netty.util.concurrent.Promise<V>
      • await

        public io.netty.util.concurrent.Promise<V> await()
                                                  throws java.lang.InterruptedException
        Specified by:
        await in interface io.netty.util.concurrent.Future<V>
        Specified by:
        await in interface io.netty.util.concurrent.Promise<V>
        Throws:
        java.lang.InterruptedException
      • awaitUninterruptibly

        public io.netty.util.concurrent.Promise<V> awaitUninterruptibly()
        Specified by:
        awaitUninterruptibly in interface io.netty.util.concurrent.Future<V>
        Specified by:
        awaitUninterruptibly in interface io.netty.util.concurrent.Promise<V>
      • await

        public boolean await​(long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Specified by:
        await in interface io.netty.util.concurrent.Future<V>
        Throws:
        java.lang.InterruptedException
      • await

        public boolean await​(long timeoutMillis)
                      throws java.lang.InterruptedException
        Specified by:
        await in interface io.netty.util.concurrent.Future<V>
        Throws:
        java.lang.InterruptedException
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeout,
                                            java.util.concurrent.TimeUnit unit)
        Specified by:
        awaitUninterruptibly in interface io.netty.util.concurrent.Future<V>
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeoutMillis)
        Specified by:
        awaitUninterruptibly in interface io.netty.util.concurrent.Future<V>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object