Select expression makes it possible to await multiple suspending functions simultaneously and select the first one that becomes available.

    Let us have two producers of strings: fizz and buzz. The fizz produces “Fizz” string every 300 ms:

    And the buzz produces “Buzz!” string every 500 ms:

    1. fun CoroutineScope.buzz() = produce<String> {
    2. while (true) { // sends "Buzz!" every 500 ms
    3. delay(500)
    4. send("Buzz!")
    5. }
    6. }

    Using suspending function we can receive either from one channel or the other. But select expression allows us to receive from both simultaneously using its clauses:

    1. suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<String>) {
    2. select<Unit> { // <Unit> means that this select expression does not produce any result
    3. fizz.onReceive { value -> // this is the first select clause
    4. println("fizz -> '$value'")
    5. }
    6. buzz.onReceive { value -> // this is the second select clause
    7. println("buzz -> '$value'")
    8. }
    9. }
    10. }

    Let us run it all seven times:

    1. import kotlinx.coroutines.*
    2. import kotlinx.coroutines.channels.*
    3. import kotlinx.coroutines.selects.*
    4. fun CoroutineScope.fizz() = produce<String> {
    5. while (true) { // sends "Fizz" every 300 ms
    6. delay(300)
    7. send("Fizz")
    8. }
    9. }
    10. fun CoroutineScope.buzz() = produce<String> {
    11. while (true) { // sends "Buzz!" every 500 ms
    12. delay(500)
    13. send("Buzz!")
    14. }
    15. }
    16. suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<String>) {
    17. select<Unit> { // <Unit> means that this select expression does not produce any result
    18. fizz.onReceive { value -> // this is the first select clause
    19. println("fizz -> '$value'")
    20. }
    21. buzz.onReceive { value -> // this is the second select clause
    22. println("buzz -> '$value'")
    23. }
    24. }
    25. }
    26. fun main() = runBlocking<Unit> {
    27. //sampleStart
    28. val fizz = fizz()
    29. val buzz = buzz()
    30. repeat(7) {
    31. selectFizzBuzz(fizz, buzz)
    32. }
    33. coroutineContext.cancelChildren() // cancel fizz & buzz coroutines
    34. //sampleEnd
    35. }

    You can get the full code here.

    The result of this code is:

    1. fizz -> 'Fizz'
    2. buzz -> 'Buzz!'
    3. fizz -> 'Fizz'
    4. fizz -> 'Fizz'
    5. buzz -> 'Buzz!'
    6. fizz -> 'Fizz'
    7. buzz -> 'Buzz!'

    The clause in select fails when the channel is closed causing the corresponding select to throw an exception. We can use onReceiveOrNull clause to perform a specific action when the channel is closed. The following example also shows that select is an expression that returns the result of its selected clause:

    1. suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String =
    2. select<String> {
    3. a.onReceiveOrNull { value ->
    4. if (value == null)
    5. "Channel 'a' is closed"
    6. else
    7. "a -> '$value'"
    8. }
    9. b.onReceiveOrNull { value ->
    10. if (value == null)
    11. else
    12. "b -> '$value'"
    13. }
    14. }

    Let’s use it with channel a that produces “Hello” string four times and channel that produces “World” four times:

    The result of this code is quite interesting, so we’ll analyze it in more detail:

    1. a -> 'Hello 0'
    2. a -> 'Hello 1'
    3. b -> 'World 0'
    4. a -> 'Hello 2'
    5. a -> 'Hello 3'
    6. b -> 'World 1'
    7. Channel 'a' is closed
    8. Channel 'a' is closed

    There are couple of observations to make out of it.

    First of all, select is biased to the first clause. When several clauses are selectable at the same time, the first one among them gets selected. Here, both channels are constantly producing strings, so a channel, being the first clause in select, wins. However, because we are using unbuffered channel, the a gets suspended from time to time on its invocation and gives a chance for b to send, too.

    The second observation, is that onReceiveOrNull gets immediately selected when the channel is already closed.

    Select expression has clause that can be used for a great good in combination with a biased nature of selection.

    Let us write an example of producer of integers that sends its values to a side channel when the consumers on its primary channel cannot keep up with it:

    1. fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> {
    2. for (num in 1..10) { // produce 10 numbers from 1 to 10
    3. delay(100) // every 100 ms
    4. select<Unit> {
    5. onSend(num) {} // Send to the primary channel
    6. side.onSend(num) {} // or to the side channel
    7. }
    8. }
    9. }

    Consumer is going to be quite slow, taking 250 ms to process each number:

    1. import kotlinx.coroutines.*
    2. import kotlinx.coroutines.channels.*
    3. import kotlinx.coroutines.selects.*
    4. fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> {
    5. for (num in 1..10) { // produce 10 numbers from 1 to 10
    6. delay(100) // every 100 ms
    7. select<Unit> {
    8. onSend(num) {} // Send to the primary channel
    9. side.onSend(num) {} // or to the side channel
    10. }
    11. }
    12. }
    13. fun main() = runBlocking<Unit> {
    14. //sampleStart
    15. val side = Channel<Int>() // allocate side channel
    16. launch { // this is a very fast consumer for the side channel
    17. side.consumeEach { println("Side channel has $it") }
    18. }
    19. produceNumbers(side).consumeEach {
    20. println("Consuming $it")
    21. delay(250) // let us digest the consumed number properly, do not hurry
    22. }
    23. println("Done consuming")
    24. coroutineContext.cancelChildren()
    25. //sampleEnd
    26. }

    So let us see what happens:

    1. Consuming 1
    2. Side channel has 2
    3. Side channel has 3
    4. Consuming 4
    5. Side channel has 5
    6. Side channel has 6
    7. Consuming 7
    8. Side channel has 8
    9. Side channel has 9
    10. Consuming 10
    11. Done consuming

    Deferred values can be selected using onAwait clause. Let us start with an async function that returns a deferred string value after a random delay:

    1. fun CoroutineScope.asyncString(time: Int) = async {
    2. delay(time.toLong())
    3. "Waited for $time ms"
    4. }

    Let us start a dozen of them with a random delay.

    Now the main function awaits for the first of them to complete and counts the number of deferred values that are still active. Note that we’ve used here the fact that select expression is a Kotlin DSL, so we can provide clauses for it using an arbitrary code. In this case we iterate over a list of deferred values to provide onAwait clause for each deferred value.

    1. import kotlinx.coroutines.*
    2. import kotlinx.coroutines.selects.*
    3. import java.util.*
    4. fun CoroutineScope.asyncString(time: Int) = async {
    5. delay(time.toLong())
    6. "Waited for $time ms"
    7. }
    8. fun CoroutineScope.asyncStringsList(): List<Deferred<String>> {
    9. val random = Random(3)
    10. return List(12) { asyncString(random.nextInt(1000)) }
    11. }
    12. fun main() = runBlocking<Unit> {
    13. //sampleStart
    14. val list = asyncStringsList()
    15. val result = select<String> {
    16. list.withIndex().forEach { (index, deferred) ->
    17. "Deferred $index produced answer '$answer'"
    18. }
    19. }
    20. }
    21. println(result)
    22. val countActive = list.count { it.isActive }
    23. println("$countActive coroutines are still active")
    24. //sampleEnd
    25. }

    The output is:

    1. Deferred 4 produced answer 'Waited for 128 ms'
    2. 11 coroutines are still active

    Let us write a channel producer function that consumes a channel of deferred string values, waits for each received deferred value, but only until the next deferred value comes over or the channel is closed. This example puts together and onAwait clauses in the same select:

    1. fun CoroutineScope.switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> {
    2. var current = input.receive() // start with first received deferred value
    3. while (isActive) { // loop while not cancelled/closed
    4. val next = select<Deferred<String>?> { // return next deferred value from this select or null
    5. input.onReceiveOrNull { update ->
    6. update // replaces next value to wait
    7. }
    8. current.onAwait { value ->
    9. send(value) // send value that current deferred has produced
    10. input.receiveOrNull() // and use the next deferred from the input channel
    11. }
    12. }
    13. if (next == null) {
    14. println("Channel was closed")
    15. break // out of loop
    16. } else {
    17. current = next
    18. }
    19. }
    20. }

    To test it, we’ll use a simple async function that resolves to a specified string after a specified time:

    1. fun CoroutineScope.asyncString(str: String, time: Long) = async {
    2. delay(time)
    3. str
    4. }

    The main function just launches a coroutine to print results of switchMapDeferreds and sends some test data to it:

    1. import kotlinx.coroutines.*
    2. import kotlinx.coroutines.channels.*
    3. import kotlinx.coroutines.selects.*
    4. fun CoroutineScope.switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> {
    5. var current = input.receive() // start with first received deferred value
    6. while (isActive) { // loop while not cancelled/closed
    7. val next = select<Deferred<String>?> { // return next deferred value from this select or null
    8. input.onReceiveOrNull { update ->
    9. update // replaces next value to wait
    10. }
    11. current.onAwait { value ->
    12. send(value) // send value that current deferred has produced
    13. input.receiveOrNull() // and use the next deferred from the input channel
    14. }
    15. }
    16. if (next == null) {
    17. println("Channel was closed")
    18. break // out of loop
    19. } else {
    20. current = next
    21. }
    22. }
    23. }
    24. fun CoroutineScope.asyncString(str: String, time: Long) = async {
    25. delay(time)
    26. str
    27. }
    28. fun main() = runBlocking<Unit> {
    29. //sampleStart
    30. val chan = Channel<Deferred<String>>() // the channel for test
    31. launch { // launch printing coroutine
    32. for (s in switchMapDeferreds(chan))
    33. println(s) // print each received string
    34. }
    35. chan.send(asyncString("BEGIN", 100))
    36. delay(200) // enough time for "BEGIN" to be produced
    37. chan.send(asyncString("Slow", 500))
    38. delay(100) // not enough time to produce slow
    39. chan.send(asyncString("Replace", 100))
    40. delay(500) // give it time before the last one
    41. chan.send(asyncString("END", 500))
    42. delay(1000) // give it time to process
    43. chan.close() // close the channel ...
    44. delay(500) // and wait some time to let it finish
    45. //sampleEnd

    The result of this code: