bj0
08/14/2018, 5:16 PMRuckus
08/14/2018, 5:20 PMbj0
08/14/2018, 5:28 PMRuckus
08/14/2018, 5:32 PMbj0
08/14/2018, 5:34 PMRuckus
08/14/2018, 5:36 PMbj0
08/14/2018, 5:38 PMRuckus
08/14/2018, 5:42 PMwhile(true)
loop when there are no tasks to run).bj0
08/14/2018, 5:43 PMRuckus
08/14/2018, 5:46 PMbj0
08/14/2018, 5:53 PMRuckus
08/14/2018, 6:01 PMcoroutines-guide.md
, I was thinking an actor would be what I'm looking for, but I'm not sure how to check if there are no more generate tasks in the channel, run the combine task, then go back to waiting for generate tasks, without closing the channel. Does that sound like I'm (roughly) on the right track, or is there a better way that comes to mind?
(I'm also not sure if actor would easily allow for running multiple generates concurrently.)bj0
08/14/2018, 6:17 PMRuckus
08/14/2018, 6:18 PMbj0
08/14/2018, 6:18 PMRuckus
08/14/2018, 6:18 PMbj0
08/14/2018, 6:19 PMsuspend fun run(generateInputs: Channel<Int>) {
val genContext = newFixedThreadPoolContext(5, "gen pool")
suspend fun genTask(input: Int) {
println("launching $input")
delay(input.toLong(), TimeUnit.SECONDS)
println("finished $input")
}
for (input in generateInputs) {
val job = launch(genContext) { genTask(input) }
while (!job.isCompleted && !generateInputs.isClosedForReceive) {
select<Unit> {
generateInputs.onReceiveOrNull {
if (it == null) {
println("chan closed")
job.join()
} else
launch(genContext, parent = job) { genTask(it) }
}
job.onJoin { println("joined") }
}
}
println("combine")
// doCombineTask()
}
}
while
loop,run
function can be turned into an actor and it would be essentially the sameRuckus
08/14/2018, 6:40 PM