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> SelectoraddCase(InputChannel<T> ch, Consumer<T> c) Adds anInputChannelCaseto the selector.<T> SelectoraddCase(OutputChannel<T> ch, T message, Runnable r) Adds anOutputChannelCaseto the selector.Adds aDelayedCaseto the selector.Adds a default case to the selector.voidrun()Executes the selector logic.static Selectorselect()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 anInputChannelCaseto the selector. The case will execute itsConsumerafter 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
Selectorinstance for method chaining
-
addCase
Adds anOutputChannelCaseto the selector. The case will execute itsRunnableafter 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
Selectorinstance for method chaining
-
addCase
Adds aDelayedCaseto the selector. The delayed case will reserve a platform thread until either another case in theSelectoror the delayed case's method is completed. This means that the amount of delayed cases in aSelectorand 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
Selectorinstance for method chaining
-
addDefault
Adds a default case to the selector. The default case will be executed if everyInputChannelCaseandOutputChannelCase's channel is empty or full respectively.- Parameters:
r- the runnable to execute as the default case- Returns:
- this
Selectorinstance 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 theSelectoror the delayed case's method is completed. This means that the amount of delayed cases in aSelectorand 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.
-