TransWikia.com

kotlin coroutines - why does Thread.sleep not pause main thread from with launch?

Stack Overflow Asked by j2emanue on December 30, 2021

I wanted to try and pause the main thread from within a courtine. a few question but first let me show you the code block im working with as a test:

class MainActivity : Activity(), CoroutineScope by MainScope() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    launch(Dispatchers.Main) {

        val threadName = Thread.currentThread()
        Log.v("mytag", "$threadName")
        Log.v("mytag", "111 ")
        Thread.sleep(5000)
        Log.v("mytag", "222")

    }

    Thread.sleep(2500) //give coroutine some time to launch fully. 
    Log.v("mytag", "333")
}

}

which yields an output of:

V/mytag: 333
V/mytag: Thread[main,5,main]
V/mytag: 111 
V/mytag: 222

since im printing out the current thread name is it not main ? i am getting "Thread[main,5,main]"

so assuming that i am on main thread then why is it when i run Thread.sleep(5000) that "333" is getting printed ? i am expecting that "333" will be last to print since i’ve frozen the main thread.

2 Answers

The dispatcher Dispatchers.Main schedules the coroutine to run as soon as main thread has been freed up.

It doesn't launch up immediately, and since your onCreate() is running on main thread as well, so the main isn't free once onCreate(), main thread looper will check for any scheduled coroutine and runs it if there were.

If you explicitly want to launch immediately as after calling the launch, and if you are the owner of the thread (in this case main) and you are the owner here because your function onCreate is running on main, you can use the Dispatchers.Main.immediate dispatcher there.

// immediate will launch immediately if current function is running on Dispatchers.Main which is true
launch(Dispatchers.Main.immediate) {
    val threadName = Thread.currentThread()
    Log.v("mytag", "$threadName")
    Log.v("mytag", "111")
    Thread.sleep(5000)
    Log.v("mytag", "222")
}

Thread.sleep(2500)
Log.v("mytag", "333")

Output (Logs):

V/mytag: Thread[main,5,main]
V/mytag: 111 
V/mytag: 222
V/mytag: 333

Answered by Animesh Sahu on December 30, 2021

so assuming that i am on main thread

Yes, I believe you are on the main thread. Coroutine infrastructure may rename the thread temporarily so you get better diagnostics on the actual coroutine running on that thread.

then why is it when i run Thread.sleep(5000) that "333" is getting printed ? i am expecting that "333" will be last to print since i've frozen the main thread.

Your launch call creates a new coroutine and adds it to the tail of the main thread's event queue. Then it goes on to block the main thread for 2.5 seconds. After that the onCreate callback may finally complete, the GUI event loop may go on processing events from the event queue, and eventually reach the one added there by your launch call, which will then again block the GUI thread for 5 seconds.

Answered by Marko Topolnik on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP