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 aSelectAction
for a receive operationstatic <Message> SelectAction<Message>
receive(ReceivePort<? extends Message> ch, SelectReceiveListener<Message> listener)
Creates aSelectAction
for a receive operationjava.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 aSelectAction
for a send operationstatic <Message> SelectAction<Message>
send(SendPort<? super Message> ch, Message message, SelectSendListener<Message> listener)
Creates aSelectAction
for a send operationjava.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)
-
-
-
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
- Iftrue
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. Iffalse
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
- Iftrue
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. Iffalse
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 timeoutactions
- 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
- Iftrue
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. Iffalse
the order of the operations is ignored.timeout
- the method will not block for longer than the amount remaining in theTimeout
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
- Iftrue
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. Iffalse
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 timeoutpriority
- Iftrue
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. Iffalse
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 theTimeout
priority
- Iftrue
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. Iffalse
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 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.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 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
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 callingselect(false, timeout, unit, actions)
.- Parameters:
timeout
- the method will not block for longer than the amount remaining in theTimeout
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 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.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 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
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 callingselect(false, timeout, unit, actions)
.- Parameters:
timeout
- the method will not block for longer than the amount remaining in theTimeout
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
- Iftrue
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. Iffalse
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
- Iftrue
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. Iffalse
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 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 aSelectAction
for 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
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 aSelectAction
for a send operation- Type Parameters:
Message
-- Parameters:
ch
- The channel to which the operation tries to send the messagemessage
- the message to send.listener
- aSelectSendListener
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 aSelectAction
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 aSelectAction
for a receive operation- Type Parameters:
Message
-- Parameters:
ch
- the channel from which the operation tries to receivelistener
- aSelectReceiveListener
which will be triggered if this operation succeeds.- Returns:
- a receive
SelectAction
that can be selected by the selector.
-
register
public java.lang.Object register()
- Specified by:
register
in interfaceSynchronization
-
unregister
public void unregister(java.lang.Object registrationToken)
- Specified by:
unregister
in interfaceSynchronization
-
trySelect
public SelectAction<Message> trySelect() throws SuspendExecution
- Throws:
SuspendExecution
-
getWinner
public SelectAction<?> getWinner()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-