I have something similar to this basic example of coroutines:
val jobs = List(100_000) {
launch {
delay(100)
// the task
}
}
jobs.forEach { it.join() }
But I don’t like the loop to join the jobs. Isn’t there some optimized function like joinAll(List<Job>) for such case? Currently I see that thread processing the join loop wakes up for each completed job, which seems like waste of resources if it just wants to make sure that all jobs are done.
Actually I do this construct, hope this is correct way:
launch(UI){
val jobs = repeat(1000){
async(coroutineContext+CommonPool){
// task
}
}
coroutineContext[Job]!!.joinChildren()
// continue work after all sub-jobs are done
}
Both are valid option.
Using coroutineContext you are waiting for all children in the context, using dedicated job you wait only children in the block.
You can also consider to use Job(parent = ...)