# Coroutines println() output not overlapped

**URL:** <https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029>\
**Category:** Support\
**Created:** [October 26, 2018, 10:57am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029 "2018-10-26T10:57:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [October 26, 2018, 10:57am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/1 "2018-10-26T10:57:14Z")

</div>

My code resemble something like this

```auto
fun main(args: Array<String>) = initialize {
    0..100.map {
        async { 
            println("$it: started")
            //... execution that takes random time ...
            println("$it: finished")
        }
    }
    .forEach { it.await() }
    return@initialize
}

fun <T> initialize(block: suspend CoroutineScope.() -> T) = runBlocking {
    //...some stuff...
    val res: T = block()
    //... some other stuff...
    return@runBlocking res
}

```

The output is not overlapping:

```auto
0: started
0: finished
1: started
1: finished
...

```

How is this possible? I suspect I am doing no parallel computation at all. Using `kotlinx-coroutines-core`:`1.0.0-RC1` but I was having the same behaviour even with previous versions.

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [October 26, 2018, 11:52am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/2 "2018-10-26T11:52:15Z")

</div>

This is your code, I just reduced the number of coroutines from `0..100` to `0..10` to have a smaller output and fixed some errors so it can compile:

```kotlin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.random.Random

fun main() = initialize {
    (0..10).map {
        async {
            println("$it: started")
            delay(Random.nextLong(100, 1000)) // Simulate workload
            println("$it: finished")
        }
    }.forEach { it.await() }
    return@initialize
}

fun <T> initialize(block: suspend CoroutineScope.() -> T) = runBlocking {
    // ... some stuff ...
    val res: T = block()
    // ... some other stuff ...
    return@runBlocking res
}

```

And these are the outputs for two different executions:

> **Output 1**
>
> ```auto
> 0: started
> 1: started
> 2: started
> 3: started
> 4: started
> 5: started
> 6: started
> 7: started
> 8: started
> 9: started
> 10: started
> 3: finished
> 2: finished
> 0: finished
> 4: finished
> 8: finished
> 6: finished
> 10: finished
> 7: finished
> 1: finished
> 9: finished
> 5: finished
> 
> ```

> **Output 2**
>
> ```auto
> 0: started
> 1: started
> 2: started
> 3: started
> 4: started
> 5: started
> 6: started
> 7: started
> 8: started
> 9: started
> 10: started
> 9: finished
> 6: finished
> 1: finished
> 0: finished
> 4: finished
> 8: finished
> 7: finished
> 2: finished
> 3: finished
> 5: finished
> 10: finished
> 
> ```

So your code works as you expected, I don’t know why it doesn’t work for you. Try testing the code I posted above.

---

<div class="post-metadata">

**Author:** ![christofferberg79](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@christofferberg79](https://discuss.kotlinlang.org/u/christofferberg79)\
**Post date:** [October 26, 2018, 2:10pm UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/3 "2018-10-26T14:10:09Z")

</div>

I think the result depends on whether the “execution that takes random time” suspends or not. Compare `delay` vs `Thread.sleep`. (I can’t test it myself at the moment.)

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [October 27, 2018, 12:22am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/4 "2018-10-27T00:22:53Z")

</div>

Yes, you are right. If you use `Thread.sleep` then the output is as you say.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [October 27, 2018, 1:10am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/5 "2018-10-27T01:10:23Z")

</div>

Actually the operations I’m performing is I/O bound. I guess it’s actually blocking the thread. Should I be using threads instead then?

---

<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:** [October 27, 2018, 5:44am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/6 "2018-10-27T05:44:30Z")

</div>

`runBlocking` use the current thread and an event loop, please consider the official documentation [https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html)

And please consider [https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/await-all.html](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/await-all.html)

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [October 27, 2018, 12:28pm UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/7 "2018-10-27T12:28:50Z")

</div>

If you specify the coroutine context then the code runs as expected:

```auto
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlin.random.Random

fun main() = initialize {
    val jobs = List(10) {
        async(Dispatchers.Default) {
            println("$it: started")
            Thread.sleep(Random.nextLong(100, 1000))
            println("$it: finished")
        }
    }
    jobs.forEach { it.await() }
    return@initialize
}

fun <T> initialize(block: suspend CoroutineScope.() -> T) = runBlocking {
    // ... some stuff ...
    val res: T = block()
    // ... some other stuff ...
    return@runBlocking res
}

```

> **Output**
>
> ```auto
> 0: started
> 1: started
> 2: started
> 3: started
> 4: started
> 5: started
> 6: started
> 7: started
> 1: finished
> 8: started
> 8: finished
> 9: started
> 5: finished
> 2: finished
> 0: finished
> 7: finished
> 6: finished
> 3: finished
> 4: finished
> 9: finished
> 
> ```
> 
> Note: I have an 8 thread processor that’s why only 0-7 start and then wait for a thread to be free (1: finished) to then start another one (8: started), and so on.

Check [https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/index.html](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/index.html) to decide with one is the best dispatcher for your use case.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [October 28, 2018, 11:19am UTC](https://discuss.kotlinlang.org/t/coroutines-println-output-not-overlapped/10029/8 "2018-10-28T11:19:57Z")

</div>

That was it! Using the `IO` dispatcher I managed to actually achieve concurrency! Thank you guys 🙂

Unfortunately the SDK I’m using has restrictions on multithreading so I’ll have to figure out how to handle things properly.
