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 Details

    • add

      void add(Selectable selectable) throws IOException
      Adds a selectable to the selector.
      Parameters:
      selectable -
      Throws:
      IOException
    • update

      void update(Selectable selectable)
      Updates the selector to reflect any changes interest by the specified selectable. This is achieved by calling the Selectable.isReading() and Selectable.isWriting() methods.
      Parameters:
      selectable -
    • remove

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

      void select(long timeout) throws 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:
      IOException
    • readable

      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

      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

      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

      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.