nwh
07/16/2019, 5:25 PMMatt Thompson
07/16/2019, 5:29 PMnwh
07/16/2019, 5:43 PMval ch = Channel<Int>(5)
repeat(100) {
launch {
delay(Random.nextLong(1000))
ch.send(it)
}
}
It actually launches 100 coroutines immediately. It should launch 5 and start launching new ones as the channel's buffer is emptied. But the launching of the task doesn't occupy the channel's buffer, only a task finishing does.withoutclass
07/16/2019, 6:08 PMbj0
07/16/2019, 6:08 PMwithoutclass
07/16/2019, 6:09 PMbj0
07/16/2019, 6:09 PMnwh
07/16/2019, 6:11 PMwithoutclass
07/16/2019, 6:11 PMrepeat(5) { launch { // read stuff }}
then you have launched 5 async coroutines to read from the channelnwh
07/16/2019, 6:13 PMwithoutclass
07/16/2019, 6:13 PMfun main() = runBlocking<Unit> {
//sampleStart
val producer = produceNumbers()
repeat(5) { launchProcessor(it, producer) }
delay(950)
producer.cancel() // cancel producer coroutine and thus kill them all
//sampleEnd
}
producer
and then launch 5 workers by repeating launchProcessor
5 timesnwh
07/16/2019, 6:17 PMwithoutclass
07/16/2019, 6:18 PMchannel
, the repeat(100)
. This is completely separate from your "worker` coroutines, of which you want 5producer
repeat
is outside of your launch
nwh
07/16/2019, 6:21 PMwithoutclass
07/16/2019, 6:22 PMnwh
07/16/2019, 6:23 PMwithoutclass
07/16/2019, 6:23 PMnwh
07/16/2019, 6:52 PMTolriq
07/16/2019, 7:03 PMnwh
07/16/2019, 7:17 PMgildor
07/16/2019, 11:16 PM