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 channels.md by Knit tool. Do not edit. 6 package kotlinx.coroutines.guide.exampleChannel06 7 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.channels.* 10 <lambda>null11fun main() = runBlocking<Unit> { 12 val producer = produceNumbers() 13 repeat(5) { launchProcessor(it, producer) } 14 delay(950) 15 producer.cancel() // cancel producer coroutine and thus kill them all 16 } 17 <lambda>null18fun CoroutineScope.produceNumbers() = produce<Int> { 19 var x = 1 // start from 1 20 while (true) { 21 send(x++) // produce next 22 delay(100) // wait 0.1s 23 } 24 } 25 <lambda>null26fun CoroutineScope.launchProcessor(id: Int, channel: ReceiveChannel<Int>) = launch { 27 for (msg in channel) { 28 println("Processor #$id received $msg") 29 } 30 } 31