1 // This file was automatically generated from channels.md by Knit tool. Do not edit. 2 package kotlinx.coroutines.guide.exampleChannel06 3 4 import kotlinx.coroutines.* 5 import kotlinx.coroutines.channels.* 6 <lambda>null7fun main() = runBlocking<Unit> { 8 val producer = produceNumbers() 9 repeat(5) { launchProcessor(it, producer) } 10 delay(950) 11 producer.cancel() // cancel producer coroutine and thus kill them all 12 } 13 <lambda>null14fun CoroutineScope.produceNumbers() = produce<Int> { 15 var x = 1 // start from 1 16 while (true) { 17 send(x++) // produce next 18 delay(100) // wait 0.1s 19 } 20 } 21 <lambda>null22fun CoroutineScope.launchProcessor(id: Int, channel: ReceiveChannel<Int>) = launch { 23 for (msg in channel) { 24 println("Processor #$id received $msg") 25 } 26 } 27