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 PMwithoutclass
07/16/2019, 6:12 PMwithoutclass
07/16/2019, 6:12 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
}
withoutclass
07/16/2019, 6:14 PMproducer
and then launch 5 workers by repeating launchProcessor
5 timeswithoutclass
07/16/2019, 6:14 PMnwh
07/16/2019, 6:17 PMnwh
07/16/2019, 6:18 PMwithoutclass
07/16/2019, 6:18 PMchannel
, the repeat(100)
. This is completely separate from your "worker` coroutines, of which you want 5withoutclass
07/16/2019, 6:19 PMproducer
withoutclass
07/16/2019, 6:19 PMrepeat
is outside of your launch
withoutclass
07/16/2019, 6:20 PMnwh
07/16/2019, 6:21 PMwithoutclass
07/16/2019, 6:22 PMnwh
07/16/2019, 6:23 PMwithoutclass
07/16/2019, 6:23 PMwithoutclass
07/16/2019, 6:24 PMwithoutclass
07/16/2019, 6:24 PMwithoutclass
07/16/2019, 6:25 PMnwh
07/16/2019, 6:52 PMTolriq
07/16/2019, 7:03 PMnwh
07/16/2019, 7:17 PMgildor
07/16/2019, 11:16 PMgildor
07/16/2019, 11:18 PM