'Why show me different result when use coroutines in Android

I starting work with Coroutines and show some tutorials from Youtube!
I write below codes, I thins print 10 times Log message every 1 sec.
But not shown me 10 times!
Sometimes show me 6 times, sometimes 3 times, sometime 8 times and more ...
Why show me different result in LogCat ?

My Codes :

        CoroutineScope(Main).launch {
        val job = CoroutineScope(Main).launch {
            repeat(10){
                delay(1000)
                Log.e("Join", "Show")
            }
        }
        job.join()
        Log.e("Join", "Done")
    }

Logcat logs :

2022-02-03 20:27:13.289 21152-21152/my.app.coroutines E/Join: Show
2022-02-03 20:27:14.289 21152-21152/my.app.coroutines E/Join: Show
2022-02-03 20:27:16.292 21152-21152/my.app.coroutines E/Join: Show
2022-02-03 20:27:17.293 21152-21152/my.app.coroutines E/Join: Show
2022-02-03 20:27:18.294 21152-21152/my.app.coroutines E/Join: Show
2022-02-03 20:27:22.298 21152-21152/my.app.coroutines E/Join: Show
2022-02-03 20:27:22.299 21152-21152/my.app.coroutines E/Join: Done


Solution 1:[1]

You are running coroutine on the main dispatcher which is the IO thread as well. try using a different dispatcher like IO etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syed Faizan Ali