Package io.javago
Class Selector
java.lang.Object
io.javago.Selector
The
Selector
class implements Go's select
statement.
It implements a select-like mechanism for handling multiple asynchronous tasks.
It manages cases involving input channels, output channels, delayed tasks, and a default case.
When a Selector
's run()
method is called, it creates a virtual thread for each case.
In the event that one of the cases is a DelayedCase
, the delayed case will also reserve a platform thread
until either another case in the Selector
or the delayed case's method is completed.
This means that the amount of delayed cases in a Selector
and their duration should be kept to a minimum.
When a case is executed, the virtual threads for all other tasks are interrupted and any pending tasks on them are
cancelled.-
Method Summary
Modifier and TypeMethodDescription<T> Selector
addCase
(InputChannel<T> ch, Consumer<T> c) Adds anInputChannelCase
to the selector.<T> Selector
addCase
(OutputChannel<T> ch, T message, Runnable r) Adds anOutputChannelCase
to the selector.Adds aDelayedCase
to the selector.Adds a default case to the selector.void
run()
Executes the selector logic.static Selector
select()
Static factory method to create a new instance ofSelector
.
-
Method Details
-
select
Static factory method to create a new instance ofSelector
.- Returns:
- a new instance of
Selector
-
addCase
Adds anInputChannelCase
to the selector. The case will execute itsConsumer
after it receives a message from its associatedInputChannel
. The message received from the channel will be the input to the consumer.- Type Parameters:
T
- the type of messages handled by the input channel- Parameters:
ch
- the input channel to monitorc
- the consumer to execute when a message is received- Returns:
- this
Selector
instance for method chaining
-
addCase
Adds anOutputChannelCase
to the selector. The case will execute itsRunnable
after it sends a message to its associatedOutputChannel
.- Type Parameters:
T
- the type of message to send through the output channel- Parameters:
ch
- the output channel to send the messagemessage
- the message to send through the output channelr
- the callback to execute after sending the message- Returns:
- this
Selector
instance for method chaining
-
addCase
Adds aDelayedCase
to the selector. The delayed case will reserve a platform thread until either another case in theSelector
or the delayed case's method is completed. This means that the amount of delayed cases in aSelector
and their duration should be kept to a minimum.- Parameters:
d
- the duration to wait before executing the callbackr
- the runnable to execute after the delay- Returns:
- this
Selector
instance for method chaining
-
addDefault
Adds a default case to the selector. The default case will be executed if everyInputChannelCase
andOutputChannelCase
's channel is empty or full respectively.- Parameters:
r
- the runnable to execute as the default case- Returns:
- this
Selector
instance for method chaining
-
run
public void run()Executes the selector logic. Creates a virtual thread for each case. In the event that one of the cases is aDelayedCase
, the delayed case will also reserve a platform thread until either another case in theSelector
or the delayed case's method is completed. This means that the amount of delayed cases in aSelector
and their duration should be kept to a minimum. When a case is executed, the virtual threads for all other tasks are interrupted and any pending tasks on them are cancelled.
-