Class Promise<T>

  • Direct Known Subclasses:
    MatchingPromise

    public class Promise<T>
    extends java.lang.Object
    Allows a thread to submit an asynchronous request and to wait for the result. The caller may choose to check for the result at a later time, or immediately and it may block or not. Both the caller and responder have to know the promise.

    When the result is available, hasResult() will always return true and getResult() will return the result. In order to block for a different result, reset() has to be called first.

    Author:
    Bela Ban
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected CondVar cond  
      protected boolean hasResult  
      protected java.util.concurrent.locks.Lock lock  
      protected T result  
    • Constructor Summary

      Constructors 
      Constructor Description
      Promise()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected T _getResultWithTimeout​(long timeout)
      Blocks until a result is available, or timeout milliseconds have elapsed.
      T getResult()
      Returns when the result is available (blocking until tthe result is available)
      T getResult​(long timeout)
      Returns the result, but never throws a TimeoutException; returns null instead.
      T getResult​(long timeout, boolean reset)  
      T getResultWithTimeout​(long timeout)
      Blocks until a result is available, or timeout milliseconds have elapsed
      T getResultWithTimeout​(long timeout, boolean reset)  
      boolean hasResult()
      Checks whether result is available.
      void reset()
      Causes all waiting threads to return
      void reset​(boolean signal)  
      void setResult​(T obj)
      Sets the result and notifies any threads waiting for it
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • lock

        protected final java.util.concurrent.locks.Lock lock
      • cond

        protected final CondVar cond
      • result

        protected T result
      • hasResult

        protected volatile boolean hasResult
    • Constructor Detail

      • Promise

        public Promise()
    • Method Detail

      • getResultWithTimeout

        public T getResultWithTimeout​(long timeout)
                               throws java.util.concurrent.TimeoutException
        Blocks until a result is available, or timeout milliseconds have elapsed
        Parameters:
        timeout - in ms
        Returns:
        An object
        Throws:
        java.util.concurrent.TimeoutException - If a timeout occurred (implies that timeout > 0)
      • getResultWithTimeout

        public T getResultWithTimeout​(long timeout,
                                      boolean reset)
                               throws java.util.concurrent.TimeoutException
        Throws:
        java.util.concurrent.TimeoutException
      • getResult

        public T getResult()
        Returns when the result is available (blocking until tthe result is available)
      • getResult

        public T getResult​(long timeout)
        Returns the result, but never throws a TimeoutException; returns null instead.
        Parameters:
        timeout - in ms
        Returns:
        T
      • getResult

        public T getResult​(long timeout,
                           boolean reset)
      • hasResult

        public boolean hasResult()
        Checks whether result is available. Does not block.
      • setResult

        public void setResult​(T obj)
        Sets the result and notifies any threads waiting for it
      • reset

        public void reset()
        Causes all waiting threads to return
      • reset

        public void reset​(boolean signal)
      • toString

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

        protected T _getResultWithTimeout​(long timeout)
                                   throws java.util.concurrent.TimeoutException
        Blocks until a result is available, or timeout milliseconds have elapsed. Needs to be called with lock held
        Parameters:
        timeout - in ms
        Returns:
        An object
        Throws:
        java.util.concurrent.TimeoutException - If a timeout occurred (implies that timeout > 0)