Class Selector<Message>

  • All Implemented Interfaces:
    Synchronization

    public class Selector<Message>
    extends java.lang.Object
    implements Synchronization
    Attempts to perform at most one channel operation (send or receive) of a given set.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      SelectAction<?> getWinner()  
      static <Message> SelectAction<Message> receive​(ReceivePort<? extends Message> ch)
      Creates a SelectAction for a receive operation
      static <Message> SelectAction<Message> receive​(ReceivePort<? extends Message> ch, SelectReceiveListener<Message> listener)
      Creates a SelectAction for a receive operation
      java.lang.Object register()  
      static <Message> SelectAction<Message> select​(boolean priority, long timeout, java.util.concurrent.TimeUnit unit, SelectAction<Message>... actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
      static <Message> SelectAction<Message> select​(boolean priority, long timeout, java.util.concurrent.TimeUnit unit, java.util.List<? extends SelectAction<Message>> actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the specified timeout.
      static <Message> SelectAction<Message> select​(boolean priority, SelectAction<Message>... actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes.
      static <Message> SelectAction<Message> select​(boolean priority, Timeout timeout, SelectAction<Message>... actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
      static <Message> SelectAction<Message> select​(boolean priority, Timeout timeout, java.util.List<? extends SelectAction<Message>> actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the specified timeout.
      static <Message> SelectAction<Message> select​(boolean priority, java.util.List<? extends SelectAction<Message>> actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes.
      static <Message> SelectAction<Message> select​(long timeout, java.util.concurrent.TimeUnit unit, SelectAction<Message>... actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
      static <Message> SelectAction<Message> select​(long timeout, java.util.concurrent.TimeUnit unit, java.util.List<? extends SelectAction<Message>> actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
      static <Message> SelectAction<Message> select​(SelectAction<Message>... actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes.
      static <Message> SelectAction<Message> select​(Timeout timeout, SelectAction<Message>... actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
      static <Message> SelectAction<Message> select​(Timeout timeout, java.util.List<? extends SelectAction<Message>> actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
      static <Message> SelectAction<Message> select​(java.util.List<? extends SelectAction<Message>> actions)
      Performs exactly one channel operation of a given set, blocking until any of the actions completes.
      static <Message> SelectAction<Message> send​(SendPort<? super Message> ch, Message message)
      Creates a SelectAction for a send operation
      static <Message> SelectAction<Message> send​(SendPort<? super Message> ch, Message message, SelectSendListener<Message> listener)
      Creates a SelectAction for a send operation
      java.lang.String toString()  
      SelectAction<Message> trySelect()  
      static <Message> SelectAction<Message> trySelect​(boolean priority, SelectAction<Message>... actions)
      Attempts to performs exactly one channel operation of a given set if one can be completed without blocking.
      static <Message> SelectAction<Message> trySelect​(boolean priority, java.util.List<? extends SelectAction<Message>> actions)
      Attempts to performs exactly one channel operation of a given set if one can be completed without blocking.
      static <Message> SelectAction<Message> trySelect​(SelectAction<Message>... actions)
      Attempts to performs exactly one channel operation of a given set if one can be completed without blocking.
      static <Message> SelectAction<Message> trySelect​(java.util.List<? extends SelectAction<Message>> actions)
      Attempts to performs exactly one channel operation of a given set if one can be completed without blocking.
      void unregister​(java.lang.Object registrationToken)  
      • Methods inherited from class java.lang.Object

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

      • select

        public static <Message> SelectAction<Message> select​(boolean priority,
                                                             SelectAction<Message>... actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes.
        Parameters:
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(boolean priority,
                                                             long timeout,
                                                             java.util.concurrent.TimeUnit unit,
                                                             SelectAction<Message>... actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
        Parameters:
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        timeout - the maximum duration to block waiting for an operation to complete.
        unit - the time unit of the given timeout
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully, or null if the timeout expired before an operation could complete.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(boolean priority,
                                                             Timeout timeout,
                                                             SelectAction<Message>... actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout.
        Parameters:
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        timeout - the method will not block for longer than the amount remaining in the Timeout
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully, or null if the timeout expired before an operation could complete.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(boolean priority,
                                                             java.util.List<? extends SelectAction<Message>> actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes.
        Parameters:
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(boolean priority,
                                                             long timeout,
                                                             java.util.concurrent.TimeUnit unit,
                                                             java.util.List<? extends SelectAction<Message>> actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the specified timeout.
        Parameters:
        timeout - the maximum duration to block waiting for an operation to complete.
        unit - the time unit of the given timeout
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(boolean priority,
                                                             Timeout timeout,
                                                             java.util.List<? extends SelectAction<Message>> actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the specified timeout.
        Parameters:
        timeout - the method will not block for longer than the amount remaining in the Timeout
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(SelectAction<Message>... actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes. Same as calling select(false, actions).
        Parameters:
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(long timeout,
                                                             java.util.concurrent.TimeUnit unit,
                                                             SelectAction<Message>... actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout. Same as calling select(false, timeout, unit, actions).
        Parameters:
        timeout - the maximum duration to block waiting for an operation to complete.
        unit - the time unit of the given timeout
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully, or null if the timeout expired before an operation could complete.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(Timeout timeout,
                                                             SelectAction<Message>... actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout. Same as calling select(false, timeout, unit, actions).
        Parameters:
        timeout - the method will not block for longer than the amount remaining in the Timeout
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully, or null if the timeout expired before an operation could complete.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(java.util.List<? extends SelectAction<Message>> actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes. Same as calling select(false, actions).
        Parameters:
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(long timeout,
                                                             java.util.concurrent.TimeUnit unit,
                                                             java.util.List<? extends SelectAction<Message>> actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout. Same as calling select(false, timeout, unit, actions).
        Parameters:
        timeout - the maximum duration to block waiting for an operation to complete.
        unit - the time unit of the given timeout
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully, or null if the timeout expired before an operation could complete.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • select

        public static <Message> SelectAction<Message> select​(Timeout timeout,
                                                             java.util.List<? extends SelectAction<Message>> actions)
                                                      throws java.lang.InterruptedException,
                                                             SuspendExecution
        Performs exactly one channel operation of a given set, blocking until any of the actions completes, but no longer than the given timeout. Same as calling select(false, timeout, unit, actions).
        Parameters:
        timeout - the method will not block for longer than the amount remaining in the Timeout
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully, or null if the timeout expired before an operation could complete.
        Throws:
        java.lang.InterruptedException
        SuspendExecution
      • trySelect

        public static <Message> SelectAction<Message> trySelect​(boolean priority,
                                                                SelectAction<Message>... actions)
                                                         throws SuspendExecution
        Attempts to performs exactly one channel operation of a given set if one can be completed without blocking. This method never blocks.
        Parameters:
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        SuspendExecution
      • trySelect

        public static <Message> SelectAction<Message> trySelect​(boolean priority,
                                                                java.util.List<? extends SelectAction<Message>> actions)
                                                         throws SuspendExecution
        Attempts to performs exactly one channel operation of a given set if one can be completed without blocking. This method only blocks if the listeners associated with the select actions block.
        Parameters:
        priority - If true and more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. If false the order of the operations is ignored.
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        SuspendExecution
      • trySelect

        public static <Message> SelectAction<Message> trySelect​(SelectAction<Message>... actions)
                                                         throws SuspendExecution
        Attempts to performs exactly one channel operation of a given set if one can be completed without blocking. This method only blocks if the listeners associated with the select actions block. Same as calling trySelect(false, actions).
        Parameters:
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        SuspendExecution
      • trySelect

        public static <Message> SelectAction<Message> trySelect​(java.util.List<? extends SelectAction<Message>> actions)
                                                         throws SuspendExecution
        Attempts to performs exactly one channel operation of a given set if one can be completed without blocking. This method only blocks if the listeners associated with the select actions block.. Same as calling trySelect(false, actions).
        Parameters:
        actions - a list of actions, one of which will be performed.
        Returns:
        the action that has completed successfully
        Throws:
        SuspendExecution
      • send

        public static <Message> SelectAction<Message> send​(SendPort<? super Message> ch,
                                                           Message message)
        Creates a SelectAction for a send operation
        Type Parameters:
        Message -
        Parameters:
        ch - The channel to which the operation tries to send the message
        message - the message to send.
        Returns:
        a send SelectAction that can be selected by the selector.
      • send

        public static <Message> SelectAction<Message> send​(SendPort<? super Message> ch,
                                                           Message message,
                                                           SelectSendListener<Message> listener)
        Creates a SelectAction for a send operation
        Type Parameters:
        Message -
        Parameters:
        ch - The channel to which the operation tries to send the message
        message - the message to send.
        listener - a SelectSendListener which will be triggered if this operation succeeds.
        Returns:
        a send SelectAction that can be selected by the selector.
      • receive

        public static <Message> SelectAction<Message> receive​(ReceivePort<? extends Message> ch)
        Creates a SelectAction for a receive operation
        Type Parameters:
        Message -
        Parameters:
        ch - the channel from which the operation tries to receive
        Returns:
        a receive SelectAction that can be selected by the selector.
      • receive

        public static <Message> SelectAction<Message> receive​(ReceivePort<? extends Message> ch,
                                                              SelectReceiveListener<Message> listener)
        Creates a SelectAction for a receive operation
        Type Parameters:
        Message -
        Parameters:
        ch - the channel from which the operation tries to receive
        listener - a SelectReceiveListener which will be triggered if this operation succeeds.
        Returns:
        a receive SelectAction that can be selected by the selector.
      • unregister

        public void unregister​(java.lang.Object registrationToken)
        Specified by:
        unregister in interface Synchronization
      • toString

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