Interface Selector


  • public interface Selector
    A multiplexor of instances of Selectable.

    Many instances of Selectable can be added to a selector, and the select(long) method used to block the calling thread until one of the Selectables becomes read to perform an operation.

    This class is not thread safe, so only one thread should be manipulating the contents of the selector, or running the select(long) method at any given time.

    • Method Detail

      • add

        void add​(Selectable selectable)
          throws java.io.IOException
        Adds a selectable to the selector.
        Parameters:
        selectable -
        Throws:
        java.io.IOException
      • remove

        void remove​(Selectable selectable)
        Removes a selectable from the selector.
        Parameters:
        selectable -
      • select

        void select​(long timeout)
             throws java.io.IOException
        Waits for the specified timeout period for one or more selectables to become ready for an operation. Selectables that become ready are returned by the readable(), writeable(), expired(), or error() methods.
        Parameters:
        timeout - the maximum number of milliseconds to block the calling thread waiting for a selectable to become ready for an operation. The value zero is interpreted as check but don't block.
        Throws:
        java.io.IOException
      • readable

        java.util.Iterator<Selectable> readable()
        Returns:
        the selectables that have become readable since the last call to select(long). Calling select clears any previous values in this set before adding new values corresponding to those selectables that have become readable.
      • writeable

        java.util.Iterator<Selectable> writeable()
        Returns:
        the selectables that have become writable since the last call to select(long). Calling select clears any previous values in this set before adding new values corresponding to those selectables that have become writable.
      • expired

        java.util.Iterator<Selectable> expired()
        Returns:
        the selectables that have expired since the last call to select(long). Calling select clears any previous values in this set before adding new values corresponding to those selectables that have now expired.
      • error

        java.util.Iterator<Selectable> error()
        Returns:
        the selectables that have encountered an error since the last call to select(long). Calling select clears any previous values in this set before adding new values corresponding to those selectables that have encountered an error.
      • free

        void free()
        Frees the resources used by this selector.