public final class ChannelSelect
extends Object
Selects the first available value from multiple AsyncChannels.
This is the channel equivalent of Awaitable.any —
while Awaitable.any races futures, ChannelSelect races
channel receives. Each call to select() returns an
Awaitable that completes with a 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.
| Modifiers | Name | Description |
|---|---|---|
static class |
ChannelSelect.Result |
The result of a select() operation, indicating which channel produced the value. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static ChannelSelect |
from(AsyncChannel<?> channels)Creates a select over the given channels. |
|
public Awaitable<Result> |
select()Waits for the first value available from any of the channels. |
Creates a select over the given channels.
channels - the channels to select fromWaits for the first value available from any of the channels.
Returns an Awaitable that completes with a 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.