Class Selector<Message>
- java.lang.Object
-
- co.paralleluniverse.strands.channels.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 aSelectActionfor a receive operationstatic <Message> SelectAction<Message>receive(ReceivePort<? extends Message> ch, SelectReceiveListener<Message> listener)Creates aSelectActionfor a receive operationjava.lang.Objectregister()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 aSelectActionfor a send operationstatic <Message> SelectAction<Message>send(SendPort<? super Message> ch, Message message, SelectSendListener<Message> listener)Creates aSelectActionfor a send operationjava.lang.StringtoString()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.voidunregister(java.lang.Object registrationToken)
-
-
-
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- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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.InterruptedExceptionSuspendExecution
-
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- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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 timeoutactions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully, or
nullif the timeout expired before an operation could complete. - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe order of the operations is ignored.timeout- the method will not block for longer than the amount remaining in theTimeoutactions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully, or
nullif the timeout expired before an operation could complete. - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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.InterruptedExceptionSuspendExecution
-
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 timeoutpriority- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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.InterruptedExceptionSuspendExecution
-
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 theTimeoutpriority- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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.InterruptedExceptionSuspendExecution
-
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 callingselect(false, actions).- Parameters:
actions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully
- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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 callingselect(false, timeout, unit, actions).- Parameters:
timeout- the maximum duration to block waiting for an operation to complete.unit- the time unit of the given timeoutactions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully, or
nullif the timeout expired before an operation could complete. - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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 callingselect(false, timeout, unit, actions).- Parameters:
timeout- the method will not block for longer than the amount remaining in theTimeoutactions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully, or
nullif the timeout expired before an operation could complete. - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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 callingselect(false, actions).- Parameters:
actions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully
- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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 callingselect(false, timeout, unit, actions).- Parameters:
timeout- the maximum duration to block waiting for an operation to complete.unit- the time unit of the given timeoutactions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully, or
nullif the timeout expired before an operation could complete. - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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 callingselect(false, timeout, unit, actions).- Parameters:
timeout- the method will not block for longer than the amount remaining in theTimeoutactions- a list of actions, one of which will be performed.- Returns:
- the action that has completed successfully, or
nullif the timeout expired before an operation could complete. - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
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- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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- Iftrueand more than one operation can complete at the same time, the one that appears in the given list first will be the one performed. Iffalsethe 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 callingtrySelect(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 callingtrySelect(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 aSelectActionfor a send operation- Type Parameters:
Message-- Parameters:
ch- The channel to which the operation tries to send the messagemessage- the message to send.- Returns:
- a send
SelectActionthat can be selected by the selector.
-
send
public static <Message> SelectAction<Message> send(SendPort<? super Message> ch, Message message, SelectSendListener<Message> listener)
Creates aSelectActionfor a send operation- Type Parameters:
Message-- Parameters:
ch- The channel to which the operation tries to send the messagemessage- the message to send.listener- aSelectSendListenerwhich will be triggered if this operation succeeds.- Returns:
- a send
SelectActionthat can be selected by the selector.
-
receive
public static <Message> SelectAction<Message> receive(ReceivePort<? extends Message> ch)
Creates aSelectActionfor a receive operation- Type Parameters:
Message-- Parameters:
ch- the channel from which the operation tries to receive- Returns:
- a receive
SelectActionthat can be selected by the selector.
-
receive
public static <Message> SelectAction<Message> receive(ReceivePort<? extends Message> ch, SelectReceiveListener<Message> listener)
Creates aSelectActionfor a receive operation- Type Parameters:
Message-- Parameters:
ch- the channel from which the operation tries to receivelistener- aSelectReceiveListenerwhich will be triggered if this operation succeeds.- Returns:
- a receive
SelectActionthat can be selected by the selector.
-
register
public java.lang.Object register()
- Specified by:
registerin interfaceSynchronization
-
unregister
public void unregister(java.lang.Object registrationToken)
- Specified by:
unregisterin interfaceSynchronization
-
trySelect
public SelectAction<Message> trySelect() throws SuspendExecution
- Throws:
SuspendExecution
-
getWinner
public SelectAction<?> getWinner()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-