Package groovy.concurrent

# Class ChannelSelect

java.lang.Object
groovy.concurrent.ChannelSelect

public final class ChannelSelect extends Object
Selects the first available value from multiple `AsyncChannel`s.

This is the channel equivalent of `Awaitable.any(Object...)` — while `Awaitable.any` races futures, `ChannelSelect` races channel receives. Each call to `select()` returns an `Awaitable` that completes with a `ChannelSelect.Result` indicating which channel produced the value and what it was.

     def prices = AsyncChannel.create(10)
     def alerts = AsyncChannel.create(10)

     def sel = ChannelSelect.from(prices, alerts)
     def result = await sel.select()
     println "Channel ${result.index}: ${result.value}"

Inspired by GPars' `Select` and Go's `select` statement.

Since:
  6.0.0

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final class`
  `ChannelSelect.Result`
  The result of a `select()` operation, indicating which channel produced the value.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static ChannelSelect`
  `from(AsyncChannel<?>... channels)`
  Creates a select over the given channels.
  `Awaitable<ChannelSelect.Result>`
  `select()`
  Waits for the first value available from any of the channels.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### from

    @SafeVarargs public static ChannelSelect from(AsyncChannel<?>... channels)
    Creates a select over the given channels.
    Parameters:
      `channels` - the channels to select from
    Returns:
      a new ChannelSelect

  - ### select

    public Awaitable<ChannelSelect.Result> select()
    Waits for the first value available from any of the channels.

    Returns an `Awaitable` that completes with a `ChannelSelect.Result` containing the channel index and the received value.

    Values consumed by non-winning channels are re-sent back to those channels to prevent message loss. This may reorder values within a channel but guarantees no values are silently dropped.

    Returns:
      an awaitable result indicating which channel produced the value