Class DefaultIoFuture

java.lang.Object
org.apache.mina.core.future.DefaultIoFuture
All Implemented Interfaces:
IoFuture
Direct Known Subclasses:
AbstractIoService.ServiceOperationFuture, CompositeIoFuture, DefaultCloseFuture, DefaultConnectFuture, DefaultReadFuture, DefaultWriteFuture

public class DefaultIoFuture extends Object implements IoFuture
A default implementation of IoFuture associated with an IoSession.
  • Field Details

    • 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:
    • session

      private final IoSession session
      The associated session
    • lock

      private final 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 List<IoFutureListener<?>> otherListeners
      All the other listeners, in case we have more than one
    • result

      private 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 Details

    • DefaultIoFuture

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

    • getSession

      public IoSession getSession()
      Specified by:
      getSession in interface IoFuture
      Returns:
      the IoSession which is associated with this future.
    • join

      @Deprecated public void join()
      Deprecated.
      Replaced with awaitUninterruptibly().
      Specified by:
      join in interface IoFuture
    • 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 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:
      InterruptedException - If the thread is interrupted while waiting
    • await

      public boolean await(long timeout, TimeUnit unit) throws 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:
      InterruptedException - If the thread is interrupted while waiting
    • await

      public boolean await(long timeoutMillis) throws 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:
      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, 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 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:
      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(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 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.
    • notifyListener

      private void notifyListener(IoFutureListener listener)