``` import kotlinx.coroutines.CoroutineScope impor...
# coroutines
m
Copy code
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.newFixedThreadPoolContext
import kotlinx.coroutines.runBlocking
import java.util.concurrent.atomic.AtomicInteger

@OptIn(DelicateCoroutinesApi::class)
val scope = CoroutineScope(newFixedThreadPoolContext(Runtime.getRuntime().availableProcessors(), "Background workers"))


var count = AtomicInteger(0)
fun main() {

    runBlocking {
        repeat(50_000) {
            scope.launch() {
                delay(5000)
                println(count.incrementAndGet())
            }
        }
    }

    println("Done!")
}
Why does “Done!” get printed earlier than “count” value? I want “Done!” to be printed only after completing code inside runBlocking. What’s wrong with my code? Thanks in advance.
z
Because you’re launching those coroutines into a scope that is not a child of the runBlocking scope
m
Thanks resolve the issue.