Package groovy.concurrent
Class ChannelSelect
java.lang.Object
groovy.concurrent.ChannelSelect
Selects the first available value from multiple
AsyncChannels.
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 ClassesModifier and TypeClassDescriptionstatic final classThe result of aselect()operation, indicating which channel produced the value. -
Method Summary
Modifier and TypeMethodDescriptionstatic ChannelSelectfrom(AsyncChannel<?>... channels) Creates a select over the given channels.select()Waits for the first value available from any of the channels.
-
Method Details
-
from
Creates a select over the given channels.- Parameters:
channels- the channels to select from- Returns:
- a new ChannelSelect
-
select
Waits for the first value available from any of the channels.Returns an
Awaitablethat completes with aChannelSelect.Resultcontaining 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
-