Coroutines - how to join list of Jobs?


#1

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.


#2

Use a parent Job and joinChildren


#3

Thanks, that works!

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
}

#4
launch(UI){
   val job = Job()
   repeat(1000){
      async(parent = job){
         // task
      }
   }
   job.joinChildren()
   // continue work after all sub-jobs are done
}

#5

Are you sure? I’d rather make inner jobs to be children of outer job, so that all can be possibly cancelled together by cancelling the main coroutine.

Your example looks like that inner jobs can’t be cancelled any way.
See https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#children-of-a-coroutine

My intention is to run a task that can be completely and fast cancelled if needed, including all its internal sub-tasks.


#6

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 = ...)