Class DefaultIoFuture

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long DEAD_LOCK_CHECK_INTERVAL
      A number of milliseconds to wait between two deadlock controls ( 5 seconds )
      private IoFutureListener<?> firstListener
      The first listener.
      private java.lang.Object lock
      A lock used by the wait() method
      private java.util.List<IoFutureListener<?>> otherListeners
      All the other listeners, in case we have more than one
      private boolean ready
      The flag used to determinate if the Future is completed or not
      private java.lang.Object result  
      private IoSession session
      The associated session
      private int waiters
      A counter for the number of threads waiting on this future
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      IoFuture addListener​(IoFutureListener<?> listener)
      Adds an event listener which is notified when this future is completed.
      IoFuture await()
      Wait for the asynchronous operation to complete.
      boolean await​(long timeoutMillis)
      Wait for the asynchronous operation to complete with the specified timeout.
      boolean await​(long timeout, java.util.concurrent.TimeUnit unit)
      Wait for the asynchronous operation to complete with the specified timeout.
      private boolean await0​(long timeoutMillis, boolean interruptable)
      Wait for the Future to be ready.
      IoFuture awaitUninterruptibly()
      Wait for the asynchronous operation to complete uninterruptibly.
      boolean awaitUninterruptibly​(long timeoutMillis)
      Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.
      boolean awaitUninterruptibly​(long timeout, java.util.concurrent.TimeUnit unit)
      Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.
      private void checkDeadLock()
      Check for a deadlock, ie look into the stack trace that we don't have already an instance of the caller.
      IoSession getSession()
      protected java.lang.Object getValue()  
      boolean isDone()
      void join()
      Deprecated.
      Replaced with awaitUninterruptibly().
      boolean join​(long timeoutMillis)
      Deprecated.
      private void notifyListener​(IoFutureListener listener)  
      private void notifyListeners()
      Notify the listeners, if we have some.
      IoFuture removeListener​(IoFutureListener<?> listener)
      Removes an existing event listener so it won't be notified when the future is completed.
      boolean setValue​(java.lang.Object newValue)
      Sets the result of the asynchronous operation, and mark it as finished.
      • Methods inherited from class java.lang.Object

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

      • DEAD_LOCK_CHECK_INTERVAL

        private static final long DEAD_LOCK_CHECK_INTERVAL
        A number of milliseconds to wait between two deadlock controls ( 5 seconds )
        See Also:
        Constant Field Values
      • session

        private final IoSession session
        The associated session
      • lock

        private final java.lang.Object lock
        A lock used by the wait() method
      • firstListener

        private IoFutureListener<?> firstListener
        The first listener. This is easier to have this variable when we most of the time have one single listener
      • otherListeners

        private java.util.List<IoFutureListener<?>> otherListeners
        All the other listeners, in case we have more than one
      • result

        private java.lang.Object result
      • ready

        private boolean ready
        The flag used to determinate if the Future is completed or not
      • waiters

        private int waiters
        A counter for the number of threads waiting on this future
    • Constructor Detail

      • DefaultIoFuture

        public DefaultIoFuture​(IoSession session)
        Creates a new instance associated with an IoSession.
        Parameters:
        session - an IoSession which is associated with this future
    • Method Detail

      • join

        @Deprecated
        public boolean join​(long timeoutMillis)
        Deprecated.
        Specified by:
        join in interface IoFuture
        Parameters:
        timeoutMillis - The time to wait for the join before bailing out
        Returns:
        true if the join was successful
      • await

        public IoFuture await()
                       throws java.lang.InterruptedException
        Wait for the asynchronous operation to complete. The attached listeners will be notified when the operation is completed.
        Specified by:
        await in interface IoFuture
        Returns:
        The instance of IoFuture that we are waiting for
        Throws:
        java.lang.InterruptedException - If the thread is interrupted while waiting
      • await

        public boolean await​(long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
        Wait for the asynchronous operation to complete with the specified timeout.
        Specified by:
        await in interface IoFuture
        Parameters:
        timeout - The maximum delay to wait before getting out
        unit - the type of unit for the delay (seconds, minutes...)
        Returns:
        true if the operation is completed.
        Throws:
        java.lang.InterruptedException - If the thread is interrupted while waiting
      • await

        public boolean await​(long timeoutMillis)
                      throws java.lang.InterruptedException
        Wait for the asynchronous operation to complete with the specified timeout.
        Specified by:
        await in interface IoFuture
        Parameters:
        timeoutMillis - The maximum milliseconds to wait before getting out
        Returns:
        true if the operation is completed.
        Throws:
        java.lang.InterruptedException - If the thread is interrupted while waiting
      • awaitUninterruptibly

        public IoFuture awaitUninterruptibly()
        Wait for the asynchronous operation to complete uninterruptibly. The attached listeners will be notified when the operation is completed.
        Specified by:
        awaitUninterruptibly in interface IoFuture
        Returns:
        the current IoFuture
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeout,
                                            java.util.concurrent.TimeUnit unit)
        Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.
        Specified by:
        awaitUninterruptibly in interface IoFuture
        Parameters:
        timeout - The maximum delay to wait before getting out
        unit - the type of unit for the delay (seconds, minutes...)
        Returns:
        true if the operation is completed.
      • awaitUninterruptibly

        public boolean awaitUninterruptibly​(long timeoutMillis)
        Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.
        Specified by:
        awaitUninterruptibly in interface IoFuture
        Parameters:
        timeoutMillis - The maximum milliseconds to wait before getting out
        Returns:
        true if the operation is finished.
      • await0

        private boolean await0​(long timeoutMillis,
                               boolean interruptable)
                        throws java.lang.InterruptedException
        Wait for the Future to be ready. If the requested delay is 0 or negative, this method immediately returns the value of the 'ready' flag. Every 5 second, the wait will be suspended to be able to check if there is a deadlock or not.
        Parameters:
        timeoutMillis - The delay we will wait for the Future to be ready
        interruptable - Tells if the wait can be interrupted or not
        Returns:
        true if the Future is ready
        Throws:
        java.lang.InterruptedException - If the thread has been interrupted when it's not allowed.
      • checkDeadLock

        private void checkDeadLock()
        Check for a deadlock, ie look into the stack trace that we don't have already an instance of the caller.
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface IoFuture
        Returns:
        true if the operation is completed.
      • setValue

        public boolean setValue​(java.lang.Object newValue)
        Sets the result of the asynchronous operation, and mark it as finished.
        Parameters:
        newValue - The result to store into the Future
        Returns:
        true if the value has been set, false if the future already has a value (thus is in ready state)
      • getValue

        protected java.lang.Object getValue()
        Returns:
        the result of the asynchronous operation.
      • addListener

        public IoFuture addListener​(IoFutureListener<?> listener)
        Adds an event listener which is notified when this future is completed. If the listener is added after the completion, the listener is directly notified.
        Specified by:
        addListener in interface IoFuture
        Parameters:
        listener - The listener to add
        Returns:
        the current IoFuture
      • removeListener

        public IoFuture removeListener​(IoFutureListener<?> listener)
        Removes an existing event listener so it won't be notified when the future is completed.
        Specified by:
        removeListener in interface IoFuture
        Parameters:
        listener - The listener to remove
        Returns:
        the current IoFuture
      • notifyListeners

        private void notifyListeners()
        Notify the listeners, if we have some.