Package io.javago

Class Selector

java.lang.Object
io.javago.Selector

public class Selector extends Object
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 Details

    • select

      public static Selector select()
      Static factory method to create a new instance of Selector.
      Returns:
      a new instance of Selector
    • addCase

      public <T> Selector addCase(InputChannel<T> ch, Consumer<T> c)
      Adds an InputChannelCase to the selector. The case will execute its Consumer after it receives a message from its associated InputChannel. 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 monitor
      c - the consumer to execute when a message is received
      Returns:
      this Selector instance for method chaining
    • addCase

      public <T> Selector addCase(OutputChannel<T> ch, T message, Runnable r)
      Adds an OutputChannelCase to the selector. The case will execute its Runnable after it sends a message to its associated OutputChannel.
      Type Parameters:
      T - the type of message to send through the output channel
      Parameters:
      ch - the output channel to send the message
      message - the message to send through the output channel
      r - the callback to execute after sending the message
      Returns:
      this Selector instance for method chaining
    • addCase

      public Selector addCase(Duration d, Runnable r)
      Adds a DelayedCase to the selector. The delayed case will 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.
      Parameters:
      d - the duration to wait before executing the callback
      r - the runnable to execute after the delay
      Returns:
      this Selector instance for method chaining
    • addDefault

      public Selector addDefault(Runnable r)
      Adds a default case to the selector. The default case will be executed if every InputChannelCase and OutputChannelCase'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 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.