Does handler.post(runnable) start a new thread?
Asked Answered
I

2

5

If the handler was instantiated in the main UI thread, does a post with a Runnable create a child thread that gets added to the message queue, or does it just get run in the UI thread?

handler.post(new Runnable(){
    public void run() {
        // do stuff
    }
});
Injunction answered 6/2, 2012 at 16:24 Comment(0)
D
11

No, it doesn't create a new thread. It simply executes your runnable on the thread your handler is attached to, which in this case means your UI thread

Dutiful answered 6/2, 2012 at 16:26 Comment(0)
T
0
handler.post(new Runnable()){

public  void run(){
//do something
}
});

this does not guarantee that it will create a new thread.it will just call the runnable of the thread in which the handler is attached to(UI thread here).

Handler doesn't create a new thread, it binds to the looper of the thread that is it created in (the main thread in this case), or to a looper you give it during construction.

Twotime answered 14/5, 2014 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.