# Coroutines - how to join list of Jobs?

**URL:** <https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954>\
**Category:** Uncategorized\
**Created:** [March 8, 2018, 1:27pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954 "2018-03-08T13:27:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mice777](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mice777/32/4560_2.png) [@mice777](https://discuss.kotlinlang.org/u/mice777)\
**Post date:** [March 8, 2018, 1:27pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/1 "2018-03-08T13:27:40Z")

</div>

I have something similar to this basic example of coroutines:

```auto
   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.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [March 8, 2018, 2:10pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/2 "2018-03-08T14:10:04Z")

</div>

Use a parent Job and `joinChildren`

---

<div class="post-metadata">

**Author:** ![mice777](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mice777/32/4560_2.png) [@mice777](https://discuss.kotlinlang.org/u/mice777)\
**Post date:** [March 8, 2018, 6:03pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/3 "2018-03-08T18:03:45Z")

</div>

Thanks, that works!

Actually I do this construct, hope this is correct way:

```auto
launch(UI){
   val jobs = repeat(1000){
      async(coroutineContext+CommonPool){
         // task
      }
   }
   coroutineContext[Job]!!.joinChildren()
   // continue work after all sub-jobs are done
}

```

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [March 8, 2018, 8:44pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/4 "2018-03-08T20:44:36Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![mice777](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mice777/32/4560_2.png) [@mice777](https://discuss.kotlinlang.org/u/mice777)\
**Post date:** [March 8, 2018, 9:55pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/5 "2018-03-08T21:55:21Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [March 9, 2018, 8:20am UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/6 "2018-03-09T08:20:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dmitry.zaicew](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dmitry.zaicew/32/5886_2.png) [@dmitry.zaicew](https://discuss.kotlinlang.org/u/dmitry.zaicew)\
**Post date:** [December 20, 2018, 2:21pm UTC](https://discuss.kotlinlang.org/t/coroutines-how-to-join-list-of-jobs/6954/7 "2018-12-20T14:21:37Z")

</div>

You can achieve the same by using `coroutineScope`:

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

// Continue after 100 000 tasks are completed

```
