# Launch doesn't start a coroutine if in an infinite loop

**URL:** <https://discuss.kotlinlang.org/t/launch-doesnt-start-a-coroutine-if-in-an-infinite-loop/16814>\
**Category:** Uncategorized\
**Created:** [March 23, 2020, 4:20pm UTC](https://discuss.kotlinlang.org/t/launch-doesnt-start-a-coroutine-if-in-an-infinite-loop/16814 "2020-03-23T16:20:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zxwing](https://avatars.discourse-cdn.com/v4/letter/z/e79b87/32.png) [@zxwing](https://discuss.kotlinlang.org/u/zxwing)\
**Post date:** [March 23, 2020, 4:20pm UTC](https://discuss.kotlinlang.org/t/launch-doesnt-start-a-coroutine-if-in-an-infinite-loop/16814/1 "2020-03-23T16:20:18Z")

</div>

I found no coroutine will be started in below code:

```kotlin
runBlocking {
            while (true) {
                launch {
                    println("hello world")
                }
            }
}

```

not a single “hello world” will be printed in the above example, however, simply moving the `runBlocking` into the while loop works:

```auto
            while (true) {
               runBlocking {
                   launch {
                      println("hello world")
                   }
              }
            }

```

Can somebody explain the reason? Thank you!

---

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [March 25, 2020, 10:15am UTC](https://discuss.kotlinlang.org/t/launch-doesnt-start-a-coroutine-if-in-an-infinite-loop/16814/2 "2020-03-25T10:15:24Z")

</div>

interesting…

I always thought `suspend fun main` was just short for `fun main = runBlocking{}`…

however:

```
fun main(){
    runBlocking {
        coroutineScope {
            while (true) launch {
                println("hello world")
                delay(1)
            }
        }
    }
}

```

doesn’t work and with `suspend fun main` it does…

```
suspend fun main(){
    coroutineScope {
        while (true) launch {
            println("hello world")
            delay(1)
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [March 27, 2020, 6:01pm UTC](https://discuss.kotlinlang.org/t/launch-doesnt-start-a-coroutine-if-in-an-infinite-loop/16814/3 "2020-03-27T18:01:41Z")

</div>

`launch()` will schedule the coroutine to run on the current thread (the thread that is performing an infinite loop) if you don’t specify a different dispatcher. But in order for the scheduled coroutine to run, the current coroutine (the one created by `runBlocking()`) has to suspend. That allows the current thread to run other coroutines scheduled on it. A coroutine created with `runBlocking()` will suspend when it gets to the end, which is why your second example works. But in your first example, the outer coroutine created by `runBlocking()` never suspends – it just creates coroutine after coroutine, scheduling them on a thread which will never be allowed to run them.
