Coroutines - how to join list of Jobs?

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.

Use a parent Job and joinChildren

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
}
launch(UI){
   val job = Job()
   repeat(1000){
      async(parent = job){
         // task
      }
   }
   job.joinChildren()
   // continue work after all sub-jobs are done
}
1 Like

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 kotlinx.coroutines/coroutines-guide.md at master · Kotlin/kotlinx.coroutines · GitHub

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

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

1 Like

You can achieve the same by using coroutineScope:

coroutineScope {
    List(100_000) {
        launch {
            delay(100)
        }
    }
}

// Continue after 100 000 tasks are completed
1 Like