1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 // This file was automatically generated from select-expression.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleSelect03 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.channels.* 10 import kotlinx.coroutines.selects.* 11 <lambda>null12fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> { 13 for (num in 1..10) { // produce 10 numbers from 1 to 10 14 delay(100) // every 100 ms 15 select<Unit> { 16 onSend(num) {} // Send to the primary channel 17 side.onSend(num) {} // or to the side channel 18 } 19 } 20 } 21 <lambda>null22fun main() = runBlocking<Unit> { 23 val side = Channel<Int>() // allocate side channel 24 launch { // this is a very fast consumer for the side channel 25 side.consumeEach { println("Side channel has $it") } 26 } 27 produceNumbers(side).consumeEach { 28 println("Consuming $it") 29 delay(250) // let us digest the consumed number properly, do not hurry 30 } 31 println("Done consuming") 32 coroutineContext.cancelChildren() 33 } 34